<template>
|
<view />
|
</template>
|
|
<script>
|
let mainActivity;
|
let receiver;
|
let intentFilter;
|
let isRegistered = false;
|
let lock = false;
|
|
export default {
|
name: "PdaScan",
|
props: {
|
active: { type: Boolean, default: true },
|
eventName: { type: String, default: "scan" },
|
action: { type: String, default: "" },
|
extraKey: { type: String, default: "" },
|
debounceMs: { type: Number, default: 150 },
|
},
|
mounted() {
|
this.init();
|
if (this.active) this.start();
|
},
|
beforeUnmount() {
|
this.stop();
|
},
|
watch: {
|
active(val) {
|
if (val) {
|
this.init();
|
this.start();
|
} else {
|
this.stop();
|
}
|
},
|
},
|
methods: {
|
canUseAndroidBroadcast() {
|
try {
|
if (typeof plus === "undefined") return false;
|
if (!plus.os || plus.os.name !== "Android") return false;
|
if (!plus.android) return false;
|
return true;
|
} catch (e) {
|
return false;
|
}
|
},
|
resolveBroadcastConfig(sys) {
|
if (this.action && this.extraKey) {
|
return { action: this.action, extraKey: this.extraKey };
|
}
|
|
const brand = String(sys?.brand || "").toUpperCase();
|
const model = String(sys?.model || "").toUpperCase();
|
|
if (
|
brand.includes("DROI") ||
|
model.includes("MV-IDP5204") ||
|
model.includes("IDP")
|
) {
|
return { action: "com.service.scanner.data", extraKey: "ScanCode" };
|
}
|
|
return { action: "", extraKey: "" };
|
},
|
init() {
|
if (!this.canUseAndroidBroadcast()) return;
|
if (mainActivity && receiver && intentFilter) return;
|
|
let sys = {};
|
try {
|
sys = uni.getSystemInfoSync() || {};
|
} catch (e) {
|
sys = {};
|
}
|
|
const cfg = this.resolveBroadcastConfig(sys);
|
if (!cfg.action || !cfg.extraKey) return;
|
this.initAndroidReceiver(cfg.action, cfg.extraKey);
|
},
|
initAndroidReceiver(action, extraKey) {
|
const _this = this;
|
mainActivity = plus.android.runtimeMainActivity();
|
const IntentFilter = plus.android.importClass(
|
"android.content.IntentFilter"
|
);
|
intentFilter = new IntentFilter();
|
intentFilter.addAction(action);
|
|
receiver = plus.android.implements(
|
"io.dcloud.feature.internal.reflect.BroadcastReceiver",
|
{
|
onReceive(context, intent) {
|
plus.android.importClass(intent);
|
const code = intent.getStringExtra(extraKey);
|
_this.emitCode(code);
|
},
|
}
|
);
|
},
|
start() {
|
if (!this.canUseAndroidBroadcast()) return;
|
if (!mainActivity || !receiver || !intentFilter) return;
|
if (isRegistered) return;
|
try {
|
mainActivity.registerReceiver(receiver, intentFilter);
|
isRegistered = true;
|
} catch (e) {}
|
},
|
stop() {
|
if (!this.canUseAndroidBroadcast()) return;
|
if (!mainActivity || !receiver) return;
|
if (!isRegistered) return;
|
try {
|
mainActivity.unregisterReceiver(receiver);
|
} catch (e) {}
|
isRegistered = false;
|
},
|
emitCode(code) {
|
const value = code == null ? "" : String(code);
|
const text = value.replace(/\n/g, "").trim();
|
if (!text) return;
|
|
if (lock) return;
|
lock = true;
|
setTimeout(() => {
|
lock = false;
|
}, this.debounceMs);
|
|
const payload = { code: text };
|
uni.$emit(this.eventName, payload);
|
this.$emit("scan", payload);
|
},
|
},
|
};
|
</script>
|