index.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. <template>
  2. <view class="scanCode">
  3. <capsule :showBorder="true">
  4. <template v-slot:top>
  5. <view class="shop-top">
  6. <view class="back-icon" @click="backClick">
  7. <tui-icon name="arrowleft" :size="42" color="#FFFFFF"></tui-icon>
  8. </view>
  9. <view class="shop-name">{{ shopInfo.shopName }}</view>
  10. </view>
  11. </template>
  12. </capsule>
  13. <view class="scanning">
  14. <view class="scan-box">
  15. <view class="border-box"></view>
  16. <view class="border-box2"></view>
  17. <view class="border-box3"></view>
  18. <view class="border-box4"></view>
  19. <camera
  20. class="scan-camera"
  21. @scancode="onScancode"
  22. @error="errorChange"
  23. @initdone="cameraReady"
  24. mode="scanCode"
  25. device-position="back"
  26. flash="auto"
  27. >
  28. <cover-view
  29. class="scan-animation"
  30. :animation="animation"
  31. ></cover-view>
  32. </camera>
  33. </view>
  34. <view class="scab-txt">将二维码/条码放到取景框中,即可自动扫描</view>
  35. </view>
  36. <view class="btn-list">
  37. <view class="btn" @click="goCoupons">
  38. <image src="@/static/image/code/keyboard.png" />
  39. <text>输码验劵</text>
  40. </view>
  41. <view class="btn" @click="openAlbum">
  42. <image src="@/static/image/code/photo.png" />
  43. <text>打开相册</text>
  44. </view>
  45. </view>
  46. <modal
  47. :showModal="modal"
  48. :promptList="promptList"
  49. @closeModal="closeModal"
  50. @btnClick="btnClick"
  51. :showBtn="showBtn"
  52. showText="确定"
  53. ></modal>
  54. </view>
  55. </template>
  56. <script>
  57. var animation = uni.createAnimation({
  58. timingFunction: "linear",
  59. delay: 0,
  60. });
  61. export default {
  62. created() {
  63. // 获取本地存储的店铺数据
  64. this.shopInfo = uni.getStorageSync("shopInfo");
  65. // setTimeout(() => {
  66. // // this.getOrderIdFromUrl('https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/pages/jump/jump?orderId=5557&type=verification&code=5557~JFHXF4LCXA')
  67. // this.getOrderIdFromUrl('https://test.tuanfengkeji.cn/TFShop_Uni_H5/#/pages/jump/jump?orderId=5065&type=verification&code=5065~JFHXUDHQUM')
  68. // // this.getOrderIdFromUrl('https://www.tuanfengkeji.cn/TFShop_Uni_H5/#/pages/jump/jump?orderId=5555&type=verification&code=5557~JFHXF4LCXA')
  69. // }, 3000)
  70. uni.getSetting({
  71. success: (res) => {
  72. let flag = res.authSetting["scope.camera"];
  73. if (!flag) {
  74. this.errorChange();
  75. }
  76. },
  77. });
  78. },
  79. onShow() {
  80. this.lineAnimation();
  81. },
  82. data() {
  83. return {
  84. // 扫描线动画
  85. animation: {},
  86. shopInfo: {},
  87. // 控制框显示
  88. modal: false,
  89. // 弹框显示的内容
  90. promptList: [],
  91. // 控制弹框按钮
  92. showBtn: false,
  93. };
  94. },
  95. methods: {
  96. lineAnimation() {
  97. this.animation = animation;
  98. let that = this;
  99. let scode = true;
  100. this.timer = setInterval(
  101. function() {
  102. if (scode) {
  103. animation.translateY(260).step({
  104. duration: 1500,
  105. });
  106. scode = !scode;
  107. } else {
  108. animation.translateY(-10).step({
  109. duration: 1500,
  110. });
  111. scode = !scode;
  112. }
  113. that.animation = animation.export();
  114. }.bind(this),
  115. 1500
  116. );
  117. },
  118. // 关闭弹框
  119. // 关闭弹框
  120. closeModal() {
  121. this.modal = false;
  122. },
  123. // 相机初始化完成回调函数
  124. onCameraInitDone() {
  125. // setInterval(() => {
  126. // this.$refs.camera.scanCode();
  127. // }, 1000);
  128. },
  129. // 用户不同意使用摄像头
  130. errorChange(e) {
  131. this.showBtn = true;
  132. this.modal = true;
  133. this.promptList = ["摄像头权限未开启", "请在设置中打开摄像头权限"];
  134. },
  135. // 相机初始化完成
  136. cameraReady() {
  137. this.modal = false;
  138. console.log("初始化完成了,打卡时间可达时间跨度");
  139. },
  140. /**扫码成功*/
  141. onScancode(e) {
  142. console.log(e.detail.result);
  143. this.getOrderIdFromUrl(e.detail.result);
  144. },
  145. // 获取订单
  146. getOrderIdFromUrl(url) {
  147. try {
  148. let march = url.match(/orderId=(\d+)/);
  149. let marchTwo = url.match(/code=([^&]*)/);
  150. // 传递值过去
  151. uni.navigateTo({
  152. url: `/pages_module/orderVerifica/index?orderId=${march[1]}&code=${marchTwo[1]}`,
  153. });
  154. } catch (error) {
  155. this.modal = true;
  156. this.showBtn = false;
  157. this.promptList = ["此二维码/条码无效", "请扫描正常的二维码/条码"];
  158. }
  159. },
  160. // 打开相册
  161. openAlbum() {
  162. uni.chooseImage({
  163. count: 1, // 最多可以选择的图片张数
  164. sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
  165. sourceType: ["album"], // 选择图片的来源,相册
  166. success: function(res) {
  167. // console.log(res,res.tempFiles[0].path);
  168. uni.scanCode({
  169. path: res.tempFiles[0].path,
  170. success: function(res) {
  171. console.log("二维码内容为:" + res.result);
  172. },
  173. fail: function(res) {
  174. console.log("识别二维码失败");
  175. },
  176. });
  177. },
  178. });
  179. },
  180. // 去到输入验劵
  181. goCoupons() {
  182. uni.navigateTo({
  183. url: "/pages_module/scanCoupons/index",
  184. });
  185. },
  186. // 确定打开设置
  187. btnClick() {
  188. this.modal = false;
  189. uni.openSetting({
  190. success: (res) => {
  191. if (res.authSetting["scope.camera"]) {
  192. uni.reLaunch({
  193. url: "/pages_module/scanCode/index",
  194. });
  195. }
  196. },
  197. });
  198. },
  199. backClick() {
  200. // uni.navigateBack()
  201. uni.switchTab({
  202. url: "/pages/tabbar/index/index",
  203. });
  204. },
  205. },
  206. };
  207. </script>
  208. <style lang="scss" scoped>
  209. %btn {
  210. width: 55rpx;
  211. height: 13rpx;
  212. background-color: #fff;
  213. position: relative;
  214. }
  215. %btnAfter {
  216. content: "";
  217. position: absolute;
  218. width: 13rpx;
  219. height: 55rpx;
  220. background-color: #fff;
  221. }
  222. .scanCode {
  223. width: 100vw;
  224. height: 100vh;
  225. background: url("https://jufeng-shop-1317254189.cos.ap-guangzhou.myqcloud.com/1717469011005-bg.png")
  226. center;
  227. background-size: 100% 100%;
  228. position: relative;
  229. .shop-top {
  230. width: 100%;
  231. height: 100%;
  232. position: relative;
  233. z-index: 999;
  234. @include flex(center);
  235. .back-icon {
  236. position: absolute;
  237. left: 0;
  238. top: 50%;
  239. transform: translateY(-50%);
  240. }
  241. .shop-name {
  242. color: #ffffff;
  243. }
  244. }
  245. .scanning {
  246. width: 100%;
  247. position: absolute;
  248. top: 50%;
  249. left: 50%;
  250. transform: translate(-50%, -50%);
  251. @include flex(center, column, 30rpx);
  252. .scan-box {
  253. width: 452rpx;
  254. height: 452rpx;
  255. position: relative;
  256. .border-box {
  257. @extend %btn;
  258. position: absolute;
  259. left: -5rpx;
  260. top: -5rpx;
  261. &::after {
  262. @extend %btnAfter;
  263. left: -2rpx;
  264. top: 0;
  265. }
  266. }
  267. .border-box2 {
  268. @extend %btn;
  269. position: absolute;
  270. right: -5rpx;
  271. top: -5rpx;
  272. &::after {
  273. @extend %btnAfter;
  274. right: -2rpx;
  275. top: 0;
  276. }
  277. }
  278. .border-box3 {
  279. @extend %btn;
  280. position: absolute;
  281. right: -5rpx;
  282. bottom: -5rpx;
  283. &::after {
  284. @extend %btnAfter;
  285. right: -2rpx;
  286. bottom: 0;
  287. }
  288. }
  289. .border-box4 {
  290. @extend %btn;
  291. position: absolute;
  292. left: -5rpx;
  293. bottom: -5rpx;
  294. &::after {
  295. @extend %btnAfter;
  296. left: -2rpx;
  297. bottom: 0;
  298. }
  299. }
  300. .scan-camera {
  301. width: 100%;
  302. height: 100%;
  303. }
  304. .scan-animation {
  305. position: absolute;
  306. top: 5%;
  307. left: 0;
  308. width: 100%;
  309. height: 3rpx;
  310. background-color: $primary-color;
  311. border-radius: 50%;
  312. }
  313. }
  314. .scab-txt {
  315. font-size: 28rpx;
  316. color: #ffffff;
  317. }
  318. }
  319. .btn-list {
  320. position: absolute;
  321. left: 50%;
  322. bottom: 104rpx;
  323. transform: translateX(-50%);
  324. @include flex(center, null, 60rpx);
  325. display: none;
  326. .btn {
  327. width: 276rpx;
  328. height: 108rpx;
  329. background: rgba(255, 255, 255, 0.4);
  330. @include flex(center, null, 22rpx);
  331. border-radius: 24rpx;
  332. image {
  333. width: 44rpx;
  334. height: 36rpx;
  335. display: block;
  336. }
  337. text {
  338. font-size: 36rpx;
  339. color: #e6e4e5;
  340. }
  341. }
  342. }
  343. }
  344. </style>