scan.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <template>
  2. <view class="scancode">
  3. <view class="scanKuang">
  4. <view class="kuang" id='barcodekuang'></view>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. // import {
  10. // equipmentScanApi,
  11. // subtenancyApplicationApi
  12. // } from '@/common/api/homeApi.js'
  13. export default {
  14. data() {
  15. return {
  16. barcode: null,
  17. flash: false,
  18. isUser: true,
  19. }
  20. },
  21. onLoad(opt) {
  22. // #ifdef APP-PLUS
  23. plus.navigator.setFullscreen(true); //全屏
  24. let currentWebview = this.$scope.$getAppWebview();
  25. this.createBarcode(currentWebview)
  26. this.createFlashBarView(currentWebview)
  27. this.createTipInfoView(currentWebview)
  28. // #endif
  29. },
  30. onBackPress() {
  31. uni.navigateBack({
  32. delta: 1
  33. })
  34. },
  35. methods: {
  36. toStart() {
  37. this.barcode.start()
  38. },
  39. goback() {
  40. uni.switchTab({
  41. url: '/pages/home/index'
  42. })
  43. },
  44. /**
  45. * 创建二维码
  46. * @param {Object} currentWebview
  47. */
  48. createBarcode(currentWebview) {
  49. if (!this.barcode) {
  50. this.barcode = plus.barcode.create('barcodekuang', [plus.barcode.QR], {
  51. top: `auto`,
  52. left: 'auto',
  53. height: `100%`,
  54. width: '100%',
  55. position:'absolute',
  56. background: '#1A86F4',
  57. frameColor: '#1A86F4',
  58. scanbarColor: '#1A86F4',
  59. });
  60. this.barcode.onmarked = this.onmarked;
  61. this.barcode.setFlash(this.flash);
  62. //此处未演示扫码成功回调的地址设置,实际请参考HTML5Plus API自行处理
  63. //注意扫码区域需为正方形,否则影响扫码识别率
  64. currentWebview.append(this.barcode);
  65. this.barcode.start()
  66. }
  67. },
  68. /**
  69. * 创建提示信息
  70. * @param {Object} currentWebview
  71. */
  72. createTipInfoView(currentWebview) {
  73. const content = new plus.nativeObj.View('content', {
  74. bottom: '170px',
  75. height: '50px',
  76. width: '100%',
  77. textAlign:'center',
  78. },
  79. [{
  80. tag: 'font',
  81. id: 'scanTips',
  82. text: '请将QR Code放入框内,即可自动扫描',
  83. textStyles: {
  84. size: '14px',
  85. color: '#ffffff',
  86. whiteSpace: 'normal'
  87. },
  88. position: {
  89. top: '15px',
  90. left: '10%',
  91. width: '80%',
  92. height: 'wrap_content'
  93. }
  94. }]);
  95. currentWebview.append(content);
  96. const contentBtn = new plus.nativeObj.View('contentBtn', {
  97. bottom: '80px',
  98. left: '15%',
  99. height: '40px',
  100. width: '70%',
  101. textAlign:'center',
  102. backgroundColor: '#1A86F4',
  103. borderRadius: '20px',
  104. lineHeight:'30px'
  105. },
  106. [{
  107. tag: 'font',
  108. id: 'scanTips',
  109. text: '手动输入',
  110. textStyles: {
  111. size: '16px',
  112. color: '#ffffff',
  113. whiteSpace: 'normal'
  114. },
  115. position: {
  116. top: '15px',
  117. left: '10%',
  118. width: '80%',
  119. height: 'wrap_content'
  120. }
  121. }]);
  122. currentWebview.append(contentBtn);
  123. contentBtn.addEventListener('click', () => {
  124. this.inputCode();
  125. })
  126. },
  127. // 创建 开关灯按钮
  128. createFlashBarView(currentWebview) {
  129. const openImg = this.crtFlashImg()
  130. const closeImg = this.crtFlashImg()
  131. const scanBarVew = new plus.nativeObj.View('scanBarVew', {
  132. top: '425px',
  133. left: '40%',
  134. height: '10%',
  135. width: '20%',
  136. },
  137. closeImg);
  138. scanBarVew.interceptTouchEvent(true);
  139. currentWebview.append(scanBarVew);
  140. scanBarVew.addEventListener("click", (e) => { //点亮手电筒
  141. this.flash = !this.flash;
  142. if (this.flash) {
  143. scanBarVew.draw(openImg);
  144. } else {
  145. scanBarVew.draw(closeImg)
  146. }
  147. if (this.barcode) {
  148. this.barcode.setFlash(this.flash);
  149. }
  150. }, false)
  151. },
  152. crtFlashImg() {
  153. return [{
  154. tag: 'font',
  155. id: 'font',
  156. text: '轻触照亮',
  157. textStyles: {
  158. size: '14px',
  159. color: '#ffffff'
  160. },
  161. position: {
  162. width: '100%',
  163. left: '10%',
  164. // top: `500px`,
  165. }
  166. }]
  167. },
  168. // 扫码成功回调
  169. onmarked(type, result) {
  170. let obj = this.barcode
  171. console.log('条码类型:' + type);
  172. console.log('条码内容:' + result);
  173. // 业务代码
  174. // 核对扫描结果
  175. // 判断是否是正确的格式
  176. // 不正确则跳转到 错误页面
  177. if (type == 0) {
  178. // equipmentScanApi({
  179. // userid: getApp().globalData.userid,
  180. // qrcodecontent: result,
  181. // }).then((res) => {
  182. // if (res.resultCode === 0) {
  183. // if (this.isUser) {
  184. // uni.redirectTo({
  185. // url: '/pages/home/linkEpt?code=' + result
  186. // })
  187. // } else {
  188. // uni.redirectTo({
  189. // url: '/pages/equipment/devicedetails?equipmentId=' + res.data.equipmentid + '&outboundId=' +
  190. // res.data.outboundid
  191. // })
  192. // }
  193. // } else {
  194. // // console.log('错误')
  195. // uni.showToast({
  196. // icon: 'none',
  197. // title: res.resultMsg
  198. // })
  199. // }
  200. // })
  201. } else {
  202. uni.showToast({
  203. icon: 'none',
  204. title: '请扫描正确的二维码'
  205. })
  206. }
  207. },
  208. inputCode() {
  209. uni.redirectTo({
  210. url: '/pages/home/inputCode'
  211. })
  212. }
  213. }
  214. }
  215. </script>
  216. <style lang="scss">
  217. .scancode {
  218. height: 100vh;
  219. background: #1B1B1F;
  220. // padding-top: 20px;
  221. position: relative;
  222. .scanKuang {
  223. // margin-top: 240rpx;
  224. // text-align: center;
  225. width: 100%;
  226. height: 100%;
  227. .kuang {
  228. width: 100%;
  229. height: 100%;
  230. // display: inline-block;
  231. // background: url('../../static/img/saomiaokuang.png') no-repeat;
  232. // background-size: 100% 100%;
  233. }
  234. }
  235. .desc {
  236. margin-top: 150rpx;
  237. font-size: 30rpx;
  238. font-family: PingFang SC;
  239. font-weight: 400;
  240. color: #FFFFFF;
  241. text-align: center;
  242. }
  243. .btnsty {
  244. text-align: center;
  245. margin-top: 122rpx;
  246. .btnin {
  247. width: 520rpx;
  248. height: 80rpx;
  249. background: linear-gradient(90deg, #0173F4 0%, #65BFF5 100%);
  250. border-radius: 20rpx;
  251. line-height: 80rpx;
  252. color: #FFFFFF;
  253. display: inline-block;
  254. }
  255. }
  256. }
  257. .barcode {
  258. width: 534rpx;
  259. height: 534rpx;
  260. }
  261. </style>