index.vue 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. delta: 1,
  202. fail: () => {
  203. uni.switchTab({
  204. url: "/pages/tabbar/index/index",
  205. });
  206. },
  207. });
  208. },
  209. },
  210. };
  211. </script>
  212. <style lang="scss" scoped>
  213. %btn {
  214. width: 55rpx;
  215. height: 13rpx;
  216. background-color: #fff;
  217. position: relative;
  218. }
  219. %btnAfter {
  220. content: "";
  221. position: absolute;
  222. width: 13rpx;
  223. height: 55rpx;
  224. background-color: #fff;
  225. }
  226. .scanCode {
  227. width: 100vw;
  228. height: 100vh;
  229. background: url("https://jufeng-shop-1317254189.cos.ap-guangzhou.myqcloud.com/1717469011005-bg.png")
  230. center;
  231. background-size: 100% 100%;
  232. position: relative;
  233. .shop-top {
  234. width: 100%;
  235. height: 100%;
  236. position: relative;
  237. z-index: 999;
  238. @include flex(center);
  239. .back-icon {
  240. position: absolute;
  241. left: 0;
  242. top: 50%;
  243. transform: translateY(-50%);
  244. }
  245. .shop-name {
  246. color: #ffffff;
  247. }
  248. }
  249. .scanning {
  250. width: 100%;
  251. position: absolute;
  252. top: 50%;
  253. left: 50%;
  254. transform: translate(-50%, -50%);
  255. @include flex(center, column, 30rpx);
  256. .scan-box {
  257. width: 452rpx;
  258. height: 452rpx;
  259. position: relative;
  260. .border-box {
  261. @extend %btn;
  262. position: absolute;
  263. left: -5rpx;
  264. top: -5rpx;
  265. &::after {
  266. @extend %btnAfter;
  267. left: -2rpx;
  268. top: 0;
  269. }
  270. }
  271. .border-box2 {
  272. @extend %btn;
  273. position: absolute;
  274. right: -5rpx;
  275. top: -5rpx;
  276. &::after {
  277. @extend %btnAfter;
  278. right: -2rpx;
  279. top: 0;
  280. }
  281. }
  282. .border-box3 {
  283. @extend %btn;
  284. position: absolute;
  285. right: -5rpx;
  286. bottom: -5rpx;
  287. &::after {
  288. @extend %btnAfter;
  289. right: -2rpx;
  290. bottom: 0;
  291. }
  292. }
  293. .border-box4 {
  294. @extend %btn;
  295. position: absolute;
  296. left: -5rpx;
  297. bottom: -5rpx;
  298. &::after {
  299. @extend %btnAfter;
  300. left: -2rpx;
  301. bottom: 0;
  302. }
  303. }
  304. .scan-camera {
  305. width: 100%;
  306. height: 100%;
  307. }
  308. .scan-animation {
  309. position: absolute;
  310. top: 5%;
  311. left: 0;
  312. width: 100%;
  313. height: 3rpx;
  314. background-color: $primary-color;
  315. border-radius: 50%;
  316. }
  317. }
  318. .scab-txt {
  319. font-size: 28rpx;
  320. color: #ffffff;
  321. }
  322. }
  323. .btn-list {
  324. position: absolute;
  325. left: 50%;
  326. bottom: 104rpx;
  327. transform: translateX(-50%);
  328. @include flex(center, null, 60rpx);
  329. display: none;
  330. .btn {
  331. width: 276rpx;
  332. height: 108rpx;
  333. background: rgba(255, 255, 255, 0.4);
  334. @include flex(center, null, 22rpx);
  335. border-radius: 24rpx;
  336. image {
  337. width: 44rpx;
  338. height: 36rpx;
  339. display: block;
  340. }
  341. text {
  342. font-size: 36rpx;
  343. color: #e6e4e5;
  344. }
  345. }
  346. }
  347. }
  348. </style>