index.vue 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  1. <template>
  2. <view class="pages">
  3. <view class="" :style="{height: navHeight+'px' }"></view>
  4. <view class="page-wrap">
  5. <view class="title-wrap">
  6. <img src="../../static/img/index-title.png" alt="">
  7. </view>
  8. <view class="search-wrap u-flex">
  9. <u-search
  10. placeholder="请输入核销码"
  11. color="#009AEF"
  12. searchIconColor="#009AEF"
  13. bgColor="#F5F9FC"
  14. @search="getCheckGoods"
  15. @custom="getCheckGoods"
  16. :inputStyle="{height:'88rpx'}"
  17. :actionStyle="{'background-color':'#3ebbff',width:'88rpx',height:'88rpx','line-height':'88rpx',color:'#fff','border-radius':'16rpx'}"
  18. v-model="exchangeCode">
  19. </u-search>
  20. <view class="qr-link" @click="openScan"></view>
  21. </view>
  22. <view class="goods-wrap" v-if="showResult&&showEmpty==false">
  23. <view class="title">搜索结果</view>
  24. <view class="goods u-flex u-col-top">
  25. <img class="img" :src="checkGoods.mainImg" alt="">
  26. <view class="text">
  27. <view class="title ellipsis-2">{{checkGoods.goodsName}}</view>
  28. <view class="credit">{{checkGoods.credit}}积分</view>
  29. <view class="bottom u-flex u-row-between">
  30. <view class="left">
  31. <view class="quantity">兑换数量:{{checkGoods.quantity}}</view>
  32. <view class="userinfo">姓名:{{checkGoods.name}} / {{checkGoods.dept}} </view>
  33. </view>
  34. <u-button
  35. @click.native.stop="check()"
  36. type="primary"
  37. :customStyle="{height:'58rpx',width: 'auto','margin-right': 'unset'}"
  38. text="核销">
  39. </u-button>
  40. </view>
  41. </view>
  42. </view>
  43. </view>
  44. <u-empty mode="data" :text="emptyText" v-if="showEmpty&&!showResult"></u-empty>
  45. <view class="scan" v-show="showScan">
  46. <!-- <view class="loop_line"></view> -->
  47. <u-icon class="close" name="close-circle" color="#fff" size="30" @click="closeScan"></u-icon>
  48. <view class="video-container">
  49. <video class="video" id="video_nav_id" :controls="false"></video>
  50. </view>
  51. <view class="camera-container" v-if="videoInputDevices.length">
  52. <label>摄像头:</label>
  53. <select v-model="currentVideoInputDevice">
  54. <option
  55. v-for="(videoInputDevice, index) in videoInputDevices"
  56. :key="index"
  57. :value="videoInputDevice"
  58. >
  59. {{ videoInputDevice.label }}
  60. </option>
  61. </select>
  62. </view>
  63. </view>
  64. <tabbar :tabbarIndexProps='0' />
  65. </view>
  66. <u-toast ref="uToast"></u-toast>
  67. </view>
  68. </template>
  69. <script>
  70. import { systemInfo } from "@/mixin.js";
  71. import {
  72. BrowserMultiFormatReader,
  73. ChecksumException,
  74. FormatException,
  75. } from "@zxing/library";
  76. import { LOG } from "@zxing/library/esm/core/datamatrix/encoder/constants";
  77. import tabbar from "../../components/tabbar.vue";
  78. export default {
  79. components:{
  80. tabbar,
  81. },
  82. mixins:[systemInfo],
  83. data() {
  84. return {
  85. exchangeCode:'',
  86. codeReader: null,
  87. videoInputDevices: [],
  88. currentVideoInputDevice: {},
  89. decodeResult: undefined,
  90. showScan:false,
  91. checkGoods:{},
  92. showResult:false,
  93. emptyText:'',
  94. showEmpty:false,
  95. }
  96. },
  97. onLoad() {
  98. let that = this;
  99. // this.initEvent()
  100. },
  101. mounted() {
  102. var video = document.getElementById('video_nav_id').getElementsByTagName('video')[0]
  103. video.setAttribute('id','video-1')
  104. video.setAttribute('class','video_calss')
  105. this.codeReader = new BrowserMultiFormatReader();
  106. },
  107. onShow() {
  108. },
  109. beforeDestroy() {
  110. this.codeReader.reset(); //关闭摄像头
  111. },
  112. watch: {
  113. currentVideoInputDevice: function () {
  114. this.decodeFromInputVideo();
  115. },
  116. decodeResult: function () {
  117. this.successDecode();
  118. },
  119. exchangeCode:{
  120. handler(val){
  121. if(val.length>=8){
  122. this.getCheckGoods()
  123. }
  124. }
  125. }
  126. },
  127. methods: {
  128. getCheckGoods(){
  129. this.$u.api.getCheckGood({exchangeCode:this.exchangeCode}).then(res=>{
  130. this.checkGoods = res.data;
  131. this.showResult = true;
  132. this.showEmpty = false;
  133. // console.log('getCheckGood',this.checkGoods);
  134. }).catch(err=>{
  135. this.showEmpty = true;
  136. this.showResult = false;
  137. this.emptyText = err.msg;
  138. console.log('getCheckGood',err);
  139. })
  140. },
  141. check(){//核销
  142. this.$u.api.check({exchangeCode:this.exchangeCode}).then(res=>{
  143. this.checkGoods = {};
  144. this.showResult = false;
  145. uni.showToast({
  146. title:res.msg,
  147. icon:"success"
  148. })
  149. uni.reLaunch({url: `/center/checksuccess?success=true`});
  150. // console.log('check',this.checkGoods);
  151. }).catch(err=>{
  152. console.log('check',err);
  153. })
  154. },
  155. closeScan(){
  156. this.showScan = false;
  157. },
  158. async openScan() {
  159. uni.$u.route('/center/scan');
  160. return
  161. const _this = this;
  162. // console.log('codeReader', this.codeReader);
  163. // camera.setDisplayOrientation(90);
  164. this.showScan = true;
  165. _this.codeReader
  166. .getVideoInputDevices() //老版本listVideoInputDevices()
  167. .then((videoInputDevices) => {
  168. if (videoInputDevices && videoInputDevices.length) {
  169. if (videoInputDevices.length > 1) {
  170. videoInputDevices.reverse();
  171. } //防止先唤出前摄像头
  172. _this.videoInputDevices = videoInputDevices;
  173. _this.currentVideoInputDevice = videoInputDevices[0];
  174. }
  175. })
  176. .catch(() => {});
  177. },
  178. decodeFromInputVideo() {
  179. const _this = this;
  180. _this.codeReader.reset();
  181. // 多次
  182. try {
  183. _this.codeReader.decodeFromVideoDevice(_this.currentVideoInputDevice.deviceId, 'video-1',(res,err) => {
  184. if(res) {
  185. _this.decodeResult = res.text;
  186. }
  187. if (err) {
  188. // alert(err)
  189. if (err instanceof ChecksumException) {
  190. alert("A code was found, but it's read value was not valid.")
  191. console.log(
  192. "A code was found, but it's read value was not valid."
  193. );
  194. }
  195. if (err instanceof FormatException) {
  196. alert("A code was found, but it was in a invalid format.")
  197. console.log("A code was found, but it was in a invalid format.");
  198. }
  199. }
  200. })
  201. }catch(err){
  202. uni.showToast({title: `初始化失败${err}`,icon: 'none'});
  203. }
  204. },
  205. successDecode() {
  206. const _this = this;
  207. let result = JSON.parse(_this.decodeResult);
  208. this.exchangeCode = result.qrcode;
  209. this.closeScan();
  210. this.getCheckGoods();
  211. // alert(_this.decodeResult);
  212. },
  213. }
  214. }
  215. </script>
  216. <style>
  217. page{
  218. background-color: #fff;
  219. }
  220. </style>
  221. <style lang="scss" scoped>
  222. .title-wrap{
  223. text-align: center;
  224. margin: 100rpx auto 60rpx;
  225. }
  226. .qr-link{
  227. margin-left: 10rpx;
  228. width: 88rpx;
  229. height: 88rpx;
  230. background: url(../../static/img/qr-ico.png) no-repeat center;
  231. background-color: rgba(0,154,239,0.2);
  232. border-radius: 16rpx;
  233. }
  234. .loop_line {
  235. height: 3px;
  236. width: 80%;
  237. background-color: aliceblue;
  238. border-radius: 50%;
  239. box-shadow: 1px -4px 25px 7px rgba(255, 255, 255, 0.5);
  240. position: absolute;
  241. left: 50%;
  242. top: 5%;
  243. transform: translateX(-50%);
  244. animation: myfirst 3s infinite;
  245. z-index: 999999;
  246. }
  247. @keyframes myfirst {
  248. from {
  249. top: 5%;
  250. }
  251. to {
  252. top: 80%;
  253. }
  254. }
  255. #video_nav_id {
  256. height: 100%;
  257. width: 100%;
  258. }
  259. /deep/.uni-video-cover {
  260. display: none;
  261. }
  262. .scan {
  263. position: fixed;
  264. left: 0;
  265. top: 0;
  266. z-index: 99;
  267. display: flex;
  268. flex-direction: column;
  269. align-items: center;
  270. color: #ff9900;
  271. width: 100%;
  272. height: 100vh;
  273. background: #000;
  274. .video-container {
  275. width: 100%;
  276. height: 100%;
  277. display: flex;
  278. justify-content: center;
  279. /deep/ #video-1{
  280. width: 100%;
  281. height: 100%;
  282. }
  283. @media (min-width: 500px) {
  284. video {
  285. // width: 80vh;
  286. max-width: 96vw;
  287. height: 80vh;
  288. }
  289. }
  290. }
  291. .camera-container {
  292. margin-top: 5vh;
  293. width: 80%;
  294. height: 50px;
  295. line-height: 44px;
  296. border-radius: 10px;
  297. border: 3px solid #ff9900;
  298. display: flex;
  299. justify-content: center;
  300. select {
  301. width: calc(100% - 80px);
  302. color: #ff9900;
  303. background: transparent;
  304. border: none;
  305. outline: none;
  306. }
  307. }
  308. .close{
  309. position: absolute;
  310. right: 40rpx;
  311. top: 40rpx;
  312. z-index: 100;
  313. }
  314. }
  315. .goods-wrap{
  316. margin-top: 42rpx;
  317. .title{
  318. font-size: 32rpx;
  319. font-weight: 400;
  320. color: #999999;
  321. line-height: 44rpx;
  322. margin-bottom: 20rpx;
  323. }
  324. .goods{
  325. padding: 28rpx 0;
  326. background-color: #fff;
  327. .img{
  328. width: 200rpx;
  329. height: 202rpx;
  330. }
  331. .text{
  332. flex: 1;
  333. margin-left: 40rpx;
  334. .title{
  335. font-size: 30rpx;
  336. font-family: PingFangSC-Regular, PingFang SC;
  337. font-weight: 400;
  338. color: #333333;
  339. line-height: 42rpx;
  340. margin-bottom: 20rpx;
  341. }
  342. .credit{
  343. font-size: 32rpx;
  344. font-family: PingFangSC-Semibold, PingFang SC;
  345. font-weight: 600;
  346. color: #FFB100;
  347. line-height: 44rpx;
  348. margin-bottom: 20rpx;
  349. }
  350. .left{
  351. font-size: 28rpx;
  352. font-family: PingFangSC-Regular, PingFang SC;
  353. font-weight: 400;
  354. color: #999999;
  355. line-height: 40rpx;
  356. }
  357. }
  358. &:not(:last-of-type){
  359. border-bottom: 0.5px solid #ddd;
  360. padding-bottom: 20rpx;
  361. }
  362. }
  363. }
  364. </style>