App.vue 3.3 KB

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