zhangwencui
10 天以前 716c7c0d9c55883ed88f7cfe61557d33175cab7a
增加巡检扫码设备验证功能
已修改1个文件
130 ■■■■■ 文件已修改
src/pages/inspectionUpload/index.vue 130 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/pages/inspectionUpload/index.vue
@@ -316,10 +316,11 @@
  };
  // 为指定任务开始扫码(真机)
  const startScanForTask = async task => {
  const startScanForTask = task => {
    try {
      currentScanningTask.value = task;
      uni.scanCode({
        scanType: ["qrCode", "barCode"],
        success: res => {
          handleScanSuccess(res);
        },
@@ -341,48 +342,109 @@
  };
  // 扫码成功处理:校验后打开上传弹窗
  const handleScanSuccess = result => {
    try {
      // 解析二维码数据,提取deviceId
      let deviceId = "";
  const getScannedDeviceId = scanResult => {
    if (!scanResult || typeof scanResult !== "string") return "";
      if (result?.result && typeof result.result === "string") {
        if (result.result.includes("deviceId=")) {
          const match = result.result.match(/deviceId=(\d+)/);
          if (match && match[1]) deviceId = match[1];
        } else {
          try {
            const qrData = JSON.parse(result.result);
            deviceId = qrData.deviceId || qrData.qrCodeId || "";
          } catch (e) {
            deviceId = result.result;
          }
        }
      }
    const value = scanResult.trim();
    const urlMatch = value.match(/[?&]deviceId=([^&#]+)/i);
    if (urlMatch?.[1]) {
      return decodeURIComponent(urlMatch[1]).trim();
    }
    try {
      const qrData = JSON.parse(value);
      return String(
        qrData?.deviceId ??
          qrData?.deviceLedgerId ??
          qrData?.data?.deviceId ??
          ""
      ).trim();
    } catch (e) {
      // 兼容只存放设备ID的二维码/条形码
      return /^\d+$/.test(value) ? value : "";
    }
  };
  const getTaskDeviceIdentity = task => {
    const id =
      task?.deviceLedgerId ??
      task?.deviceId ??
      task?.ledgerId ??
      task?.equipmentId ??
      task?.deviceLedger?.id ??
      task?.device?.id;
    const code =
      task?.deviceCode ??
      task?.equipmentCode ??
      task?.deviceLedger?.deviceCode ??
      task?.device?.deviceCode;
    const name =
      task?.deviceName ??
      task?.equipmentName ??
      task?.deviceLedger?.deviceName ??
      task?.device?.deviceName ??
      task?.taskName;
    return { id, code, name };
  };
  const isTaskDevice = (task, device, scannedDeviceId) => {
    const expected = getTaskDeviceIdentity(task);
    if (
      expected.id !== undefined &&
      expected.id !== null &&
      expected.id !== ""
    ) {
      return String(expected.id) === String(device?.id ?? scannedDeviceId);
    }
    if (expected.code && device?.deviceCode) {
      return String(expected.code).trim() === String(device.deviceCode).trim();
    }
    if (expected.name && device?.deviceName) {
      return String(expected.name).trim() === String(device.deviceName).trim();
    }
    return false;
  };
  const handleScanSuccess = async result => {
    try {
      const deviceId = getScannedDeviceId(result?.result);
      if (!deviceId) {
        uni.showToast({ title: "未识别到设备ID", icon: "error" });
        uni.showToast({ title: "未识别到设备码", icon: "none" });
        return;
      }
      const currentTaskId =
        currentScanningTask.value?.taskId || currentScanningTask.value?.id;
      if (!currentTaskId) {
        uni.showToast({ title: "任务信息缺失", icon: "error" });
      const task = currentScanningTask.value;
      if (!task) {
        uni.showToast({ title: "巡检任务信息缺失", icon: "none" });
        return;
      }
      if (deviceId === currentTaskId.toString()) {
        uni.showToast({ title: "识别成功", icon: "success" });
        openUploadDialog(currentScanningTask.value);
      } else {
        uni.showToast({ title: "请扫描正确的设备", icon: "error" });
      uni.showLoading({ title: "正在验证设备...", mask: true });
      const { code, data } = await getLedgerById(deviceId);
      uni.hideLoading();
      if (code !== 200 || !data) {
        uni.showToast({ title: "未找到该设备", icon: "none" });
        return;
      }
      if (!isTaskDevice(task, data, deviceId)) {
        uni.showToast({ title: "设备不匹配,请扫描当前巡检设备", icon: "none" });
        return;
      }
      uni.showToast({ title: "设备验证成功", icon: "success" });
      setTimeout(() => openUploadDialog(task), 300);
    } catch (error) {
      uni.hideLoading();
      console.error("扫码结果处理失败:", error);
      uni.showToast({
        title: error?.message || "数据解析失败",
        icon: "error",
        title: error?.message || "设备验证失败",
        icon: "none",
      });
    }
  };
@@ -397,9 +459,9 @@
  };
  // 图片上传(可选择图片上传或者是相机拍照)
  const startUploadForTask = async (task, type) => {
    // 打开上传页面
    openUploadDialog(task);
  const startUploadForTask = task => {
    // 上传前必须先扫码验证当前设备
    startScanForTask(task);
  };
  // 查看附件 - 跳转到附件页面
@@ -765,4 +827,4 @@
    max-height: 60vh;
    display: block;
  }
</style>
</style>