feat(scanner): 多厂商PDA扫码器兼容方案及扫码功能重构
- 采用策略模式及配置表驱动方式支持多品牌扫码器广播注册 - 实现扫码广播的统一注册和注销管理,支持并发多个扫码广播接收器 - 重构扫码处理逻辑,统一处理扫码结果,支持条码和二维码识别 - 移除原有单一品牌扫码注册,整合商米、新大陆等扫码头支持 - 调整扫码相关页面代码,集成scanMixin,实现扫码广播注册和注销自动化 - 优化扫码结果处理,支持删除模式及整缸扫描状态区分 - 修正多处界面文本格式与代码风格,提高代码一致性和可维护性 - 升级manifest版本号至1.1.3,标记此功能更新
This commit is contained in:
+177
-133
@@ -1,143 +1,175 @@
|
||||
// common/scanMixin.js
|
||||
// 多厂商PDA扫码器兼容方案 - 策略模式 + 配置表驱动
|
||||
export default {
|
||||
data() {
|
||||
return {
|
||||
scanReceiver: null,
|
||||
isPageActive: false
|
||||
scanReceivers: [], // 支持多个接收器
|
||||
isPageActive: false,
|
||||
registeredBrands: [] // 已注册的品牌
|
||||
}
|
||||
},
|
||||
|
||||
methods: {
|
||||
// 注册扫码广播(兼容多种设备)
|
||||
registerScanBroadcast(scanCallback) {
|
||||
try {
|
||||
// 先尝试注册商米扫码头
|
||||
this.registerSunmiBroadcast(scanCallback)
|
||||
.then(() => {
|
||||
console.log('商米扫码头注册成功');
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log('商米扫码头注册失败,尝试新大陆扫码头:', error);
|
||||
// 如果商米扫码头注册失败,尝试新大陆扫码头
|
||||
this.registerNewlandBroadcast(scanCallback);
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("注册扫码广播失败:", error);
|
||||
return false;
|
||||
}
|
||||
// 扫码器配置表 - 方便扩展新厂商
|
||||
getScannerConfigs() {
|
||||
return [
|
||||
{
|
||||
brand: 'newland', // 东集
|
||||
name: '东集',
|
||||
action: 'com.android.server.scannerservice.broadcast',
|
||||
dataKey: 'scannerdata',
|
||||
needSetup: true,
|
||||
setupAction: 'com.android.scanner.service_settings',
|
||||
setupParams: {
|
||||
action_barcode_broadcast: 'com.android.server.scannerservice.broadcast',
|
||||
key_barcode_broadcast: 'scannerdata',
|
||||
endchar: 'ENTER'
|
||||
}
|
||||
},
|
||||
{
|
||||
brand: 'sunmi', // 商米
|
||||
name: '商米',
|
||||
action: 'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED',
|
||||
dataKey: 'data',
|
||||
needSetup: false
|
||||
},
|
||||
// {
|
||||
// brand: 'honeywell', // 霍尼韦尔
|
||||
// name: '霍尼韦尔',
|
||||
// action: 'com.honeywell.decode.intent.action.EDIT_DATA',
|
||||
// dataKey: 'data',
|
||||
// needSetup: false
|
||||
// },
|
||||
// {
|
||||
// brand: 'zebra', // 斑马
|
||||
// name: '斑马',
|
||||
// action: 'com.symbol.datawedge.api.RESULT_ACTION',
|
||||
// dataKey: 'com.symbol.datawedge.data_string',
|
||||
// needSetup: false
|
||||
// },
|
||||
// {
|
||||
// brand: 'idata', // 盈达聚力
|
||||
// name: '盈达聚力',
|
||||
// action: 'android.intent.action.SCANRESULT',
|
||||
// dataKey: 'value',
|
||||
// needSetup: false
|
||||
// },
|
||||
// {
|
||||
// brand: 'urovo', // 优博讯
|
||||
// name: '优博讯',
|
||||
// action: 'android.intent.ACTION_DECODE_DATA',
|
||||
// dataKey: 'barcode_string',
|
||||
// needSetup: false
|
||||
// }
|
||||
];
|
||||
},
|
||||
|
||||
// 注册商米扫码头广播接收器
|
||||
registerSunmiBroadcast(scanCallback) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 注册所有扫码广播(推荐方式:同时注册所有厂商)
|
||||
registerScanBroadcast(scanCallback) {
|
||||
// #ifdef APP-PLUS
|
||||
const configs = this.getScannerConfigs();
|
||||
|
||||
configs.forEach(config => {
|
||||
try {
|
||||
const main = plus.android.runtimeMainActivity();
|
||||
const IntentFilter = plus.android.importClass("android.content.IntentFilter");
|
||||
|
||||
const filter = new IntentFilter();
|
||||
filter.addAction("com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED");
|
||||
|
||||
const self = this;
|
||||
const receiver = plus.android.implements(
|
||||
"io.dcloud.feature.internal.reflect.BroadcastReceiver",
|
||||
{
|
||||
onReceive: (context, intent) => {
|
||||
console.log('商米扫码广播接收,isPageActive:', self.isPageActive);
|
||||
// 只有当页面活动时才处理广播
|
||||
if (!self.isPageActive) return;
|
||||
|
||||
try {
|
||||
// 导入 Intent 类以使用其方法
|
||||
const Intent = plus.android.importClass("android.content.Intent");
|
||||
|
||||
const scanResult = intent.getStringExtra("data");
|
||||
const sourceBytes = intent.getByteArrayExtra("source_byte");
|
||||
|
||||
console.log('商米扫码结果:', scanResult, sourceBytes);
|
||||
|
||||
if (scanResult) {
|
||||
self.handleScanResult(scanResult, scanCallback);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('处理商米广播数据时出错:', error);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
// 注册广播接收器
|
||||
main.registerReceiver(receiver, filter);
|
||||
this.scanReceiver = receiver;
|
||||
console.log('商米扫码广播接收器注册成功');
|
||||
resolve();
|
||||
this.registerSingleBroadcast(config, scanCallback);
|
||||
this.registeredBrands.push(config.brand);
|
||||
console.log(`${config.name}扫码广播注册成功`);
|
||||
} catch (error) {
|
||||
console.error('注册商米广播接收器失败:', error);
|
||||
reject(error);
|
||||
console.log(`${config.name}扫码广播注册失败:`, error);
|
||||
}
|
||||
});
|
||||
|
||||
if (this.registeredBrands.length > 0) {
|
||||
console.log('扫码广播注册完成,已注册厂商:', this.registeredBrands.join(', '));
|
||||
return true;
|
||||
}
|
||||
|
||||
console.error('所有扫码广播注册均失败');
|
||||
return false;
|
||||
// #endif
|
||||
},
|
||||
|
||||
// 注册新大陆扫码头广播接收器
|
||||
registerNewlandBroadcast(scanCallback) {
|
||||
try {
|
||||
const main = plus.android.runtimeMainActivity();
|
||||
|
||||
// 先配置扫码枪广播设置
|
||||
try {
|
||||
const Intent = plus.android.importClass("android.content.Intent");
|
||||
const intent = new Intent("com.android.scanner.service_settings");
|
||||
intent.putExtra(
|
||||
"action_barcode_broadcast",
|
||||
"com.android.server.scannerservice.broadcast"
|
||||
);
|
||||
intent.putExtra("key_barcode_broadcast", "scannerdata");
|
||||
main.sendBroadcast(intent);
|
||||
console.log('新大陆扫码枪广播配置完成');
|
||||
} catch (error) {
|
||||
console.error("配置新大陆扫码枪广播失败:", error);
|
||||
}
|
||||
|
||||
// 注册广播接收器
|
||||
const IntentFilter = plus.android.importClass(
|
||||
"android.content.IntentFilter"
|
||||
);
|
||||
const filter = new IntentFilter();
|
||||
filter.addAction("com.android.server.scannerservice.broadcast");
|
||||
console.log("添加新大陆广播action完成");
|
||||
|
||||
const self = this;
|
||||
const receiver = plus.android.implements(
|
||||
"io.dcloud.feature.internal.reflect.BroadcastReceiver",
|
||||
{
|
||||
onReceive: (context, intent) => {
|
||||
console.log('新大陆扫码广播接收,isPageActive:', self.isPageActive);
|
||||
// 只有当页面活动时才处理广播
|
||||
if (!self.isPageActive) return;
|
||||
|
||||
try {
|
||||
// 导入 Intent 类以使用其方法
|
||||
const Intent = plus.android.importClass("android.content.Intent");
|
||||
|
||||
const scanResult = intent.getStringExtra("scannerdata");
|
||||
console.log("新大陆扫码结果:", scanResult);
|
||||
if (scanResult) {
|
||||
self.handleScanResult(scanResult, scanCallback);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("处理新大陆广播数据时出错:", error);
|
||||
}
|
||||
},
|
||||
}
|
||||
);
|
||||
|
||||
// 注册广播接收器
|
||||
main.registerReceiver(receiver, filter);
|
||||
this.scanReceiver = receiver;
|
||||
console.log("新大陆扫码广播注册成功,等待扫码...");
|
||||
} catch (error) {
|
||||
console.error("注册新大陆扫码广播失败:", error);
|
||||
throw error;
|
||||
// 注册指定厂商的扫码广播
|
||||
registerScanBroadcastByBrand(brand, scanCallback) {
|
||||
// #ifdef APP-PLUS
|
||||
const configs = this.getScannerConfigs();
|
||||
const config = configs.find(c => c.brand === brand);
|
||||
|
||||
if (!config) {
|
||||
console.error(`未找到厂商配置: ${brand}`);
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
this.registerSingleBroadcast(config, scanCallback);
|
||||
this.registeredBrands.push(config.brand);
|
||||
console.log(`${config.name}扫码广播注册成功`);
|
||||
return true;
|
||||
} catch (error) {
|
||||
console.error(`${config.name}扫码广播注册失败:`, error);
|
||||
return false;
|
||||
}
|
||||
// #endif
|
||||
},
|
||||
|
||||
// 单个广播注册的核心方法
|
||||
registerSingleBroadcast(config, scanCallback) {
|
||||
const main = plus.android.runtimeMainActivity();
|
||||
|
||||
// 如果需要预配置(如东集)
|
||||
if (config.needSetup && config.setupAction) {
|
||||
try {
|
||||
const Intent = plus.android.importClass('android.content.Intent');
|
||||
const setupIntent = new Intent(config.setupAction);
|
||||
|
||||
Object.keys(config.setupParams || {}).forEach(key => {
|
||||
setupIntent.putExtra(key, config.setupParams[key]);
|
||||
});
|
||||
|
||||
main.sendBroadcast(setupIntent);
|
||||
console.log(`${config.name}扫码配置完成`);
|
||||
} catch (error) {
|
||||
console.error(`${config.name}扫码配置失败:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// 注册广播接收器
|
||||
const IntentFilter = plus.android.importClass('android.content.IntentFilter');
|
||||
const filter = new IntentFilter();
|
||||
filter.addAction(config.action);
|
||||
|
||||
const self = this;
|
||||
const receiver = plus.android.implements(
|
||||
'io.dcloud.feature.internal.reflect.BroadcastReceiver',
|
||||
{
|
||||
onReceive: (context, intent) => {
|
||||
console.log(`${config.name}扫码广播接收,isPageActive:`, self.isPageActive);
|
||||
|
||||
if (!self.isPageActive) return;
|
||||
|
||||
try {
|
||||
plus.android.importClass('android.content.Intent');
|
||||
const scanResult = intent.getStringExtra(config.dataKey);
|
||||
|
||||
console.log(`${config.name}扫码结果:`, scanResult);
|
||||
|
||||
if (scanResult) {
|
||||
self.handleScanResult(scanResult, scanCallback);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`处理${config.name}广播数据出错:`, error);
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
|
||||
main.registerReceiver(receiver, filter);
|
||||
|
||||
// 保存接收器引用,用于后续注销
|
||||
this.scanReceivers.push({
|
||||
brand: config.brand,
|
||||
receiver: receiver
|
||||
});
|
||||
},
|
||||
|
||||
// 处理扫码结果的统一方法
|
||||
@@ -156,18 +188,30 @@ export default {
|
||||
}
|
||||
},
|
||||
|
||||
// 取消扫码广播监听
|
||||
// 取消所有扫码广播监听
|
||||
unregisterScanBroadcast() {
|
||||
if (this.scanReceiver) {
|
||||
try {
|
||||
const main = plus.android.runtimeMainActivity();
|
||||
main.unregisterReceiver(this.scanReceiver);
|
||||
this.scanReceiver = null;
|
||||
console.log("扫码广播注销成功");
|
||||
} catch (err) {
|
||||
console.error("注销扫码广播失败:", err);
|
||||
}
|
||||
// #ifdef APP-PLUS
|
||||
if (this.scanReceivers.length === 0) return;
|
||||
|
||||
try {
|
||||
const main = plus.android.runtimeMainActivity();
|
||||
|
||||
this.scanReceivers.forEach(item => {
|
||||
try {
|
||||
main.unregisterReceiver(item.receiver);
|
||||
console.log(`${item.brand}扫码广播注销成功`);
|
||||
} catch (err) {
|
||||
console.error(`${item.brand}扫码广播注销失败:`, err);
|
||||
}
|
||||
});
|
||||
|
||||
this.scanReceivers = [];
|
||||
this.registeredBrands = [];
|
||||
console.log('所有扫码广播注销完成');
|
||||
} catch (err) {
|
||||
console.error('注销扫码广播失败:', err);
|
||||
}
|
||||
// #endif
|
||||
},
|
||||
|
||||
// 通用的商品扫描处理方法
|
||||
|
||||
Reference in New Issue
Block a user