scanCode.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. let main, receiver, filter;
  2. let _codeQueryTag = false;
  3. export const initScan = () => {
  4. let _this = this;
  5. main = plus.android.runtimeMainActivity(); //获取activity
  6. var IntentFilter = plus.android.importClass("android.content.IntentFilter");
  7. filter = new IntentFilter();
  8. filter.addAction("nlscan.action.SCANNER_RESULT"); // 换你的广播动作
  9. receiver = plus.android.implements(
  10. "io.dcloud.feature.internal.reflect.BroadcastReceiver",
  11. {
  12. onReceive: function (context, intent) {
  13. plus.android.importClass(intent);
  14. let code = intent.getStringExtra("SCAN_BARCODE1"); // 换你的广播标签
  15. queryCode(code);
  16. },
  17. }
  18. );
  19. };
  20. export const startScan = () => {
  21. main.registerReceiver(receiver, filter);
  22. };
  23. export const stopScan = () => {
  24. main.unregisterReceiver(receiver);
  25. //解绑uni的全局$on 不然会有bug
  26. uni.$off("SCAN_CODE");
  27. };
  28. export const queryCode = (code) => {
  29. //防重复
  30. if (_codeQueryTag) return false;
  31. _codeQueryTag = true;
  32. setTimeout(function () {
  33. _codeQueryTag = false;
  34. }, 150);
  35. var id = code;
  36. //id就是扫码的结果
  37. uni.$emit("SCAN_CODE", { code: id });
  38. };