buhuazhen
2025-09-23 99a3c5eff1f52bf07235e60c4a2a1922e45e3fcb
src/pages/production/twist/attachment/index.vue
@@ -55,21 +55,27 @@
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 {
@@ -95,20 +101,30 @@
        const filePaths = Array.isArray(res.tempFilePaths)
          ? res.tempFilePaths
          : [res.tempFilePaths];
        const uploadResults = await AttachmentAPI.uploadAttachmentFiles(filePaths);
        const uploadResults: any = await AttachmentAPI.uploadAttachmentFiles(filePaths);
        const result = uploadResults.map((it) => {
          return it.data;
        });
        console.log("result", result);
        // 更新附件列表
        const flattenedResult = result.flat();
        attachmentList.value.push(...flattenedResult);
        console.log(attachmentList.value);
        // 提取附件ID
        const attachmentIds = uploadResults.map((result) => result.data.id).join(",");
        const attachmentId = attachmentList.value.map((item: any) => item.id).join(",");
        // 关联到报工
        if (attachmentId) {
        await AttachmentAPI.addOutputAttachments({
          id: parseInt(reportId.value),
          attachmentIds: attachmentIds,
            id: parseInt(detailData.value.id),
            attachmentId: attachmentId,
        });
          detailData.value.attachmentId = attachmentId;
        }
        toast.show("上传成功");
        // 重新获取附件列表
        await getAttachmentList();
        // await getAttachmentList();
      } catch (error) {
        console.error("上传失败:", error);
        toast.show("上传失败");
@@ -122,7 +138,7 @@
};
// 删除附件
const deleteAttachment = async (attachmentId: number) => {
const deleteAttachment = async (aid: number) => {
  try {
    uni.showModal({
      title: "确认删除",
@@ -130,18 +146,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 +250,10 @@
};
onMounted(() => {
  getAttachmentList();
  uni.$on("detailData", (data) => {
    // 处理接收到的数据
    getAttachmentList(data);
  });
});
</script>