zhangwencui
2026-07-09 9f1324fb9e73e5b53ac67542227e63cdc0b505ba
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<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>