scanCode.vue 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <template>
  2. <view></view>
  3. </template>
  4. <script>
  5. var barcode = null;
  6. export default {
  7. data() {
  8. return {
  9. statusBarHeight: uni.getSystemInfoSync().statusBarHeight,
  10. height: 0,
  11. albumVewWidth: 0,
  12. flash: false, //是否打开摄像头
  13. currentWebview: '',
  14. typeFun: ''
  15. };
  16. },
  17. onLoad(option) {
  18. this.typeFun = option.typeFun || ''
  19. this.height = this.statusBarHeight + 44
  20. this.albumVewWidth = uni.getSystemInfoSync().windowWidth - 70
  21. this.initView()
  22. },
  23. onBackPress() {
  24. // #ifdef APP-PLUS
  25. // 返回时退出全屏
  26. barcode.close();
  27. plus.navigator.setFullscreen(false);
  28. // #endif
  29. },
  30. onUnload() {
  31. plus.navigator.setFullscreen(false);
  32. },
  33. methods: {
  34. initView() {
  35. // #ifdef APP-PLUS
  36. var pages = getCurrentPages();
  37. var page = pages[pages.length - 1];
  38. plus.navigator.setFullscreen(true); //全屏
  39. let currentWebview = page.$getAppWebview();
  40. this.currentWebview = currentWebview
  41. this.createBarcode(currentWebview); //创建二维码窗口
  42. this.createView(currentWebview); //创建操作按钮及tips界面
  43. // #endif
  44. },
  45. // 从相册中选择图片
  46. galleryImg() {
  47. let _this=this
  48. plus.gallery.pick(function(path) {
  49. _this.scanImg(path) //成功选择
  50. }, function(e) {
  51. //用户取消选择
  52. console.log("用户取消选择")
  53. barcode.start(); //开始扫码识别
  54. }, {
  55. filter: "image"
  56. });
  57. },
  58. // 从图片中扫码识别
  59. scanImg(path) {
  60. let _this=this
  61. plus.barcode.scan(path, function(type, result) {
  62. _this.onmarked(type, result)
  63. }, function(e) {
  64. uni.showToast({
  65. title: "识别失败",
  66. icon: "none",
  67. position: 'top',
  68. duration: 2000
  69. })
  70. barcode.start(); //识别失败后ios会卡住,重启扫码
  71. });
  72. },
  73. // 扫码成功回调
  74. async onmarked(type, result) {
  75. var text = '未知: ';
  76. switch (type) {
  77. case plus.barcode.QR:
  78. text = 'QR: ';
  79. break;
  80. case plus.barcode.EAN13:
  81. text = 'EAN13: ';
  82. break;
  83. case plus.barcode.EAN8:
  84. text = 'EAN8: ';
  85. break;
  86. case plus.barcode.UPCA:
  87. text = 'UPCA: ';
  88. break;
  89. case plus.barcode.UPCE:
  90. text = 'UPCE: ';
  91. break;
  92. }
  93. try {
  94. console.log('扫描结果:', type,result);
  95. const scanInfo = this.typeFun == 'getIndexScanCode' ? result : JSON.parse(result)
  96. if(typeof scanInfo === 'object' && scanInfo !== null && Object.prototype.toString.call(scanInfo) === '[object Object]'&&scanInfo.hasOwnProperty('dev_info')) {
  97. uni.$emit(this.typeFun, scanInfo.dev_info || "")
  98. uni.navigateBack()
  99. } else if(scanInfo&&scanInfo.indexOf("https://userapp.his.cdwjyyh.com/bindcompanyuser")>-1){
  100. const companyUserId = this.getUrlParam(scanInfo).companyUserId
  101. uni.$emit(this.typeFun, companyUserId || "")
  102. uni.navigateBack()
  103. } else{
  104. console.log('请扫描正确的二维码')
  105. }
  106. } catch (error) {
  107. console.log('errorerror',error)
  108. uni.showToast({
  109. title: "请扫描正确的二维码",
  110. icon: "none",
  111. position: 'top',
  112. duration: 2000
  113. })
  114. // 如果调接口,有报错,关闭后重新创建二维码
  115. barcode.close();
  116. this.createBarcode(this.currentWebview); //创建二维码窗口
  117. return
  118. }
  119. plus.navigator.setFullscreen(false);
  120. barcode.close();
  121. },
  122. getUrlParam(url) {
  123. const queryStr = url.split('?')[1];
  124. if (!queryStr) {
  125. return {};
  126. }
  127. const params = queryStr.split('&');
  128. const queryParams = {};
  129. params.forEach(param => {
  130. const [key, value] = param.split('=');
  131. queryParams[key] = value;
  132. });
  133. return queryParams;
  134. },
  135. // 创建二维码窗口
  136. createBarcode(currentWebview) {
  137. // , plus.barcode.EAN13, plus.barcode.EAN8, plus.barcode.UPCA,plus.barcode.UPCE, plus.barcode.CODE128
  138. barcode = plus.barcode.create('barcode', [plus.barcode.QR], {
  139. top: '0',
  140. left: '0',
  141. width: '100%',
  142. height: '100%',
  143. scanbarColor: '#FF7700',
  144. position: 'static',
  145. frameColor: '#FF7700',
  146. display: 'flex',
  147. justifyContent: 'center',
  148. alignItems: 'center'
  149. });
  150. barcode.onmarked = this.onmarked;
  151. barcode.setFlash(this.flash);
  152. currentWebview.append(barcode);
  153. barcode.start();
  154. },
  155. // 创建操作按钮及tips
  156. createView(currentWebview) {
  157. let _this = this;
  158. // 创建导航栏
  159. var navBarbg = new plus.nativeObj.View('navBarbg', {
  160. top: '0',
  161. left: '0',
  162. height: this.height + 'px',
  163. width: '100%',
  164. backgroundColor: "#fff",
  165. display: 'flex',
  166. justifyContent: 'center',
  167. alignItems: 'center'
  168. },
  169. [{
  170. tag: 'font',
  171. id: 'scanTitle',
  172. text: _this.typeFun == 'getIndexScanCode' ?'扫描二维码':'扫描手表二维码',
  173. textStyles: {
  174. size: '18px',
  175. color: '#222222'
  176. },
  177. position: {
  178. top: uni.getSystemInfoSync().statusBarHeight + 'px',
  179. left: '0px',
  180. width: '100%',
  181. height: '44px'
  182. }
  183. }]
  184. );
  185. // 创建返回原生按钮
  186. var backVew = new plus.nativeObj.View('backVew', {
  187. top: this.statusBarHeight + 'px',
  188. left: '3px',
  189. width: '32px',
  190. height: '44px',
  191. },
  192. [{
  193. tag: 'img',
  194. id: 'backBar',
  195. src: 'static/images/pages_watch/icons/back_black_icon.png', // 根据自己的图标修改下面的样式
  196. position: {
  197. bottom: '6px',
  198. left: '0',
  199. width: '32px',
  200. height: '32px',
  201. }
  202. }]
  203. );
  204. // 创建相册按钮
  205. var albumVew = new plus.nativeObj.View('albumVew', {
  206. top: this.statusBarHeight + 'px',
  207. left: this.albumVewWidth + 'px',
  208. width: '70px',
  209. height: '44px',
  210. },
  211. [{
  212. tag: 'font',
  213. id: 'album',
  214. text: "相册",
  215. textStyles: {
  216. size: '16px',
  217. color: '#222222',
  218. whiteSpace: 'normal'
  219. },
  220. }]
  221. );
  222. // 创建打开手电筒的按钮
  223. var scanBarVew = new plus.nativeObj.View('scanBarVew', {
  224. top: '55%',
  225. left: '40%',
  226. height: '10%',
  227. width: '20%'
  228. },
  229. [{
  230. tag: 'img',
  231. id: 'scanBar',
  232. src: 'static/images/pages_watch/icons/flashlightClose.png',
  233. position: {
  234. width: '28%',
  235. left: '36%',
  236. height: '30%'
  237. }
  238. },
  239. {
  240. tag: 'font',
  241. id: 'font',
  242. text: '轻触照亮',
  243. textStyles: {
  244. size: '10px',
  245. color: '#ffffff'
  246. },
  247. position: {
  248. width: '80%',
  249. left: '10%'
  250. }
  251. }
  252. ]
  253. );
  254. // 创建展示类内容组件
  255. var content = new plus.nativeObj.View('content', {
  256. top: '0%',
  257. left: '0px',
  258. height: '78%',
  259. width: '100%'
  260. },
  261. [{
  262. tag: 'font',
  263. id: 'scanTips',
  264. text: _this.typeFun == 'getIndexScanCode'?"扫描二维码":"请扫描手表-设置-绑定二维码进行绑定",
  265. textStyles: {
  266. size: '14px',
  267. color: '#ffffff',
  268. whiteSpace: 'normal'
  269. },
  270. position: {
  271. bottom: '10%',
  272. left: '10%',
  273. width: '80%',
  274. height: 'wrap_content'
  275. }
  276. }]);
  277. backVew.interceptTouchEvent(true);
  278. albumVew.interceptTouchEvent(true);
  279. scanBarVew.interceptTouchEvent(true);
  280. currentWebview.append(navBarbg);
  281. currentWebview.append(content);
  282. currentWebview.append(scanBarVew);
  283. currentWebview.append(backVew);
  284. currentWebview.append(albumVew);
  285. backVew.addEventListener("click", function(e) { //返回按钮
  286. uni.navigateBack({
  287. delta: 1
  288. });
  289. barcode.close();
  290. plus.navigator.setFullscreen(false);
  291. }, false);
  292. albumVew.addEventListener("click", function(e) { //相册按钮
  293. _this.galleryImg()
  294. }, false);
  295. var temp = this;
  296. scanBarVew.addEventListener("click", function(e) { //点亮手电筒
  297. temp.flash = !temp.flash;
  298. if (temp.flash) {
  299. scanBarVew.draw([{
  300. tag: 'img',
  301. id: 'scanBar',
  302. src: 'static/images/pages_watch/icons/flashlightOpen.png',
  303. position: {
  304. width: '28%',
  305. left: '36%',
  306. height: '30%'
  307. }
  308. },
  309. {
  310. tag: 'font',
  311. id: 'font',
  312. text: '轻触关闭',
  313. textStyles: {
  314. size: '10px',
  315. color: '#ffffff'
  316. },
  317. position: {
  318. width: '80%',
  319. left: '10%'
  320. }
  321. }
  322. ]);
  323. } else {
  324. scanBarVew.draw([{
  325. tag: 'img',
  326. id: 'scanBar',
  327. src: 'static/images/pages_watch/icons/flashlightClose.png',
  328. position: {
  329. width: '28%',
  330. left: '36%',
  331. height: '30%'
  332. }
  333. },
  334. {
  335. tag: 'font',
  336. id: 'font',
  337. text: '轻触照亮',
  338. textStyles: {
  339. size: '10px',
  340. color: '#ffffff'
  341. },
  342. position: {
  343. width: '80%',
  344. left: '10%'
  345. }
  346. }
  347. ])
  348. }
  349. if (barcode) {
  350. barcode.setFlash(temp.flash);
  351. }
  352. }, false)
  353. }
  354. },
  355. };
  356. </script>
  357. <style lang="scss" scoped>
  358. </style>