scam.vue 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <template>
  2. <view class="content"></view>
  3. </template>
  4. <script>
  5. // #ifdef APP-PLUS
  6. var main, receiver, filter;
  7. var _codeQueryTag = false;
  8. export default {
  9. data() {
  10. return {
  11. scanCode: "",
  12. };
  13. },
  14. created: function (option) {
  15. this.initScan();
  16. this.startScan();
  17. },
  18. onHide: function () {
  19. this.stopScan();
  20. },
  21. destroyed: function () {
  22. /*页面退出时一定要卸载监听,否则下次进来时会重复,造成扫一次出2个以上的结果*/
  23. this.stopScan();
  24. },
  25. methods: {
  26. initScan() {
  27. let _this = this;
  28. main = plus.android.runtimeMainActivity(); //获取activity
  29. var IntentFilter = plus.android.importClass(
  30. "android.content.IntentFilter"
  31. );
  32. filter = new IntentFilter();
  33. filter.addAction("nlscan.action.SCANNER_RESULT"); // 换你的广播动作
  34. receiver = plus.android.implements(
  35. "io.dcloud.feature.internal.reflect.BroadcastReceiver",
  36. {
  37. onReceive: function (context, intent) {
  38. plus.android.importClass(intent);
  39. let code = intent.getStringExtra("SCAN_BARCODE1"); // 换你的广播标签
  40. _this.queryCode(code);
  41. },
  42. }
  43. );
  44. },
  45. startScan() {
  46. main.registerReceiver(receiver, filter);
  47. },
  48. stopScan() {
  49. main.unregisterReceiver(receiver);
  50. },
  51. queryCode: function (code) {
  52. //防重复
  53. if (_codeQueryTag) return false;
  54. _codeQueryTag = true;
  55. setTimeout(function () {
  56. _codeQueryTag = false;
  57. }, 150);
  58. var id = code;
  59. uni.$emit("scancodedate", { code: id });
  60. },
  61. },
  62. };
  63. // #endif
  64. </script>