App.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <script>
  2. var main, receiver, filter;
  3. var _codeQueryTag = false;
  4. export default {
  5. onLaunch(options) {
  6. // #ifdef APP-PLUS
  7. // 重置app更新信息
  8. uni.setStorageSync('update', false);
  9. uni.setStorageSync('downloadApkParams', null);
  10. console.log('onLaunch')
  11. // 检测客户端更新
  12. this.$store.dispatch('update/update', 0)
  13. // 初始化蓝牙
  14. this.$store.dispatch('ble/initBle')
  15. // #endif
  16. },
  17. onShow() {
  18. // #ifdef APP-PLUS
  19. this.initScan();
  20. this.startScan();
  21. this.setBROADCAST()
  22. this.setBROADCASTACTION()
  23. // #endif
  24. // getApp().globalData.uploadUrl = this.$UPLOAD_URL
  25. // getApp().globalData.img_url = this.$IMG_URL
  26. },
  27. onHide() {
  28. this.stopScan();
  29. },
  30. // 应用退出,断开连接
  31. onExit(){
  32. // #ifdef APP-PLUS
  33. this.$store.dispatch('ble/closeBluetooth')
  34. // #endif
  35. },
  36. // globalData: {
  37. // uploadUrl: '',
  38. // img_url: ''
  39. // },
  40. methods: {
  41. setBROADCAST() {
  42. var main2 = plus.android.runtimeMainActivity(); //获取acitivity
  43. var Intent = plus.android.importClass("android.content.Intent");
  44. var intent2 = new Intent("com.android.scanner.service_settings");
  45. intent2.putExtra("barcode_send_mode", "BROADCAST");
  46. main2.sendBroadcast(intent2);
  47. },
  48. setBROADCASTACTION() {
  49. var main2 = plus.android.runtimeMainActivity(); //获取acitivity
  50. var Intent = plus.android.importClass("android.content.Intent");
  51. var intent2 = new Intent("com.android.scanner.service_settings");
  52. intent2.putExtra("action_barcode_broadcast", "com.pdapengyue.scan");
  53. main2.sendBroadcast(intent2);
  54. },
  55. // 监听设备按键
  56. key() {
  57. plus.key.addEventListener('keydown', event => {
  58. console.log("按键:" + event.keyCode);
  59. });
  60. },
  61. // 点击触发激光扫码
  62. ScanCode() {
  63. var main2 = plus.android.runtimeMainActivity(); //获取acitivity
  64. var Intent = plus.android.importClass("android.content.Intent");
  65. var intent2 = new Intent("com.scan.onStartScan");
  66. main2.sendBroadcast(intent2);
  67. },
  68. initScan() {
  69. var vendor = plus.device.vendor; // 获取设备制造商
  70. console.log("厂商:"+vendor);
  71. let _this = this;
  72. main = plus.android.runtimeMainActivity(); //获取activity
  73. var IntentFilter = plus.android.importClass('android.content.IntentFilter');
  74. filter = new IntentFilter();
  75. filter.addAction("com.pdapengyue.scan"); // 换你的广播动作
  76. receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
  77. onReceive: function(context, intent) {
  78. plus.android.importClass(intent);
  79. let data = intent.getStringExtra("pdapengyuescannerdata"); // 换你的广播标签
  80. // console.log("条码信息:" + data)
  81. uni.$emit('scanFinish',uni.$u.trim(data))
  82. // uni.showModal({
  83. // title: '收到条码信息',
  84. // content: '' + data,
  85. // success: function(res) {
  86. // if (res.confirm) {
  87. // // 执行确认后的操作
  88. // } else {
  89. // // 执行取消后的操作
  90. // }
  91. // }
  92. // })
  93. _this.queryCode(code);
  94. }
  95. });
  96. },
  97. startScan() {
  98. main.registerReceiver(receiver, filter);
  99. },
  100. stopScan() {
  101. main.unregisterReceiver(receiver);
  102. },
  103. queryCode(code) {
  104. //防重复
  105. if (_codeQueryTag) return false;
  106. _codeQueryTag = true;
  107. setTimeout(function() {
  108. _codeQueryTag = false;
  109. }, 150);
  110. var id = code
  111. uni.$emit('scancodedate', {
  112. code: id
  113. })
  114. }
  115. }
  116. }
  117. </script>
  118. <style lang="scss">
  119. /*每个页面公共css */
  120. @import '@/uni_modules/uview-ui/index.scss';
  121. /* #ifndef APP-NVUE */
  122. @import '@/common/css/public.scss';
  123. /* #endif */
  124. </style>