Prechádzať zdrojové kódy

照片下载之后修改保存逻辑,不保存在相册里,保存在自定义文件夹内

ZhangLeo 2 mesiacov pred
rodič
commit
1d6e6d0084
2 zmenil súbory, kde vykonal 138 pridanie a 40 odobranie
  1. 73 23
      utils/dataProcessor.uts
  2. 65 17
      utils/qcDataProcessor.uts

+ 73 - 23
utils/dataProcessor.uts

@@ -69,6 +69,57 @@ export const showProgress = (index : number, title : string) => {
 	}, 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 {
@@ -231,28 +282,27 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
 																						if (res.statusCode === 200) {
 																							// 等待一小段时间确保文件完全下载
 																							setTimeout(() => {
-																								// 图片下载成功,使用uni.saveImageToPhotosAlbum保存到相册
-																								uni.saveImageToPhotosAlbum({
-																									filePath: res.tempFilePath,
-																									success: () => {
-																										console.log('图片保存成功:', res.tempFilePath);
-																										// 将保存路径添加到数组中
-																										imagePathsArr.push(res.tempFilePath);
-
-																										// 保存成功后等待1秒再处理下一张
-																										setTimeout(() => {
-																											resolve();
-																										}, 1000);
-																									},
-																									fail: (err) => {
-																										console.error('保存图片失败:', err);
-																										// 保存失败也继续处理下一张,但记录错误
-																										setTimeout(() => {
-																											reject(err);
-																										}, 500);
-																									}
+																								// 直接移动文件到应用数据目录,不保存到相册
+																								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确保文件完全下载
+																						}, 500); // 等待500ms确保文件完全下载
 																						} else {
 																							console.error('下载图片失败,状态码:', res.statusCode);
 																							reject(new Error(`下载图片失败,状态码: ${res.statusCode}`));
@@ -593,14 +643,14 @@ export const uploadDataToAPI = async (productCode : string, callback ?: () => vo
 		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);

+ 65 - 17
utils/qcDataProcessor.uts

@@ -121,6 +121,57 @@ export const showProgress = (index : number, title : string) => {
 	}, 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 {
@@ -339,23 +390,20 @@ const downloadAndSaveImage = async (pk: string, apiToken: string): Promise<strin
 				if (res.statusCode === 200) {
 					// 等待一小段时间确保文件完全下载
 					setTimeout(() => {
-						// 图片下载成功,使用uni.saveImageToPhotosAlbum保存到相册
-						uni.saveImageToPhotosAlbum({
-							filePath: res.tempFilePath,
-							success: () => {
-								console.log('图片保存成功:', res.tempFilePath);
-								// 保存成功后等待1秒再处理下一张
-								setTimeout(() => {
-									resolve(res.tempFilePath);
-								}, 1000);
-							},
-							fail: (err) => {
-								console.error('保存图片失败:', err);
-								// 保存失败返回空字符串继续处理
-								setTimeout(() => {
-									resolve('');
-								}, 500);
-							}
+						moveFile(res.tempFilePath).then((newFilePath) => {
+							console.log('图片已移动并添加到数组:', newFilePath);
+						
+							// 处理完成后等待1秒再处理下一张
+							setTimeout(() => {
+								resolve(newFilePath);
+							}, 500);
+						}).catch((error) => {
+							console.error('文件移动失败,使用原始路径:', error);
+							// 如果移动失败,使用原始路径作为备选
+							// 处理完成后等待1秒再处理下一张
+							setTimeout(() => {
+								resolve(res.tempFilePath);
+							}, 1000);
 						});
 					}, 500); // 等待500ms确保文件完全下载
 				} else {