emis-sapp/components/tpx-physics-scan/tpx-physics-scan.vue
2023-12-23 15:52:34 +08:00

122 lines
3.2 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<template>
<view></view>
</template>
<script>
export default {
props: {
customintent: {
default () {
return {
// !!注意: 不同的设备配置项是不同的
scanDownKey: "KEYCODE_SCAN_L_DOWN",
actionKey: 'com.android.receive_scan_action',
codeKey: "data"
}
}
},
value: {
default () {
return ''
}
},
},
data() {
return {
receiver: '',
filter: '',
codeQueryTag: false,
main: '',
}
},
created() {
this.initScan()
this.startScan();
},
onHide() {
// this.stopScan();
},
unmounted() {
this.stopScan();
},
methods: {
initScan() {
let that = this;
console.log("执行initScan调用")
console.log("actionKey==== " + that.customintent.actionKey)
console.log("codeKey==== " + that.customintent.codeKey)
// #ifdef APP
console.log('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);
console.log("receive_pda_sn注册成功")
that.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: (context, intent) => {
console.log("BroadcastReceiver--onReceive-回调执行")
console.log('onReceive');
plus.android.importClass(intent);
//下面的getStringExtra内改为自己的广播标签--有误
let code = intent.getStringExtra(that.customintent.codeKey);
console.log("getStringExtra - code:" + code)
that.queryCode(code);
}
});
console.log("BroadcastReceiver绑定完成")
// #endif
},
startScan() {
const that = this
// #ifdef APP
this.main.registerReceiver(this.receiver, this.filter);
console.log('startScan');
// #endif
},
stopScan() {
console.log('stopScan');
const that = this
// #ifdef APP
this.main.unregisterReceiver(this.receiver);
// #endif
},
queryCode: function(code) {
let that = this;
// #ifdef APP
console.log('queryCode');
if (that.codeQueryTag) return false;
that.codeQueryTag = true;
setTimeout(function() {
that.codeQueryTag = false;
}, 150);
that.$emit('onChange', code)
console.log("uni.$emit触发wuli-appscan")
// #endif
},
triggerKeyScan() {
const that = this
console.log("执行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);
console.log("模拟按下L键: " + scanDownKey)
//广播这个意图
that.main.sendBroadcast(intent);
console.log("广播按下L键")
// #endif
}
}
}
</script>
<style>
</style>