feat(scanlistener): 添加PDA扫码模块支持广播和键盘输入

添加qs-scanlistener模块,支持通过广播和键盘输入方式获取扫码结果
更新manifest.json添加abiFilters配置
重构easycode.vue使用新的扫码模块替代原生扫码
This commit is contained in:
砂糖
2025-12-25 15:48:21 +08:00
parent fb5dea4dbe
commit 3650d87a34
9 changed files with 639 additions and 147 deletions

View File

@@ -0,0 +1,2 @@
## 1.0.02022-12-17
create

View File

@@ -0,0 +1,118 @@
<template>
<uni-popup ref="pdaScanPopup" type='center'>
<view>扫码提示</view>
请对准二维码或条形码扫描查看结果
<button @click="close">取消</button>
</uni-popup>
</template>
<script>
import scaninput from './scanInput.js'
scaninput.initScan()
scaninput.startScan()
export default {
name: "scan-listener",
created() {
scaninput.install(this.scanHandle)
// uni.$on('scan_handle', this.scanHandle)
},
beforeDestroy() {
scaninput.uninstall(this.scanHandle)
},
data() {
return {
scaning: false,
timer: undefined,
loading: false,
code: undefined,
}
},
methods: {
onEvent(event) {
// console.log(event.key)
if (event.key != 'Enter' && event.key != 'PrintScreen') { // 拼接输入的值Enter与PrintScreen是物理按钮要排除
this.inputVal = this.inputVal + event.key
}
if (event.key == 'Enter') {
let reg = new RegExp('Shift', 'g') //g代表全部
let reg2 = new RegExp('Unidentified', 'g') //排除Unidentified字符
let inputVal = this.inputVal
inputVal = inputVal.replace(reg, "")
inputVal = inputVal.replace(reg2, "")
inputVal = inputVal.replace(/\s/g, "")
inputVal = inputVal.replace(/\r\n/g, "")
inputVal = inputVal.replace(/\n/g, "")
if (this.inputVal) {
// console.log('键盘监听模式')
this.$emit('scan', this.inputVal)
}
this.inputVal = ''
}
},
scanHandle(code) {
// console.log('广播模式')
this.loading = true;
this.code = code;
this.$emit('scan', code)
},
open() {
return new Promise((resolve, reject) => {
if (!this.scaning) {
this.scaning = true;
this.$refs.pdaScanPopup.open();
this.timer = setInterval(() => {
if (this.loading) {
resolve(this.code)
this.close();
}
}, 100)
} else {
uni.showToast({
title: '请等待当前操作完成'
})
reject()
}
})
},
close() {
this.scaning = false;
this.loading = false;
clearInterval(this.timer);
this.$refs.pdaScanPopup.close();
}
},
data() {
return {
inputVal: '',
};
}
}
</script>
<script module="keyboard" lang="renderjs">
export default {
mounted() {
const onKey = (event) => {
const keys1 = ['type', 'timeStamp']
const keys2 = ['altKey', 'code', 'ctrlKey', 'isComposing', 'key', 'location', 'metaKey', 'repeat', 'shiftKey']
const keys3 = ['char', 'charCode', 'keyCode', 'keyIdentifier', 'keyLocation', 'which']
const data = {}
keys1.concat(keys2, keys3).forEach(key => data[key] = event[key])
this.$ownerInstance.callMethod('onEvent', data)
}
const names = ['keyup'] //'keydown',
names.forEach(name => {
document.addEventListener(name, onKey, false)
})
this.$on('hook:beforeDestroy', () => {
names.forEach(name => {
document.removeEventListener(name, onKey, false)
})
})
}
}
</script>
<style>
</style>

View File

@@ -0,0 +1,56 @@
let main, receiver, filter, _codeQueryTag = false, temp = [], init = false, start = false;
export default {
initScan() {
if(init) return
let _this = this;
main = plus.android.runtimeMainActivity(); //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
// android.intent.ACTION_DECODE_DATA
filter.addAction("com.hc.scan"); // 换你的广播动作你的pda设备里面看
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
//barcode_string
plus.android.importClass(intent);
let code = intent.getStringExtra("Barcode"); // 换你的广播标签你的pda设备里面看
_this.queryCode(code);
}
});
init = true
},
startScan() {
if(!start) {
start = true
main.registerReceiver(receiver, filter);
}
},
stopScan() {
if(start) {
start = false
main.unregisterReceiver(receiver);
}
},
install(fn) {
if(typeof fn == 'function' && !~temp.indexOf(fn)) temp.push(fn)
},
uninstall(fn) {
if(typeof fn == 'function') {
const index = temp.find(i=>i == fn)
if(~index) temp.splice(index, 1)
}
},
queryCode: function(code) {
//防重复
// if (_codeQueryTag) return false;
// _codeQueryTag = true;
// setTimeout(function() {
// _codeQueryTag = false;
// }, 150);
if(temp && temp.length) {
temp[temp.length - 1](code)
}
uni.vibrateShort()
uni.$emit("qs_scanlistener_handle", code);
}
}

View File

@@ -0,0 +1,82 @@
{
"id": "qs-scanlistener",
"displayName": "qs-scanlistener PDA扫码",
"version": "1.0.0",
"description": "PDA扫码 兼容广播和键盘",
"keywords": [
"pda",
"扫码"
],
"repository": "",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"type": "component-vue",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "无",
"data": "无",
"permissions": "无"
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "y",
"aliyun": "y"
},
"client": {
"Vue": {
"vue2": "y",
"vue3": "y"
},
"App": {
"app-vue": "y",
"app-nvue": "n"
},
"H5-mobile": {
"Safari": "n",
"Android Browser": "n",
"微信浏览器(Android)": "n",
"QQ浏览器(Android)": "n"
},
"H5-pc": {
"Chrome": "n",
"IE": "n",
"Edge": "n",
"Firefox": "n",
"Safari": "n"
},
"小程序": {
"微信": "n",
"阿里": "n",
"百度": "n",
"字节跳动": "n",
"QQ": "n",
"钉钉": "n",
"快手": "n",
"飞书": "n",
"京东": "n"
},
"快应用": {
"华为": "n",
"联盟": "n"
}
}
}
}
}

View File

@@ -0,0 +1,20 @@
## qs-scanlistener PDA扫码
## 支持广播和键盘
---
### 广播动作可在main.js设置uni._qs_scanlistener_action = 你的广播动作名称, 默认android.intent.ACTION_DECODE_DATA
### 广播标签可在main.js设置uni._qs_scanlistener_label = 你的广播标签名称, 默认barcode_string
---
### 扫码结果也可以uni.$on('qs_scanlistener_handle', code=>{}) 中获取
---
Template
```html
<qs-scanlistener @scan="scan"></qs-scanlistener>
```
js
```javascript
methods: {
scan(code) {
console.log(code)
}
}
```