App.vue 3.2 KB

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