App.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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(
  74. "android.content.IntentFilter"
  75. );
  76. filter = new IntentFilter();
  77. filter.addAction("com.pdapengyue.scan"); // 换你的广播动作
  78. receiver = plus.android.implements(
  79. "io.dcloud.feature.internal.reflect.BroadcastReceiver",
  80. {
  81. onReceive: function (context, intent) {
  82. plus.android.importClass(intent);
  83. let data = intent.getStringExtra("pdapengyuescannerdata"); // 换你的广播标签
  84. // console.log("条码信息:" + data)
  85. uni.$emit("scanFinish", uni.$u.trim(data));
  86. // uni.showModal({
  87. // title: '收到条码信息',
  88. // content: '' + data,
  89. // success: function(res) {
  90. // if (res.confirm) {
  91. // // 执行确认后的操作
  92. // } else {
  93. // // 执行取消后的操作
  94. // }
  95. // }
  96. // })
  97. _this.queryCode(code);
  98. },
  99. }
  100. );
  101. },
  102. startScan() {
  103. main.registerReceiver(receiver, filter);
  104. },
  105. stopScan() {
  106. main.unregisterReceiver(receiver);
  107. },
  108. queryCode(code) {
  109. //防重复
  110. if (_codeQueryTag) return false;
  111. _codeQueryTag = true;
  112. setTimeout(function () {
  113. _codeQueryTag = false;
  114. }, 150);
  115. var id = code;
  116. uni.$emit("scancodedate", {
  117. code: id,
  118. });
  119. },
  120. },
  121. };
  122. </script>
  123. <style lang="scss">
  124. /*每个页面公共css */
  125. @import "@/uni_modules/uview-ui/index.scss";
  126. /* #ifndef APP-NVUE */
  127. @import "@/common/css/public.scss";
  128. /* #endif */
  129. .depot-label {
  130. display: flex;
  131. align-items: center;
  132. .name {
  133. color: rgba(51, 51, 51, 1);
  134. text-align: center;
  135. font-family: "PingFang SC";
  136. font-size: 28rpx;
  137. font-weight: 600;
  138. margin-right: 10rpx;
  139. }
  140. }
  141. </style>