scanCode.vue 11 KB

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