You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.8 KiB
Vue

4 weeks ago
<template>
<view class="content"></view>
</template>
<script>
// #ifdef APP-PLUS
var main, receiver, filter;
var _codeQueryTag = false;
export default {
data() {
return {
scanCode: ''
};
},
created: function(option) {
this.initScan();
this.startScan();
},
onHide: function() {
this.stopScan();
},
destroyed: function() {
/*页面退出时一定要卸载监听,否则下次进来时会重复造成扫一次出2个以上的结果*/
this.stopScan();
},
methods: {
initScan() {
let _this = this;
main = plus.android.runtimeMainActivity(); //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
filter.addAction('com.android.server.scannerservice.broadcast'); // 换你的广播动作
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
plus.android.importClass(intent);
let code = intent.getStringExtra('scannerdata'); // 换你的广播标签
_this.queryCode(code);
}
});
},
startScan() {
main.registerReceiver(receiver, filter);
},
stopScan() {
main.unregisterReceiver(receiver);
},
queryCode: function(code) {
//防重复
if (_codeQueryTag) return false;
_codeQueryTag = true;
setTimeout(function() {
_codeQueryTag = false;
}, 150);
var id = code;
uni.$emit('scancodedate', { code: id });
},
},
};
// #endif
</script>