scanCode.vue 11 KB

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