yyb
5 天以前 883ab99ab021be1adc6dc5689368f899ea436e54
src/pages/routingInspection/detail/indexJX.vue
@@ -138,6 +138,7 @@
        style="padding-bottom: 10px"
      >
        <wd-form-item
          prop="structureItemsGroup"
          :label="formatValue(item.structureName)"
          label-width="400rpx"
          style="color: red"
@@ -184,6 +185,7 @@
          :label="formatValue(item.twistName)"
          label-width="400rpx"
          style="color: red"
          prop="inspectTwistGroup"
          required
        ></wd-form-item>
        <wd-form-item label="绞向" prop="direction" required>
@@ -290,40 +292,15 @@
      <view style="margin: 10rpx">
        <text class="title">{{ "附件" }}</text>
      </view>
      <view class="attachment-grid">
        <!-- 已上传附件(包含新上传的) -->
        <wd-col v-for="(file, index) in allFiles" :key="file.id || index" class="attachment-item">
          <wd-img :width="80" :height="80" :src="file.url" @click="previewImage(file.url)">
            <template #error>
              <view class="error-wrap">加载失败</view>
            </template>
            <template #loading>
              <view class="loading-wrap">
                <wd-loading />
              </view>
            </template>
          </wd-img>
          <!-- 编辑模式下显示删除按钮 -->
          <!-- <wd-icon
            v-if="isEdit"
            name="close-circle"
            class="delete-icon"
            @click.stop="deleteFile(file.id || index)"
          ></wd-icon> -->
        </wd-col>
        <!-- 上传按钮(仅编辑模式显示) -->
        <wd-col v-if="isEdit" class="attachment-item upload-btn">
          <wd-upload
            :multiple="true"
            :max-count="5"
            :before-upload="beforeUpload"
            @success="handleUploadSuccess"
            @fail="handleUploadFail"
          >
            <view class="upload-icon">+</view>
          </wd-upload>
        </wd-col>
      </view>
      <wd-col :span="24">
        <AttachmentUpload
          :detailData="detailData"
          :isEdit="isEdit"
          :deviceType="paramsType"
          ref="attachmentRef"
          v-if="detailDataLoaded"
        />
      </wd-col>
    </wd-row>
    <wd-popup v-model="show" custom-style="border-radius:32rpx;" @close="handleClose">
@@ -342,8 +319,9 @@
import RoutingInspectionApi from "@/api/routingInspection/routingInspection";
import Scan from "@/components/scan/index.vue";
import { useToast } from "wot-design-uni";
import AttachmentUpload from "../upload.vue";
// 核心状态
const paramsType = ref("");
const paramsId = ref("");
const recordData = ref<any>({ structureInfo: { files: [], structureRecordResult: {} } });
const show = ref(false);
@@ -353,8 +331,10 @@
const deviceUid = ref("");
const scanRef = ref();
const toast = useToast();
const attachmentRef = ref<any>(null);
const detailData = reactive<any>({});
const detailDataLoaded = ref(false);
// 表单临时数据(编辑模式绑定)
const formData = reactive({
  twistDiameter: "", // 绞制外径
  structureFormula: "", // 成品结构
@@ -365,7 +345,6 @@
  sampleComplete: "", // 样品是否齐全
});
// 选项数据
const twistDirectionOptions = [
  { label: "左向", value: "左向" },
  { label: "右向", value: "右向" },
@@ -387,47 +366,40 @@
  { label: "否", value: "否" },
];
// 合并原有附件和新上传附件
const allFiles = computed(() => {
  return [...(recordData.value.structureInfo?.files || []), ...tempFiles.value];
});
// 初始化表单数据(接口数据同步到表单)
const initFormData = () => {
  const structureResult = recordData.value.structureInfo?.structureRecordResult || {};
  const inspectionResult = recordData.value.inspectionResult || {};
  // 基础可编辑字段
  formData.twistDiameter = inspectionResult.twistDiameter || "";
  formData.structureFormula = structureResult.inspectStructure?.structureFormula || "";
  formData.sampleComplete = inspectionResult.sampleComplete || "";
  formData.conclusion = structureResult.conclusion || "";
  // 修复产品外观和结论的数据格式
  formData.productAppearance = Array.isArray(structureResult.productAppearance)
    ? structureResult.productAppearance
    : structureResult.productAppearance
    ? [structureResult.productAppearance]
    : [];
  // 循环类字段(深拷贝避免原数据污染)
  formData.structureItems = JSON.parse(
    JSON.stringify(structureResult.inspectStructure?.structureItems || [])
  );
  formData.inspectTwist = JSON.parse(JSON.stringify(structureResult.inspectTwist || []));
  // 初始化绞向数据(确保有值)
  formData.inspectTwist.forEach((item: any) => {
    if (!item.direction) item.direction = "";
  });
};
// 获取详情数据
const getDetailData = async (id: string, deviceType: string) => {
  try {
    const response = await RoutingInspectionApi.getStrandedInspectionStructureInfoById({ id });
    recordData.value = response.data;
    detailData.value = response.data.structureInfo;
    console.log("detailData.value", detailData.value);
    tempFiles.value = []; // 清空临时文件
    initFormData(); // 数据返回后初始化表单
    detailDataLoaded.value = true; // 数据加载完成后,渲染子组件
    console.log("父组件-数据就绪后打印");
  } catch (error) {
    console.error("获取详情失败:", error);
    uni.showToast({ title: "加载失败", icon: "error" });
@@ -436,8 +408,14 @@
// 页面加载
onLoad((options: any) => {
  paramsId.value = options.id;
  getDetailData(options.id, options.deviceType);
  try {
    paramsId.value = options.id;
    paramsType.value = options.deviceType;
    getDetailData(options.id, options.deviceType);
  } catch (error) {
    console.error("获取详情失败:", error);
    uni.showToast({ title: "加载失败", icon: "error" });
  }
});
// 编辑模式切换
@@ -448,8 +426,8 @@
// 取消编辑(重置表单)
const close = () => {
  isEdit.value = false;
  tempFiles.value = []; // 取消时清空临时上传的文件
  initFormData(); // 恢复原始数据
  tempFiles.value = [];
  initFormData();
};
// 保存编辑(含必填项校验)
@@ -482,7 +460,9 @@
  }
  console.log("1111", deviceUid.value);
  if (!deviceUid.value) return uni.showToast({ title: "请扫描二维码", icon: "none" });
  // 4. 提交数据到接口
  const { newFiles } = attachmentRef.value.getSubmitFiles();
  console.log("newFiles", newFiles);
  const allFileIds = [...newFiles];
  try {
    const res = await RoutingInspectionApi.strandedPatrolCheckInspection({
      deviceUid: deviceUid.value,
@@ -499,13 +479,13 @@
      inspectionResult: {
        sampleComplete: formData.sampleComplete,
      },
      processInspectionAttachmentList: tempFiles.value.map((f) => f.url), // 新上传的附件
      processInspectionAttachmentList: allFileIds,
    });
    if (res.code === 200) {
      uni.showToast({ title: "保存成功", icon: "success" });
      isEdit.value = false;
      getDetailData(paramsId.value, recordData.value.deviceType); // 刷新数据
      getDetailData(paramsId.value, paramsType.value);
    } else {
      uni.showModal({ title: res.msg || "保存失败", icon: "error" });
    }
@@ -513,61 +493,6 @@
    console.error("保存失败:", e);
    uni.showModal({ title: e.message || "保存失败", icon: "error" });
  }
};
// 附件上传前置校验
const beforeUpload = (file: any) => {
  // 限制图片大小不超过2M
  const maxSize = 2 * 1024 * 1024;
  if (file.size > maxSize) {
    uni.showToast({ title: "图片大小不能超过2M", icon: "none" });
    return false;
  }
  return true;
};
// 附件上传成功回调
const handleUploadSuccess = (res: any) => {
  // 假设接口返回格式: { url: 'xxx', id: 'xxx' }
  if (Array.isArray(res)) {
    tempFiles.value = [
      ...tempFiles.value,
      ...res.map((file) => ({
        ...file,
        id: `temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`, // 生成临时ID
      })),
    ];
  } else {
    tempFiles.value.push({
      ...res,
      id: `temp-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`,
    });
  }
};
// 附件上传失败回调
const handleUploadFail = (err: any) => {
  uni.showToast({ title: "附件上传失败", icon: "error" });
};
// 删除附件
const deleteFile = (id: string | number) => {
  // 删除临时文件
  tempFiles.value = tempFiles.value.filter((file) => file.id !== id);
  // 如果是原有文件,需要标记删除(实际项目中可能需要接口交互)
  if (typeof id !== "string" || !id.startsWith("temp-")) {
    // 这里可以实现删除原有文件的逻辑
    recordData.value.structureInfo.files = recordData.value.structureInfo.files.filter(
      (file: any, index: number) => index !== id
    );
  }
};
// 图片预览
const previewImage = (url: string) => {
  previewImageUrl.value = url;
  show.value = true;
};
const handleClose = () => {
@@ -639,7 +564,7 @@
  } catch (err) {
    console.error("JSON解析失败:", err);
    toast.error("扫码数据异常");
    return; // 解析失败直接返回,避免后续错误
    return;
  }
  deviceUid.value = codeObj?.uid;
  toast.success("扫码成功");