spring
昨天 6e763136fdf4469143ebbae0b717eb8e9b0ca954
src/App.vue
@@ -1,22 +1,81 @@
<script setup lang="ts">
import { onLaunch, onShow, onHide } from "@dcloudio/uni-app";
import { useThemeStore } from "@/store";
import { ref } from "vue";
// 主题初始化
const themeStore = useThemeStore();
// 全局扫码广播接收器
let main: any = null;
let receiver: any = null;
let filter: any = null;
// 初始化扫码广播接收
const initGlobalScan = () => {
  // #ifdef APP-PLUS
  try {
    main = plus.android.runtimeMainActivity();
    const IntentFilter = plus.android.importClass("android.content.IntentFilter");
    filter = new IntentFilter();
    filter.addAction("com.dwexample.ACTION");
    receiver = plus.android.implements("io.dcloud.feature.internal.reflect.BroadcastReceiver", {
      onReceive: (context: any, intent: any) => {
        console.log("🔍 [全局Scan] onReceive 触发:", context, intent);
        plus.android.importClass(intent);
        const scanResult = intent.getStringExtra("com.motorolasolutions.emdk.datawedge.data_string");
        console.log("🔍 [全局Scan] 扫描结果:", scanResult);
        // 发送到所有可能的事件
        const eventNames = ["scan", "scanIndex", "scanJX", "scanLS"];
        eventNames.forEach((eventName) => {
          uni.$emit(eventName, { code: scanResult });
          console.log(`🔍 [全局Scan] 已发送 ${eventName} 事件`);
        });
      },
    });
    // 注册广播接收器
    main.registerReceiver(receiver, filter);
    console.log("🔍 [全局Scan] 全局扫码广播接收器已启动");
  } catch (error) {
    console.error("🔍 [全局Scan] 初始化失败:", error);
  }
  // #endif
};
// 停止扫码广播接收
const stopGlobalScan = () => {
  // #ifdef APP-PLUS
  try {
    if (main && receiver) {
      main.unregisterReceiver(receiver);
      console.log("🔍 [全局Scan] 全局扫码广播接收器已停止");
    }
  } catch (error) {
    console.error("🔍 [全局Scan] 停止失败:", error);
  }
  // #endif
};
onLaunch(() => {
  console.log("App Launch");
  // 初始化主题
  themeStore.initTheme();
  // 初始化全局扫码广播接收器
  initGlobalScan();
});
onShow(() => {
  console.log("App Show");
  // 应用显示时重新启动广播接收器
  initGlobalScan();
});
onHide(() => {
  console.log("App Hide");
  // 应用隐藏时不停止广播(保持后台接收)
});
</script>