spring
8 天以前 78c565e37520fad100693c4e298e30e7c916d1bb
src/pages/production/wire/attachment/index.vue
@@ -32,8 +32,8 @@
              <text class="upload-time">{{ formatTime(item.createTime) }}</text>
            </view>
          </view>
          <view class="attachment-actions">
            <wd-icon name="delete" color="#ff4757" @click.stop="deleteAttachment(item.id)" />
          <view class="attachment-actions" @click.stop>
            <wd-icon name="delete" color="#ff4757" @click="deleteAttachment(item.id)" />
          </view>
        </view>
      </wd-card>
@@ -52,24 +52,31 @@
// 页面参数
const reportId = ref("");
const reportType = ref("拉丝");
const reportType = ref("绞线");
const attachmentList = ref<any[]>([]);
const detailData = ref<any>({});
// 获取附件列表
const getAttachmentList = async () => {
const getAttachmentList = async (data: any) => {
  try {
    detailData.value = data;
    console.log(" detailData.value", detailData.value);
    const pages = getCurrentPages();
    const currentPage = pages[pages.length - 1];
    const options = (currentPage as any).options;
    const currentReportId = options?.reportId;
    const currentReportId = detailData.value.id;
    if (currentReportId) {
      reportId.value = currentReportId;
      // 直接调用通用查看接口查询附件列表
      // 使用示例中的附件ID数组 [850,851]
      const attachmentIds: number[] = [850, 851]; // 使用HTTP文件中的示例数据
      const attachmentIds: number[] =
        detailData.value.attachmentId !== null ? detailData.value.attachmentId.split(",") : []; // 使用HTTP文件中的示例数据
      if (attachmentIds.length === 0) {
        return;
      }
      const { data } = await AttachmentAPI.listAttachmentFiles(attachmentIds);
      attachmentList.value = data || [];
    } else {
@@ -84,45 +91,136 @@
// 新增附件
const addAttachment = () => {
  uni.chooseFile({
    count: 9, // 最多选择9个文件
    type: "all", // 所有类型文件
    success: async (res) => {
      try {
        toast.show("正在上传...");
        // 上传文件
        const filePaths = Array.isArray(res.tempFilePaths)
          ? res.tempFilePaths
          : [res.tempFilePaths];
        const uploadResults = await AttachmentAPI.uploadAttachmentFiles(filePaths);
        // 提取附件ID
        const attachmentIds = uploadResults.map((result) => result.data.id).join(",");
        // 关联到报工
        await AttachmentAPI.addOutputAttachments({
          id: parseInt(reportId.value),
          attachmentIds: attachmentIds,
        });
        toast.show("上传成功");
        // 重新获取附件列表
        await getAttachmentList();
      } catch (error) {
        console.error("上传失败:", error);
        toast.show("上传失败");
  // 显示选择文件类型的弹窗
  uni.showActionSheet({
    itemList: ["选择图片", "选择视频", "拍照", "录像"],
    success: (res) => {
      switch (res.tapIndex) {
        case 0: // 选择图片
          chooseImages();
          break;
        case 1: // 选择视频
          chooseVideos();
          break;
        case 2: // 拍照
          takePhoto();
          break;
        case 3: // 录像
          recordVideo();
          break;
      }
    },
    fail: (error) => {
      console.error("选择文件失败:", error);
      toast.show("选择文件失败");
      console.error("选择文件类型失败:", error);
      toast.show("选择文件类型失败");
    },
  });
};
// 选择图片
const chooseImages = () => {
  uni.chooseImage({
    count: 9,
    sizeType: ["original", "compressed"],
    sourceType: ["album"],
    success: async (res) => {
      const filePaths = Array.isArray(res.tempFilePaths) ? res.tempFilePaths : [res.tempFilePaths];
      await handleFileUpload(filePaths);
    },
    fail: (error) => {
      console.error("选择图片失败:", error);
      toast.show("选择图片失败");
    },
  });
};
// 选择视频
const chooseVideos = () => {
  uni.chooseVideo({
    sourceType: ["album"],
    maxDuration: 60,
    camera: "back",
    success: async (res) => {
      await handleFileUpload([res.tempFilePath]);
    },
    fail: (error) => {
      console.error("选择视频失败:", error);
      toast.show("选择视频失败");
    },
  });
};
// 拍照
const takePhoto = () => {
  uni.chooseImage({
    count: 1,
    sizeType: ["original", "compressed"],
    sourceType: ["camera"],
    success: async (res) => {
      const filePaths = Array.isArray(res.tempFilePaths) ? res.tempFilePaths : [res.tempFilePaths];
      await handleFileUpload(filePaths);
    },
    fail: (error) => {
      console.error("拍照失败:", error);
      toast.show("拍照失败");
    },
  });
};
// 录像
const recordVideo = () => {
  uni.chooseVideo({
    sourceType: ["camera"],
    maxDuration: 60,
    camera: "back",
    success: async (res) => {
      await handleFileUpload([res.tempFilePath]);
    },
    fail: (error) => {
      console.error("录像失败:", error);
      toast.show("录像失败");
    },
  });
};
// 处理文件上传
const handleFileUpload = async (filePaths: string[]) => {
  try {
    toast.show("正在上传...");
    // 上传文件
    const uploadResults: any = await AttachmentAPI.uploadAttachmentFiles(filePaths);
    const result = uploadResults.map((it: any) => {
      return it.data;
    });
    console.log("result", result);
    // 更新附件列表
    const flattenedResult = result.flat();
    attachmentList.value.push(...flattenedResult);
    console.log(attachmentList.value);
    // 提取附件ID
    const attachmentId = attachmentList.value.map((item: any) => item.id).join(",");
    // 关联到报工
    if (attachmentId) {
      await AttachmentAPI.addOutputAttachments({
        id: parseInt(detailData.value.id),
        attachmentId: attachmentId,
      });
      detailData.value.attachmentId = attachmentId;
    }
    toast.show("上传成功");
  } catch (error) {
    console.error("上传失败:", error);
    toast.show("上传失败");
  }
};
// 删除附件
const deleteAttachment = async (attachmentId: number) => {
const deleteAttachment = async (aid: number) => {
  try {
    uni.showModal({
      title: "确认删除",
@@ -130,18 +228,17 @@
      success: async (res) => {
        if (res.confirm) {
          // 前端手动删除:直接从列表中移除这条数据
          attachmentList.value = attachmentList.value.filter((item) => item.id !== attachmentId);
          attachmentList.value = attachmentList.value.filter((item) => item.id !== aid);
          // 获取剩余的附件ID组合
          const remainingIds = attachmentList.value.map((item) => item.id);
          const attachmentIds = remainingIds.join(",");
          const attachmentId = attachmentList.value.map((item) => item.id).join(",");
          // 调用报工添加附件接口,更新附件关联
          await AttachmentAPI.addOutputAttachments({
            id: parseInt(reportId.value),
            attachmentIds: attachmentIds,
            id: parseInt(detailData.value.id),
            attachmentId: attachmentId,
          });
          detailData.value.attachmentId = attachmentId;
          toast.show("删除成功");
        }
      },
@@ -235,7 +332,10 @@
};
onMounted(() => {
  getAttachmentList();
  uni.$on("detailData", (data) => {
    // 处理接收到的数据
    getAttachmentList(data);
  });
});
</script>