zhangwencui
2026-07-14 10ae883d6368fb8fc3f95620f610c3aaf6c6a3cd
src/pages/equipmentManagement/upkeep/add.vue
@@ -85,8 +85,9 @@
                        @confirm="onDateConfirm"
                        @cancel="showDate = false"
                        mode="date" />
    <PdaScan :active="pdaScanActive"
             eventName="scan_upkeep_device" />
    <ScanWaitMask :show="scanMaskShow"
                  :status="scanMaskStatus" />
    <PdaScan />
  </view>
</template>
@@ -95,6 +96,7 @@
  import { onShow } from "@dcloudio/uni-app";
  import PageHeader from "@/components/PageHeader.vue";
  import CommonUpload from "@/components/CommonUpload.vue";
  import ScanWaitMask from "@/components/ScanWaitMask.vue";
  import PdaScan from "@/components/pda-scan/pda-scan.vue";
  import { getDeviceLedger } from "@/api/equipmentManagement/ledger";
  import {
@@ -143,8 +145,51 @@
  // 扫码相关状态
  const isScanning = ref(false);
  const scanTimer = ref(null);
  const pdaScanActive = ref(false);
  const pdaScanTimer = ref(null);
  const pendingDeviceScan = ref(false);
  let pendingDeviceScanTimer = null;
  const scanMaskShow = ref(false);
  const scanMaskStatus = ref("waiting");
  const delay = ms => new Promise(resolve => setTimeout(resolve, ms));
  const canUsePdaBroadcast = () => {
    try {
      if (typeof plus === "undefined") return false;
      if (plus?.os?.name !== "Android") return false;
      if (!plus?.android) return false;
      const sys = uni.getSystemInfoSync?.() || {};
      const brand = String(sys?.brand || "").toUpperCase();
      const model = String(sys?.model || "").toUpperCase();
      return (
        brand.includes("DROI") ||
        model.includes("MV-IDP5204") ||
        model.includes("IDP")
      );
    } catch (e) {
      return false;
    }
  };
  onShow(() => {
    uni.$off("scan");
    uni.$on("scan", data => {
      console.log("onscan");
      console.log("扫码结果:", data.code);
      if (!pendingDeviceScan.value) return;
      pendingDeviceScan.value = false;
      scanMaskStatus.value = "done";
      if (pendingDeviceScanTimer) {
        clearTimeout(pendingDeviceScanTimer);
        pendingDeviceScanTimer = null;
      }
      delay(350).then(() => {
        scanMaskShow.value = false;
        processScanResult(data.code);
      });
    });
    getPageParams();
  });
  // 表单验证规则
  const formRules = {
@@ -210,62 +255,47 @@
  // 扫描二维码功能
  const startScan = () => {
    if (isScanning.value) {
      showToast("正在扫描中,请稍候...");
      return;
    pendingDeviceScan.value = true;
    scanMaskStatus.value = "waiting";
    scanMaskShow.value = true;
    if (pendingDeviceScanTimer) {
      clearTimeout(pendingDeviceScanTimer);
      pendingDeviceScanTimer = null;
    }
    const canUsePdaBroadcast = () => {
      try {
        if (typeof plus === "undefined") return false;
        if (plus?.os?.name !== "Android") return false;
        if (!plus?.android) return false;
        const sys = uni.getSystemInfoSync?.() || {};
        const brand = String(sys?.brand || "").toUpperCase();
        const model = String(sys?.model || "").toUpperCase();
        return (
          brand.includes("DROI") ||
          model.includes("MV-IDP5204") ||
          model.includes("IDP")
        );
      } catch (e) {
        return false;
      }
    };
    if (canUsePdaBroadcast()) {
      if (pdaScanTimer.value) {
        clearTimeout(pdaScanTimer.value);
        pdaScanTimer.value = null;
      }
      uni.$off("scan_upkeep_device");
      uni.$on("scan_upkeep_device", data => {
        pdaScanActive.value = false;
        uni.$off("scan_upkeep_device");
        if (pdaScanTimer.value) {
          clearTimeout(pdaScanTimer.value);
          pdaScanTimer.value = null;
        }
        handleScanResult(data?.code);
    pendingDeviceScanTimer = setTimeout(() => {
      pendingDeviceScan.value = false;
      pendingDeviceScanTimer = null;
      scanMaskStatus.value = "timeout";
      delay(900).then(() => {
        scanMaskShow.value = false;
      });
      pdaScanActive.value = true;
      uni.showToast({ title: "请使用PDA扫码", icon: "none" });
      pdaScanTimer.value = setTimeout(() => {
        pdaScanActive.value = false;
        uni.$off("scan_upkeep_device");
        pdaScanTimer.value = null;
      }, 20000);
      return;
    }
    }, 20000);
    if (canUsePdaBroadcast()) return;
    uni.scanCode({
      scanType: ["qrCode", "barCode"],
      success: res => {
        handleScanResult(res.result);
        pendingDeviceScan.value = false;
        scanMaskStatus.value = "done";
        if (pendingDeviceScanTimer) {
          clearTimeout(pendingDeviceScanTimer);
          pendingDeviceScanTimer = null;
        }
        delay(350).then(() => {
          scanMaskShow.value = false;
          handleScanResult(res.result);
        });
      },
      fail: err => {
        pendingDeviceScan.value = false;
        scanMaskStatus.value = "timeout";
        delay(900).then(() => {
          scanMaskShow.value = false;
        });
        if (pendingDeviceScanTimer) {
          clearTimeout(pendingDeviceScanTimer);
          pendingDeviceScanTimer = null;
        }
        console.error("扫码失败:", err);
        showToast("扫码失败,请重试");
      },
@@ -342,11 +372,6 @@
    showDate.value = false;
  };
  onShow(() => {
    // 页面显示时获取参数
    getPageParams();
  });
  onMounted(() => {
    // 页面加载时获取设备列表和参数
    loadDeviceName();
@@ -358,10 +383,12 @@
    if (scanTimer.value) {
      clearTimeout(scanTimer.value);
    }
    if (pdaScanTimer.value) {
      clearTimeout(pdaScanTimer.value);
    if (pendingDeviceScanTimer) {
      clearTimeout(pendingDeviceScanTimer);
      pendingDeviceScanTimer = null;
    }
    uni.$off("scan_upkeep_device");
    scanMaskShow.value = false;
    uni.$off("scan");
  });
  // 提交表单