This commit is contained in:
aihe 2023-12-23 15:52:34 +08:00
parent 31841eeb2a
commit bea28f9942
2 changed files with 94 additions and 28 deletions

View File

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

View File

@ -0,0 +1,69 @@
<template>
<u-input :placeholder="placeholder" :fontSize="fontSize" :modelValue="value"
:custom-style="`border-radius: 10rpx;padding: 8rpx 12rpx;${customStyle};`"
@change="handleChange"
>
<template v-slot:prefix>
<u-icon @click="handleScan" name="scan" :size="fontSize*1.5"></u-icon>
</template>
</u-input>
<tpx-physics-scan onChange="handleChange"></tpx-physics-scan>
</template>
<script>
import { scanCode } from "@/common/untool"
export default {
props: {
value: {
default () {
return ''
}
},
placeholder:{
default () {
return '请输入或扫码'
}
},
fontSize: {
default () {
return 26
}
},
customStyle:{
default () {
return ''
}
},
placeholderStyle:{
default () {
return ''
}
},
},
data() {
},
created() {
},
onHide() {
},
destroyed() {},
methods: {
async handleScan() {
const res = await scanCode()
this.handleChange(res)
},
handleChange(val){
this.$emit('change', val)
this.$emit('update:value', val)
}
}
}
</script>
<style scoped lang="scss">
</style>