12345678910111213141516171819202122232425262728293031323334353637383940 |
- let main, receiver, filter;
- let _codeQueryTag = false;
- export const initScan = () => {
- let _this = this;
- main = plus.android.runtimeMainActivity(); //获取activity
- var IntentFilter = plus.android.importClass("android.content.IntentFilter");
- filter = new IntentFilter();
- filter.addAction("nlscan.action.SCANNER_RESULT"); // 换你的广播动作
- receiver = plus.android.implements(
- "io.dcloud.feature.internal.reflect.BroadcastReceiver",
- {
- onReceive: function (context, intent) {
- plus.android.importClass(intent);
- let code = intent.getStringExtra("SCAN_BARCODE1"); // 换你的广播标签
- queryCode(code);
- },
- }
- );
- };
- export const startScan = () => {
- main.registerReceiver(receiver, filter);
- };
- export const stopScan = () => {
- main.unregisterReceiver(receiver);
- //解绑uni的全局$on 不然会有bug
- uni.$off("SCAN_CODE");
- };
- export const queryCode = (code) => {
- //防重复
- if (_codeQueryTag) return false;
- _codeQueryTag = true;
- setTimeout(function () {
- _codeQueryTag = false;
- }, 150);
- var id = code;
- //id就是扫码的结果
- uni.$emit("SCAN_CODE", { code: id });
- };
|