emis-app/components/wuli-scan-code/index.vue

125 lines
3.4 KiB
Vue
Raw Normal View History

2023-11-25 16:30:38 +00:00
<template>
<view>
日志======
<view v-for="(v, index) in debugLog" class="">{{index + 1}}: {{v}}</view>
</view>
</template>
<script>
export default {
props: {
customintent: {
type: Object,
default () {
return {
// !!注意: 不同的设备配置项是不同的
scanDownKey: "KEYCODE_SCAN_L_DOWN",
actionKey: 'com.android.receive_scan_action',
codeKey: "data"
}
}
},
},
data() {
return {
debugLog: [],
receiver: '',
filter: '',
codeQueryTag: false,
main: '',
}
},
created() {
this.initScan()
this.startScan();
},
onHide() {
this.stopScan();
},
destroyed() {
this.stopScan();
},
methods: {
initScan() {
let that = this;
that.debugLog.push("执行initScan调用")
that.debugLog.push("actionKey==== " + that.customintent.actionKey)
that.debugLog.push("codeKey==== " + that.customintent.codeKey)
// #ifdef APP
that.debugLog.push('initScan');
that.main = plus.android.runtimeMainActivity(); //获取activity
//var context = plus.android.importClass('android.content.Context'); //上下文
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
that.filter = new IntentFilter();
//下面的addAction内改为自己的广播动作
that.filter.addAction(that.customintent.actionKey);
that.debugLog.push("receive_pda_sn注册成功")
that.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: (context, intent) => {
that.debugLog.push("BroadcastReceiver--onReceive-回调执行")
that.debugLog.push('onReceive');
plus.android.importClass(intent);
//下面的getStringExtra内改为自己的广播标签--有误
let code = intent.getStringExtra(that.customintent.codeKey);
that.debugLog.push("getStringExtra - code:" + code)
that.queryCode(code);
}
});
that.debugLog.push("BroadcastReceiver绑定完成")
// #endif
},
startScan() {
const that = this
// #ifdef APP
this.main.registerReceiver(this.receiver, this.filter);
that.debugLog.push('startScan');
// #endif
},
stopScan() {
const that = this
// #ifdef APP
this.main.unregisterReceiver(this.receiver);
that.debugLog.push('stopScan');
// #endif
},
queryCode: function(code) {
let that = this;
// #ifdef APP
that.debugLog.push('queryCode');
if (that.codeQueryTag) return false;
that.codeQueryTag = true;
setTimeout(function() {
that.codeQueryTag = false;
}, 150);
uni.$emit('wuli-appscan', {
code
})
that.debugLog.push("uni.$emit触发wuli-appscan")
// #endif
},
triggerKeyScan() {
const that = this
that.debugLog.push("执行triggerKeyScan调用")
debugger
// #ifdef APP
//获取Android意图类
let Intent = plus.android.importClass('android.content.Intent');
//实例化意图
let intent = new Intent();
//定义意图,模拟按下L键,L键实际上是打开激光的物理键映射,由厂商提供
const scanDownKey = "com.android.action.keyevent." + this.customintent.scanDownKey
intent.setAction(scanDownKey);
that.debugLog.push("模拟按下L键: " + scanDownKey)
//广播这个意图
that.main.sendBroadcast(intent);
that.debugLog.push("广播按下L键")
// #endif
}
}
}
</script>
<style>
</style>