From 9581c0ae9f0d9a2e92744f3dca78960780b9a2df Mon Sep 17 00:00:00 2001
From: yyb <995253665@qq.com>
Date: 星期一, 18 五月 2026 17:42:21 +0800
Subject: [PATCH] 附件

---
 src/pages/equipmentManagement/upkeep/add.vue |  132 ++++++++++++++++++++++++-------------------
 1 files changed, 73 insertions(+), 59 deletions(-)

diff --git a/src/pages/equipmentManagement/upkeep/add.vue b/src/pages/equipmentManagement/upkeep/add.vue
index 194062b..abddae6 100644
--- a/src/pages/equipmentManagement/upkeep/add.vue
+++ b/src/pages/equipmentManagement/upkeep/add.vue
@@ -161,7 +161,6 @@
 	editUpkeep,
 	getUpkeepById,
 	listMaintenanceTaskFiles,
-	addMaintenanceTaskFile,
 	delMaintenanceTaskFile,
 } from '@/api/equipmentManagement/upkeep';
 import { userListNoPageByTenantId } from '@/api/system/user';
@@ -241,6 +240,7 @@
 	maintenancePerson: userStore.nickName || undefined, // 淇濆吇浜�
 	maintenanceLocation: undefined, // 淇濆吇閮ㄤ綅
 	maintenanceItems: undefined, // 淇濆吇鍐呭
+	tempFileIds: [], // 鏈涓婁紶闄勪欢鐨勪复鏃舵枃浠禝D锛屼繚瀛樻椂鎻愪氦
 });
 
 // 鍔犺浇璁惧鍒楄〃
@@ -264,8 +264,22 @@
 };
 
 // 闄勪欢鐩稿叧
+const extractTempFileId = (uploadedFile) => {
+	if (!uploadedFile) return undefined;
+	const data = Array.isArray(uploadedFile) ? uploadedFile[0] : uploadedFile;
+	return data?.tempId ?? data?.tempFileId ?? data?.id;
+};
+
+const syncTempFileIds = () => {
+	form.value.tempFileIds = attachmentList.value
+		.filter((item) => item.isTempFile)
+		.map((item) => item.tempId ?? item.tempFileId)
+		.filter((v) => v !== undefined && v !== null && v !== '');
+};
+
 const getFileAccessUrl = (file = {}) => {
-	const url = file.url || file.tempFilePath || file.path || '';
+	if (file?._localPreviewUrl) return file._localPreviewUrl;
+	const url = file.url || file.tempPath || file.tempFilePath || file.path || '';
 	if (!url) return '';
 	if (String(url).startsWith('http') || String(url).startsWith('blob:') || String(url).startsWith('file:') || String(url).startsWith('wxfile:')) {
 		return url;
@@ -277,6 +291,7 @@
 const fetchAttachmentList = async (id) => {
 	if (!id) {
 		attachmentList.value = [];
+		form.value.tempFileIds = [];
 		return;
 	}
 	try {
@@ -286,41 +301,35 @@
 			deviceMaintenanceId: id,
 		});
 		if (code === 200) {
-			attachmentList.value = data?.records || [];
+			const records = data?.records || [];
+			attachmentList.value = records.map((file) => ({
+				...file,
+				isTempFile: false,
+			}));
 		} else {
 			attachmentList.value = [];
 		}
 	} catch (e) {
 		attachmentList.value = [];
 	}
+	syncTempFileIds();
 };
 
 const chooseAttachment = (sourceType) => {
 	const source = sourceType === 'camera' ? ['camera'] : ['album'];
+	const remaining = 9 - attachmentList.value.length;
+	if (remaining <= 0) {
+		showToast('鏈�澶氫笂浼�9寮犻檮浠�');
+		return;
+	}
 	uni.chooseImage({
-		count: 9,
+		count: Math.min(remaining, 9),
 		sizeType: ['original', 'compressed'],
 		sourceType: source,
 		success: (res) => {
 			const files = res.tempFiles || [];
 			if (!files.length) return;
-			const id = getPageId();
-			if (id) {
-				uploadAttachments(files, id);
-				return;
-			}
-			const tempItems = files.map((file, idx) => {
-				const filePath = file.path || res.tempFilePaths?.[idx];
-				return {
-					id: `temp_${Date.now()}_${idx}`,
-					url: filePath,
-					tempFilePath: filePath,
-					name: file.name || `闄勪欢_${Date.now()}_${idx}.jpg`,
-					isTemp: true,
-				};
-			});
-			attachmentList.value = [...attachmentList.value, ...tempItems];
-			showToast('宸叉坊鍔狅紝淇濆瓨璁″垝鍚庤嚜鍔ㄤ笂浼�');
+			uploadAttachments(files, res.tempFilePaths);
 		},
 		fail: () => {
 			showToast('閫夋嫨鍥剧墖澶辫触');
@@ -328,12 +337,29 @@
 	});
 };
 
-const uploadAttachments = async (files, maintenanceId) => {
-	const commonId = normalizeId(maintenanceId);
-	if (!commonId) {
-		showToast('鏈幏鍙栧埌淇濆吇璁″垝ID锛屼笂浼犲け璐�');
+const handleUploadSuccess = (response, file) => {
+	let uploadedFile = response?.data;
+	if (Array.isArray(uploadedFile)) {
+		uploadedFile = uploadedFile[0];
+	}
+	const tempId = extractTempFileId(uploadedFile);
+	if (!tempId) {
+		showToast('鏈幏鍙栧埌鏂囦欢ID');
 		return;
 	}
+	attachmentList.value.push({
+		tempId,
+		tempFileId: tempId,
+		isTempFile: true,
+		url: uploadedFile?.tempPath || uploadedFile?.url || uploadedFile?.downloadUrl || file.tempFilePath || file.path,
+		tempPath: uploadedFile?.tempPath || '',
+		name: uploadedFile?.originalName || uploadedFile?.originalFilename || file.name,
+		_localPreviewUrl: file.tempFilePath || file.path || '',
+	});
+	syncTempFileIds();
+};
+
+const uploadAttachments = async (files, tempFilePaths = []) => {
 	const token = getToken();
 	if (!token) {
 		showToast('鐧诲綍宸插け鏁堬紝璇烽噸鏂扮櫥褰�');
@@ -341,8 +367,9 @@
 	}
 	uploading.value = true;
 	try {
-		for (const file of files) {
-			const filePath = file.path || file.tempFilePath;
+		for (let i = 0; i < files.length; i++) {
+			const file = files[i];
+			const filePath = file.path || file.tempFilePath || tempFilePaths[i];
 			if (!filePath) continue;
 			await new Promise((resolve, reject) => {
 				uni.uploadFile({
@@ -356,20 +383,13 @@
 						try {
 							const parsed = JSON.parse(uploadRes.data || '{}');
 							if (uploadRes.statusCode === 200 && parsed.code === 200) {
-								const fileName = file.name || filePath.split('/').pop();
-								addMaintenanceTaskFile({
-									name: fileName,
-									deviceMaintenanceId: commonId,
-									url: parsed.data?.tempPath || parsed.data?.url || '',
-								})
-									.then((addRes) => {
-										if (addRes.code === 200) {
-											resolve(addRes);
-										} else {
-											reject(new Error(addRes.msg || '淇濆瓨闄勪欢淇℃伅澶辫触'));
-										}
-									})
-									.catch(reject);
+								handleUploadSuccess(parsed, {
+									...file,
+									tempFilePath: filePath,
+									path: filePath,
+									name: file.name || `闄勪欢_${Date.now()}_${i}.jpg`,
+								});
+								resolve(parsed);
 							} else {
 								reject(new Error(parsed.msg || '涓婁紶澶辫触'));
 							}
@@ -382,7 +402,6 @@
 			});
 		}
 		showToast('涓婁紶鎴愬姛');
-		await fetchAttachmentList(commonId);
 	} catch (e) {
 		showToast(e?.message || '涓婁紶澶辫触');
 	} finally {
@@ -402,8 +421,14 @@
 };
 
 const removeAttachment = (file, index) => {
-	if (!file?.id || file?.isTemp) {
+	if (file?.isTempFile) {
 		attachmentList.value.splice(index, 1);
+		syncTempFileIds();
+		return;
+	}
+	if (!file?.id) {
+		attachmentList.value.splice(index, 1);
+		syncTempFileIds();
 		return;
 	}
 	uni.showModal({
@@ -453,6 +478,7 @@
 		// 鏂板妯″紡
 		operationType.value = 'add';
 		attachmentList.value = [];
+		form.value.tempFileIds = [];
 	}
 };
 
@@ -589,8 +615,10 @@
 		loading.value = true;
 		const id = getPageId();
 		
+		syncTempFileIds();
 		// 鍑嗗鎻愪氦鏁版嵁
 		const submitData = { ...form.value };
+		submitData.tempFileIds = form.value.tempFileIds || [];
 		// 纭繚鏃ユ湡鏍煎紡姝g‘
 		if (submitData.maintenancePlanTime && !submitData.maintenancePlanTime.includes(':')) {
 			submitData.maintenancePlanTime = submitData.maintenancePlanTime + ' 00:00:00';
@@ -599,24 +627,9 @@
 		const result = id
 			? await editUpkeep({ id: id, ...submitData })
 			: await addUpkeep(submitData);
-		const { code, data } = result || {};
+		const { code } = result || {};
 		
 		if (code == 200) {
-			if (!id) {
-				const newId = data?.id || data?.maintenanceId || data;
-				if (newId) {
-					const tempFiles = attachmentList.value
-						.filter((item) => item?.isTemp && (item.tempFilePath || item.url))
-						.map((item) => ({
-							path: item.tempFilePath || item.url,
-							tempFilePath: item.tempFilePath || item.url,
-							name: item.name,
-						}));
-					if (tempFiles.length) {
-						await uploadAttachments(tempFiles, newId);
-					}
-				}
-			}
 			showToast(`${id ? "缂栬緫" : "鏂板"}璁″垝鎴愬姛`);
 			setTimeout(() => {
 				uni.removeStorageSync('repairId');
@@ -651,6 +664,7 @@
 	} else {
 		operationType.value = 'add';
 		attachmentList.value = [];
+		form.value.tempFileIds = [];
 		loadForm();
 	}
 };

--
Gitblit v1.9.3