| | |
| | | } from "@/api/equipmentManagement/upkeep"; |
| | | import { |
| | | listMaintenanceTaskFiles, |
| | | bindMaintenanceTaskFile, |
| | | uploadMaintenanceTaskFile, |
| | | delMaintenanceTaskFile, |
| | | } from "@/api/equipmentManagement/maintenanceTaskFile"; |
| | | import { ElMessage } from "element-plus"; |
| | |
| | | |
| | | const pendingTempFiles = ref([]); |
| | | const planFileList = ref([]); |
| | | // 编辑模式下已保存但待删除的文件列表 |
| | | const pendingDeleteFiles = ref([]); |
| | | |
| | | const registrantDisplayName = computed( |
| | | () => userStore.nickName || userStore.name || "当前登录用户" |
| | |
| | | const resetAttachmentState = () => { |
| | | pendingTempFiles.value = []; |
| | | planFileList.value = []; |
| | | pendingDeleteFiles.value = []; |
| | | }; |
| | | |
| | | const normalizeFilePreviewUrl = (url = "") => { |
| | |
| | | status: "success", |
| | | uid: `saved-${item.id}`, |
| | | fileId: item.id, |
| | | // 标记为已保存的文件,不是新上传的 |
| | | isNew: false, |
| | | })); |
| | | }; |
| | | |
| | |
| | | const handlePlanFileUpload = async (options) => { |
| | | const { file, onSuccess, onError } = options; |
| | | try { |
| | | if (id.value) { |
| | | const fd = new FormData(); |
| | | fd.append("file", file); |
| | | fd.append("deviceMaintenanceId", String(id.value)); |
| | | const res = await uploadMaintenanceTaskFile(fd); |
| | | if (res.code === 200) { |
| | | await loadPlanFiles(id.value); |
| | | onSuccess(res); |
| | | ElMessage.success("附件上传成功"); |
| | | } else { |
| | | onError(new Error(res.msg || "上传失败")); |
| | | } |
| | | return; |
| | | } |
| | | // 无论新增还是编辑,都先上传到临时目录 |
| | | const res = await uploadTempFile(file); |
| | | if (res.code !== 200) { |
| | | onError(new Error(res.msg || "上传失败")); |
| | | return; |
| | | } |
| | | const data = res.data || {}; |
| | | const tempId = data.tempId; |
| | | |
| | | // 记录临时文件信息 |
| | | pendingTempFiles.value.push({ |
| | | tempId: data.tempId, |
| | | tempId: tempId, |
| | | name: data.originalName || file.name, |
| | | fileId: id.value ? tempId : undefined, |
| | | }); |
| | | |
| | | onSuccess(res); |
| | | planFileList.value.push({ |
| | | name: data.originalName || file.name, |
| | | url: "", |
| | | status: "success", |
| | | uid: data.tempId, |
| | | tempId: data.tempId, |
| | | uid: tempId, |
| | | tempId: tempId, |
| | | // 标记为新上传的临时文件 |
| | | isNew: true, |
| | | }); |
| | | ElMessage.success("附件上传成功"); |
| | | } catch (e) { |
| | | onError(e); |
| | | ElMessage.error("附件上传失败"); |
| | |
| | | }; |
| | | |
| | | const handlePlanFileRemove = async (file) => { |
| | | if (file.fileId) { |
| | | try { |
| | | await delMaintenanceTaskFile(file.fileId); |
| | | await loadPlanFiles(id.value); |
| | | } catch (e) { |
| | | ElMessage.error("删除附件失败"); |
| | | } |
| | | // 从显示列表中移除 |
| | | planFileList.value = planFileList.value.filter((f) => f.uid !== file.uid); |
| | | |
| | | const tempId = file.tempId || file.uid; |
| | | if (file.isNew) { |
| | | // 新上传的临时文件,直接从待上传列表移除 |
| | | pendingTempFiles.value = pendingTempFiles.value.filter((f) => f.tempId !== tempId); |
| | | return; |
| | | } |
| | | const tempId = file.tempId || file.uid; |
| | | pendingTempFiles.value = pendingTempFiles.value.filter((f) => f.tempId !== tempId); |
| | | planFileList.value = planFileList.value.filter((f) => (f.tempId || f.uid) !== tempId); |
| | | |
| | | if (file.fileId) { |
| | | pendingDeleteFiles.value.push({ |
| | | fileId: file.fileId, |
| | | name: file.name, |
| | | }); |
| | | } |
| | | }; |
| | | |
| | | const bindPendingFiles = async (planId) => { |
| | | if (!pendingTempFiles.value.length) return; |
| | | for (const item of pendingTempFiles.value) { |
| | | await bindMaintenanceTaskFile({ |
| | | tempId: item.tempId, |
| | | name: item.name, |
| | | deviceMaintenanceId: planId, |
| | | }); |
| | | // 处理编辑模式下待删除的文件 |
| | | const processPendingDeletes = async () => { |
| | | if (!pendingDeleteFiles.value.length) return; |
| | | for (const file of pendingDeleteFiles.value) { |
| | | await delMaintenanceTaskFile(file.fileId); |
| | | } |
| | | }; |
| | | |
| | |
| | | payload.maintenancePerson = maintainer.nickName; |
| | | } |
| | | } |
| | | // 传递临时文件ID列表,由后端统一处理 |
| | | if (pendingTempFiles.value.length > 0) { |
| | | payload.tempFileIds = pendingTempFiles.value.map((f) => f.tempId); |
| | | } |
| | | return payload; |
| | | }; |
| | | |
| | |
| | | try { |
| | | const payload = buildSubmitPayload(); |
| | | if (id.value) { |
| | | // 编辑模式:先处理待删除的文件,再保存表单 |
| | | await processPendingDeletes(); |
| | | const { code } = await editUpkeep({ id: unref(id), ...payload }); |
| | | if (code == 200) { |
| | | ElMessage.success("编辑计划成功"); |
| | |
| | | emits("ok"); |
| | | } |
| | | } else { |
| | | // 新增模式:tempFileIds 会随 payload 一起传到后端,由后端处理临时文件的关联 |
| | | const res = await addUpkeep(payload); |
| | | if (res.code == 200) { |
| | | const planId = res.data?.id; |
| | | if (planId) { |
| | | await bindPendingFiles(planId); |
| | | } |
| | | ElMessage.success("新增计划成功"); |
| | | visible.value = false; |
| | | emits("ok"); |