| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694 | 
							- import { getCurrentUserSync, getTokenFromApi } from '@/utils/auth.uts'
 
- import { saveMediaInfo, saveMediaRecord, getLatestRecord, removeInfoAndRecord, getList, updateData, addLog } from '@/api/work'
 
- import { globalConfig } from '@/config'
 
- // 类型定义保持不变
 
- export type ApiResponse = {
 
- 	code : number;
 
- 	msg : string;
 
- 	data : MediaInfoData[];
 
- }
 
- export type MediaInfoData = {
 
- 	pk : string;
 
- 	workorder ?: string;
 
- 	invname ?: string;
 
- 	productcode ?: string;
 
- 	cardno ?: string;
 
- 	model ?: string;
 
- 	graphid ?: string;
 
- 	ver ?: string;
 
- 	phase ?: string;
 
- 	processno ?: string;
 
- 	createtime ?: string;
 
- 	createuser ?: string;
 
- 	updatetime ?: string;
 
- 	updateuser ?: string;
 
- 	qmImageTaskClist ?: MediaRecordData[];
 
- }
 
- export type MediaRecordData = {
 
- 	senum ?: string;
 
- 	photoitem ?: string;
 
- 	part ?: string;
 
- 	partno ?: string;
 
- 	pk : string;
 
- 	exampleid ?: string;
 
- 	descb ?: string;
 
- 	num ?: number;
 
- 	urlspl ?: string;
 
- 	imgname ?: string;
 
- 	urlpdt ?: string;
 
- 	createtime ?: string;
 
- 	createuser ?: string;
 
- 	updatetime ?: string;
 
- 	updateuser ?: string;
 
- 	photoFiles_example ?: PhotoFilesExample[];
 
- }
 
- export type PhotoFilesExample = {
 
- 	_id ?: string,
 
- }
 
- export type UploadImg = {
 
- 	sxid : string,
 
- 	pk : string,
 
- 	path : string
 
- }
 
- export const showProgress = (index : number, title : string) => {
 
- 	// 在Android设备上,需要给hideLoading和showLoading之间添加延迟
 
- 	// 先隐藏之前的加载提示
 
- 	uni.hideLoading();
 
- 	// 添加50ms的延迟,确保hideLoading完全执行后再显示新的进度
 
- 	setTimeout(() => {
 
- 		uni.showLoading({
 
- 			title: `正在${title}第${index}个任务数据`,
 
- 			mask: true
 
- 		});
 
- 	}, 50);
 
- }
 
- export const moveFile = (oldPath : string) : Promise<string> => {
 
- 	return new Promise((resolve, reject) => {
 
- 		// 生成唯一的文件名
 
- 		const timestamp = Date.now();
 
- 		const randomNum = Math.floor(Math.random() * 1000);
 
- 		const fileExtension = oldPath.substring(oldPath.lastIndexOf('.'));
 
- 		const newImgName = `download_${timestamp}_${randomNum}${fileExtension}`;
 
- 		
 
- 		// 定义新的保存路径 - 不放在相册里,放在应用数据目录下的downloadImgs文件夹
 
- 		const destPath = `${uni.env.USER_DATA_PATH}/downloadImgs/${newImgName}`;
 
- 		
 
- 		try {
 
- 			// 确保目标目录存在
 
- 			const fs = uni.getFileSystemManager();
 
- 			try {
 
- 				fs.accessSync(`${uni.env.USER_DATA_PATH}/downloadImgs`);
 
- 			} catch (e) {
 
- 				// 目录不存在,创建目录
 
- 				fs.mkdirSync(`${uni.env.USER_DATA_PATH}/downloadImgs`, true);
 
- 			}
 
- 			
 
- 			// 先拷贝文件到新路径
 
- 			fs.copyFile({
 
- 				srcPath: oldPath,
 
- 				destPath: destPath,
 
- 				success: function () {
 
- 					// 删除原文件,释放空间
 
- 					fs.unlink({
 
- 						filePath: oldPath,
 
- 						success: function () {
 
- 							console.log('原文件已删除');
 
- 						},
 
- 						fail: function (unlinkErr) {
 
- 							console.error('删除原文件失败', unlinkErr);
 
- 						}
 
- 					});
 
- 					console.log('文件移动成功,新路径:', destPath);
 
- 					resolve(destPath);
 
- 				},
 
- 				fail: function (copyErr) {
 
- 					console.error('拷贝文件失败', copyErr);
 
- 					reject(copyErr);
 
- 				}
 
- 			});
 
- 		} catch (error) {
 
- 			console.error('文件移动过程发生错误', error);
 
- 			reject(error);
 
- 		}
 
- 	});
 
- }
 
- //声像任务下载
 
- export const downloadDataFromAPI = async (productCode : string, callback ?: () => void) : Promise<boolean> => {
 
- 	try {
 
- 		uni.showLoading({ title: '任务开始下载' });
 
- 		const apiToken = await getTokenFromApi();
 
- 		if (apiToken == null || apiToken == '') {
 
- 			uni.hideLoading();
 
- 			uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
 
- 			return false
 
- 		}
 
- 		//校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖,
 
- 		//如果没有则直接保存。如果有存在已经在执行中的产品号,则提示已存在执行中的任务
 
- 		const infoJson = await getLatestRecord(productCode, null);
 
- 		if (infoJson?.['data'] != null) {
 
- 			let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
 
- 			let ingNum = parseInt(info?.['statusRecordCount'] as string);
 
- 			//覆盖标识位
 
- 			let overwiteFlag = ref(false);
 
- 			// 先检查是否有任务正在执行中
 
- 			if (info != null && ingNum > 0) {
 
- 				uni.showToast({ title: `当前产品号已有任务在执行中!`, icon: 'error' });
 
- 				return false;
 
- 			}
 
- 			// 使用Promise来处理异步流程
 
- 			let deleteDataPromise = new Promise<boolean>((resolve) => {
 
- 				if (info != null && ingNum == 0) {
 
- 					//可以被覆盖,需要有提示框,给用户确认
 
- 					uni.showModal({
 
- 						title: '系统提示',
 
- 						content: '该产品号已存在任务是否覆盖掉?',
 
- 						cancelText: '取消',
 
- 						confirmText: '确定',
 
- 						success: function (res) {
 
- 							if (res.confirm) {
 
- 								// 标记为需要覆盖
 
- 								overwiteFlag.value = true;
 
- 								// 执行删除数据操作
 
- 								let pid = info?.['pdid'] as string;
 
- 								removeInfoAndRecord(pid).then((recordDelResponse) => {
 
- 									console.log('删除数据响应:', recordDelResponse);
 
- 									// 删除成功,解析Promise并允许继续执行
 
- 									// 确保模态框已完全关闭后再解析Promise
 
- 									setTimeout(() => {
 
- 										resolve(true);
 
- 									}, 300);
 
- 								}).catch((error) => {
 
- 									console.error('删除数据失败:', error);
 
- 									uni.showToast({ title: '删除旧数据失败', icon: 'error' });
 
- 									resolve(false);
 
- 								});
 
- 							} else {
 
- 								// 用户取消覆盖
 
- 								uni.hideLoading();
 
- 								overwiteFlag.value = false;
 
- 								resolve(false);
 
- 							}
 
- 						}
 
- 					});
 
- 				} else {
 
- 					// 不需要显示确认框,直接解析Promise
 
- 					resolve(true);
 
- 				}
 
- 			});
 
- 			// 等待删除数据操作完成
 
- 			const canContinue : boolean = await deleteDataPromise;
 
- 			if (!canContinue) {
 
- 				// 如果不能继续(删除失败或用户取消),则终止函数执行
 
- 				return false;
 
- 			}
 
- 		}
 
- 		// 使用Promise处理HTTP请求,避免嵌套Promise
 
- 		return new Promise<boolean>((resolve) => {
 
- 			uni.request({
 
- 				url: `${globalConfig.host}${globalConfig.downloadURL}${productCode}`,
 
- 				method: 'GET',
 
- 				header: {
 
- 					'token': apiToken
 
- 				},
 
- 				success: (res) => {
 
- 					let singleObject = res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject;
 
- 					if (singleObject != null && singleObject.code == 666) {
 
- 						let mediaInfoList = singleObject?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
 
- 						if (mediaInfoList != null && mediaInfoList.length > 0) {
 
- 							mediaInfoList.forEach(item => {
 
- 								if (item != null) {
 
- 									let data = JSON.parse<MediaInfoData>(item.toJSONString());
 
- 									if (data != null) {
 
- 										saveMediaInfo(item).then((resSave : UTSJSONObject) => {
 
- 											const lastIdStr = resSave?.['lastId'] as string | null;
 
- 											const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;
 
- 											if (lastId != null) {
 
- 												let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
 
- 												if (recordList != null && recordList.length > 0) {
 
- 													const totalRecords = recordList.length;
 
- 													let processedRecords = 0;
 
- 													// 使用async/await处理循环中的异步操作
 
- 													const processAllRecords = async () => {
 
- 														// 创建一个Map来跟踪不同photoitem的计数
 
- 														const photoItemCounter : Map<string, number> = new Map();
 
- 														for (var i = 0; i < recordList.length; i++) {
 
- 															// 更新进度
 
- 															processedRecords++;
 
- 															showProgress(processedRecords, '下载');
 
- 															const record : MediaRecordData = recordList[i] as MediaRecordData;
 
- 															// 获取各个字段的值
 
- 															const photoitem = record.photoitem as string;
 
- 															// 根据photoitem类型统计senum
 
- 															if (!photoItemCounter.has(photoitem)) {
 
- 																photoItemCounter.set(photoitem, 1);
 
- 															} else {
 
- 																const currentCount = photoItemCounter.get(photoitem);
 
- 																photoItemCounter.set(photoitem, (currentCount != null ? currentCount : 0) + 1);
 
- 															}
 
- 															const senum = photoItemCounter.get(photoitem);
 
- 															const finalSenum = senum != null ? senum : 1;
 
- 															const partno = record.partno;
 
- 															const part = record.part;
 
- 															const pk = record.pk;
 
- 															const descb = record.descb;
 
- 															const num = record.num;
 
- 															const imgname = record.imgname;
 
- 															const urlpdt = record.urlpdt;
 
- 															const createtime = record.createtime;
 
- 															const createuser = record.createuser;
 
- 															const updatetime = record.updatetime;
 
- 															const updateuser = record.updateuser;
 
- 															// 创建数组来存储图片ID和保存路径
 
- 															var exampleidArr = [] as string[];
 
- 															var imagePathsArr = [] as string[];
 
- 															let exampleList = record?.['photoFiles_example'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
 
- 															if (exampleList != null && exampleList.length > 0) {
 
- 																// 下载所有图片并等待完成
 
- 																const imageDownloadPromises = [] as Promise<void>[];
 
- 																// 顺序下载和保存图片,避免并行导致的随机失败
 
- 																for (var j = 0; j < exampleList.length; j++) {
 
- 																	const example : PhotoFilesExample = exampleList[j] as PhotoFilesExample;
 
- 																	if (example._id != null) {
 
- 																		exampleidArr.push(example._id);
 
- 																		try {
 
- 																			// 串行执行,等待前一张图片处理完成再处理下一张
 
- 																			await new Promise<void>((resolve, reject) => {
 
- 																				// 使用uni.downloadFile下载图片
 
- 																				uni.downloadFile({
 
- 																					url: `${globalConfig.getImgURL}${example._id}`,
 
- 																					header: {
 
- 																						'token': apiToken
 
- 																					},
 
- 																					success: (res) => {
 
- 																						console.log(`${globalConfig.getImgURL}${example._id}`);
 
- 																						if (res.statusCode === 200) {
 
- 																							// 等待一小段时间确保文件完全下载
 
- 																							setTimeout(() => {
 
- 																								// 直接移动文件到应用数据目录,不保存到相册
 
- 																								moveFile(res.tempFilePath).then((newFilePath) => {
 
- 																									// 将新的文件路径添加到数组中
 
- 																									imagePathsArr.push(newFilePath);
 
- 																									console.log('图片已移动并添加到数组:', newFilePath);
 
- 																									// 处理完成后等待1秒再处理下一张
 
- 																									setTimeout(() => {
 
- 																										resolve();
 
- 																									}, 500);
 
- 																								}).catch((error) => {
 
- 																									console.error('文件移动失败,使用原始路径:', error);
 
- 																									// 如果移动失败,使用原始路径作为备选
 
- 																									imagePathsArr.push(res.tempFilePath);
 
- 																									// 处理完成后等待1秒再处理下一张
 
- 																									setTimeout(() => {
 
- 																										resolve();
 
- 																									}, 1000);
 
- 																								});
 
- 																						}, 500); // 等待500ms确保文件完全下载
 
- 																						} else {
 
- 																							console.error('下载图片失败,状态码:', res.statusCode);
 
- 																							reject(new Error(`下载图片失败,状态码: ${res.statusCode}`));
 
- 																						}
 
- 																					},
 
- 																					fail: (err) => {
 
- 																						console.error('请求图片失败:', err);
 
- 																						reject(err);
 
- 																					}
 
- 																				});
 
- 																			});
 
- 																		} catch (error) {
 
- 																			console.error(`处理第${j + 1}张图片时出错:`, error);
 
- 																			// 出错后继续处理下一张图片
 
- 																		}
 
- 																	}
 
- 																}
 
- 															}
 
- 															// 拼接图片ID和路径
 
- 															const exampleid = exampleidArr.join(",");
 
- 															const imagePaths = imagePathsArr.join(",");
 
- 															console.log("=========================================>");
 
- 															console.log(imagePaths);
 
- 															// 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
 
- 															var values = `${finalSenum === null ? '1' : `'${finalSenum}'`}, ${photoitem === null ? 'null' : `'${photoitem}'`}, ${data?.['productcode'] === null ? 'null' : `'${data?.['productcode']}'`},${part === null ? 'null' : `'${part}'`},${partno === null ? 'null' : `'${partno}'`},${pk === null ? 'null' : `'${pk}'`},${exampleid === null ? 'null' : `'${exampleid}'`},${descb === null ? 'null' : `'${descb}'`},${num === null ? 0 : num},1,'', ${imagePaths === null ? 'null' : `'${imagePaths}'`},${imgname === null ? 'null' : `'${imgname}'`},${urlpdt === null ? 'null' : `'${urlpdt}'`}, ${createtime === null ? 'null' : `'${createtime}'`}, ${createuser === null ? 'null' : `'${createuser}'`}, ${updatetime === null ? 'null' : `'${updatetime}'`}, ${updateuser === null ? 'null' : `'${updateuser}'`}, ${lastId === null ? 0 : lastId}`;
 
- 															// 只有在所有图片下载完成后才调用saveMediaRecord
 
- 															saveMediaRecord(values);
 
- 														}
 
- 														// 所有记录处理完成,显示完成提示
 
- 														setTimeout(() => {
 
- 															uni.hideLoading();
 
- 															uni.showToast({ title: `下载完成`, icon: 'success' });
 
- 															if (callback != null) {
 
- 																callback();
 
- 															}
 
- 														}, 500);
 
- 													};
 
- 													// 开始处理所有记录
 
- 													processAllRecords();
 
- 												}
 
- 											} else {
 
- 												console.log('保存媒体信息成功,但未获取到主键ID');
 
- 											}
 
- 										})
 
- 									}
 
- 								}
 
- 							});
 
- 							
 
- 							addLog({ module: null, dataid: 0, content: '下载声像记录', status: 1, params: productCode, createuser: getCurrentUserSync()??'' })
 
- 							resolve(true);
 
- 						} else {
 
- 							uni.hideLoading();
 
- 							uni.showToast({ title: '未获取到数据', icon: 'error' });
 
- 							resolve(false);
 
- 						}
 
- 					} else {
 
- 						const errorMsg = singleObject.msg != null ? singleObject.msg : '未知错误';
 
- 						uni.hideLoading();
 
- 						uni.showToast({ title: `请求失败: ${errorMsg}`, icon: 'error' });
 
- 						resolve(false);
 
- 					}
 
- 				},
 
- 				fail: (err) => {
 
- 					console.error('请求数据失败:', err);
 
- 					uni.hideLoading();
 
- 					uni.showToast({ title: `请求失败: ${err.errMsg}`, icon: 'error' });
 
- 					resolve(false);
 
- 				}
 
- 			});
 
- 		});
 
- 	} catch (error) {
 
- 		console.error(error);
 
- 		uni.showToast({ title: '下载失败,请重试', icon: 'error' });
 
- 		uni.hideLoading();
 
- 		return false;
 
- 	}
 
- }
 
- //声像任务上传
 
- export const uploadDataToAPI = async (productCode : string, callback ?: () => void) : Promise<boolean> => {
 
- 	try {
 
- 		//暂定需要上传的数据文件
 
- 		const apiToken = await getTokenFromApi();
 
- 		if (apiToken == null || apiToken == '') {
 
- 			uni.hideLoading();
 
- 			uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
 
- 			return false
 
- 		}
 
- 		// const apiToken = ''
 
- 		// 获取数据
 
- 		let query = `productno = '${productCode}' and uploadFlag = '0' and urlpdt is not null  AND (LENGTH(urlpdt) - LENGTH(REPLACE(urlpdt, ',', '')) + 1) = num`
 
- 		const res = await getList('app_media_record', query, null, null, null, null);
 
- 		let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
 
- 		console.log(dataList);
 
- 		
 
- 		if (dataList == null || dataList.length === 0) {
 
- 			uni.hideLoading();
 
- 			uni.showToast({ title: '未获取到需要上传的数据', icon: 'error' });
 
- 			return false;
 
- 		}
 
- 		let doneRecordList = dataList.filter(item => item.getString("status") == '3');
 
- 		console.log(doneRecordList);
 
- 		if (doneRecordList.length === 0) {
 
- 			uni.hideLoading();
 
- 			uni.showToast({ title: '上传图片数据为空', icon: 'error' });
 
- 			return false;
 
- 		}
 
- 		// 1. 收集所有需要上传的图片
 
- 		let allImagesToUpload : UploadImg[] = [];
 
- 		let processStep = 1;
 
- 		for (let index = 0; index < dataList.length; index++) {
 
- 			const record = dataList[index];
 
- 			if (record.getString('urlpdt') == '' || record.getString('urlpdt') == null
 
- 				|| record.getString('pk') == '' || record.getString('pk') == null) {
 
- 				continue
 
- 			}
 
- 			showProgress(processStep, '准备上传');
 
- 			//收集图片信息
 
- 			let urlpdtStr = record.getString('urlpdt');
 
- 			let sxid = record.getString('sxid');
 
- 			let pk = record.getString('pk');
 
- 			let urlArr = urlpdtStr?.split(",") ?? [];
 
- 			for (let j = 0; j < urlArr.length; j++) {
 
- 				let path = urlArr[j];
 
- 				const fullFilePath = `${uni.env.USER_DATA_PATH}` + path;
 
- 				allImagesToUpload.push({sxid: sxid ?? '', pk: pk ?? '', path: fullFilePath });
 
- 			}
 
- 			processStep++;
 
- 		}
 
- 		// 2. 统计总图片数量
 
- 		const totalImages = allImagesToUpload.length;
 
- 		console.log(`总共需要上传${totalImages}张图片`);
 
- 		if (totalImages === 0) {
 
- 			uni.hideLoading();
 
- 			uni.showToast({ title: '没有需要上传的图片', icon: 'none' });
 
- 			return true;
 
- 		}
 
- 		// 3. 执行第一次上传
 
- 		let failedImages : UploadImg[] = [];
 
- 		let successCount = 0;
 
- 		uni.showLoading({ title: `正在上传图片 (0/${totalImages})` });
 
- 		for (let i = 0; i < allImagesToUpload.length; i++) {
 
- 			const { sxid, pk, path } = allImagesToUpload[i];
 
- 			console.log(`开始上传文件: ${path}, 索引: ${i}, apiToken: ${apiToken}, billid: ${pk}, sxid: ${sxid}`);
 
- 			try {
 
- 				// 串行执行,等待前一个图片上传完成再处理下一张
 
- 				await new Promise<void>((resolve, reject) => {
 
- 					// 使用uni.uploadFile进行文件上传
 
- 					console.log(`上传路径:${globalConfig.uploadURL}`)
 
- 					const uploadTask = uni.uploadFile({
 
- 						url: `${globalConfig.uploadURL}`,
 
- 						filePath: path,
 
- 						name: 'file', // 文件参数名
 
- 						header: {
 
- 							'token': apiToken,
 
- 							'content-type': 'multipart/form-data'
 
- 						},
 
- 						formData: {
 
- 							'billid': pk
 
- 						},
 
- 						success: (uploadRes) => {
 
- 							if (uploadRes.statusCode === 200) {
 
- 								successCount++;
 
- 								//更新数据库记录
 
- 								let updatedData = " uploadFlag = 1 "
 
- 								updateData('app_media_record', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
 
- 									console.log(`更新图片记录的上传标识 ${sxid}`)
 
- 								});
 
- 								// 等待一小段时间确保文件完全上传并处理完成
 
- 								setTimeout(() => {
 
- 									resolve();
 
- 								}, 1000);
 
- 							} else {
 
- 								console.error(`文件${path}上传失败,状态码:`, uploadRes.statusCode);
 
- 								let updatedData = " uploadFlag = 0 "
 
- 								updateData('app_media_record', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
 
- 									console.log(`上传失败更新图片记录的上传标识 ${sxid}`)
 
- 								});
 
- 								uni.showModal({
 
- 									content: `图片上传失败,状态码:${uploadRes.statusCode}`,
 
- 									title: '图片上传失败'
 
- 								})
 
- 								setTimeout(() => {
 
- 									reject(new Error(`上传失败,状态码: ${uploadRes.statusCode}`));
 
- 								}, 500);
 
- 							}
 
- 						},
 
- 						fail: (err) => {
 
- 							console.error(`文件${path}上传失败`, err);
 
- 							// 上传失败也继续处理下一张,但记录错误
 
- 							let updatedData = " uploadFlag = 0 "
 
- 							updateData('app_media_record', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
 
- 								console.log(`上传错误更新图片记录的上传标识 ${sxid}`)
 
- 							});
 
- 							uni.showModal({
 
- 								content: `文件上传失败:${err}`,
 
- 								title: '图片上传失败'
 
- 							})
 
- 							setTimeout(() => {
 
- 								reject(err);
 
- 							}, 500);
 
- 						},
 
- 						complete: () => {
 
- 							// console.log(`文件${path}上传操作完成`);
 
- 							// 更新进度
 
- 							uni.hideLoading();
 
- 							uni.showLoading({ title: `正在上传图片 (${i + 1}/${totalImages})` });
 
- 						}
 
- 					});
 
- 					//调试时开启
 
- 					// uploadTask.onProgressUpdate((res) => {
 
- 					// 	console.log('上传进度' + res.progress);
 
- 					// 	console.log('已经上传的数据长度' + res.totalBytesSent);
 
- 					// 	console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
 
- 					// });
 
- 				});
 
- 			} catch (error) {
 
- 				// 捕获上传失败的错误,将失败的图片信息添加到错误数组
 
- 				console.log(`处理第${i + 1}张图片时出错:`, error);
 
- 				failedImages.push({ sxid, pk, path });
 
- 				
 
- 				uni.showModal({
 
- 					content: `文件上传失败:${error}`,
 
- 					title: '图片上传失败'
 
- 				})
 
- 			}
 
- 			// 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
 
- 			if (i < allImagesToUpload.length - 1) {
 
- 				await new Promise<void>((resolve) => {
 
- 					setTimeout(() => {
 
- 						resolve()
 
- 					}, 1000)
 
- 				})
 
- 			}
 
- 		}
 
- 		// 4. 执行重试逻辑(最多3次)
 
- 		const maxRetries = 3;
 
- 		let retryImages = [...failedImages]; // 复制初始失败的图片数组
 
- 		for (let retryCount = 1; retryCount <= maxRetries; retryCount++) {
 
- 			if (retryImages.length === 0) {
 
- 				// 如果没有需要重试的图片,提前结束循环
 
- 				break;
 
- 			}
 
- 			console.log(`开始第${retryCount}次重试上传失败的图片,共${retryImages.length}张`);
 
- 			uni.hideLoading();
 
- 			uni.showLoading({ title: `第${retryCount}次重试 (0/${retryImages.length})` });
 
- 			// 创建新的错误数组,用于收集本次重试失败的图片
 
- 			let currentFailedImages : UploadImg[] = [];
 
- 			let currentSuccessCount = 0;
 
- 			// 串行上传失败的图片
 
- 			for (let i = 0; i < retryImages.length; i++) {
 
- 				const { sxid, pk, path } = retryImages[i];
 
- 				console.log(`重试上传文件: ${path}, 索引: ${i}, 重试次数: ${retryCount}, apiToken: ${apiToken}, billid: ${pk}, sxid: ${sxid}`);
 
- 				console.log(`重试上传路径:${globalConfig.uploadURL}`)
 
- 				await new Promise<void>((resolve) => {
 
- 					uni.uploadFile({
 
- 						url: `${globalConfig.uploadURL}`,
 
- 						filePath: path,
 
- 						name: 'file',
 
- 						header: {
 
- 							'token': apiToken
 
- 						},
 
- 						formData: {
 
- 							'billid': pk
 
- 						},
 
- 						success: (uploadRes) => {
 
- 							if (uploadRes.statusCode === 200) {
 
- 								console.log(`重试文件${path}上传成功`, uploadRes);
 
- 								currentSuccessCount++;
 
- 								//更新数据库
 
- 								let updatedData = " uploadFlag = 1 "
 
- 								updateData('app_media_record', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
 
- 									console.log(`更新图片记录的上传标识 ${sxid}`)
 
- 								});
 
- 								resolve();
 
- 							} else {
 
- 								currentFailedImages.push({ sxid, pk, path });
 
- 							}
 
- 							console.log(`重试上传完成,当前成功: ${currentSuccessCount}, 当前失败: ${currentFailedImages.length}`);
 
- 							
 
- 						},
 
- 						fail: (err) => {
 
- 							console.error(`重试文件${path}上传失败`, err);
 
- 							currentFailedImages.push({sxid, pk, path });
 
- 							uni.showModal({
 
- 								content: `文件上传失败:${err}`,
 
- 								title: '图片上传失败'
 
- 							})
 
- 							resolve();
 
- 						},
 
- 						complete: () => {
 
- 							// console.log(`重试文件${path}上传操作完成`);
 
- 							// 更新进度
 
- 							uni.hideLoading();
 
- 							uni.showLoading({ title: `第${retryCount}次重试 (${i + 1}/${retryImages.length})` });
 
- 						}
 
- 					});
 
- 				});
 
- 				// 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
 
- 				if (i < retryImages.length - 1) {
 
- 					await new Promise<void>((resolve) => {
 
- 						setTimeout(() => {
 
- 							resolve()
 
- 						}, 1000)
 
- 					})
 
- 				}
 
- 			}
 
- 			// 更新成功数量
 
- 			successCount += currentSuccessCount;
 
- 			// 更新下一次重试的图片列表为本次失败的图片
 
- 			retryImages = currentFailedImages;
 
- 		}
 
- 		// 5. 显示总结信息
 
- 		const finalFailedCount = retryImages.length;
 
- 		console.log(`上传总结: 总共${totalImages}张图片, 成功${successCount}张, 失败${finalFailedCount}张`);
 
- 		uni.hideLoading();
 
- 		// 三次重试后如果仍有失败的图片,显示提示
 
- 		if (finalFailedCount > 0) {
 
- 			uni.showModal({
 
- 				title: '上传提示',
 
- 				content: `总共需要上传${totalImages}张图片,成功${successCount}张,失败${finalFailedCount}张。\n经过${maxRetries}次重试后,仍有${finalFailedCount}张图片上传失败,请检查网络后重新上传。`,
 
- 				showCancel: false
 
- 			});
 
- 		} else {
 
- 			uni.showToast({
 
- 				title: `上传完成!共${totalImages}张图片,全部成功。`,
 
- 				icon: 'success'
 
- 			});
 
- 		}
 
- 		if (callback != null) {
 
- 			callback();
 
- 		}
 
- 		if (finalFailedCount === 0) {
 
- 			let updatedData = " uploadFlag = 1 "
 
- 			updateData('app_media_info', updatedData, 'productno', productCode).then((res : UTSJSONObject) => {
 
- 				console.log(`更新完上传标识 ${productCode}`)
 
- 			});
 
- 		}
 
- 		return finalFailedCount === 0;
 
- 	} catch (error) {
 
- 		console.error(error);
 
- 		uni.showToast({ title: '上传失败,请重试', icon: 'error' });
 
- 		uni.hideLoading();
 
- 		uni.showModal({
 
- 			content: `文件上传失败:${error}`,
 
- 			title: '图片上传失败'
 
- 		})
 
- 		return false;
 
- 	}
 
- }
 
 
  |