|
@@ -2,6 +2,7 @@ import { getToken, getTokenFromApi } from './auth'
|
|
|
import { saveMediaInfo, saveMediaRecord, getLatestRecord, removeInfoAndRecord } from '@/api/work'
|
|
|
import {globalConfig} from '@/config'
|
|
|
|
|
|
+
|
|
|
// 类型定义保持不变
|
|
|
export type ApiResponse = {
|
|
|
code : number;
|
|
@@ -51,9 +52,9 @@ export type PhotoFilesExample = {
|
|
|
}
|
|
|
|
|
|
export const downloadDataFromAPI = async (productCode: string, callback ?: () => void) : Promise<boolean> => {
|
|
|
- // try {
|
|
|
+ try {
|
|
|
//校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖,
|
|
|
- //如果没有则直接保存。如果有存在已经在执行的产品号,则提示已存在执行中的任务
|
|
|
+ //如果没有则直接保存。如果有存在已经在执行中的产品号,则提示已存在执行中的任务
|
|
|
const infoJson = await getLatestRecord(productCode, null);
|
|
|
if (infoJson?.['data']!=null) {
|
|
|
let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
|
|
@@ -118,6 +119,15 @@ export const downloadDataFromAPI = async (productCode: string, callback ?: () =>
|
|
|
const apiToken = await getTokenFromApi();
|
|
|
uni.showLoading({ title: '数据下载中...' });
|
|
|
|
|
|
+ // 创建进度条
|
|
|
+ const showProgress = (percentage: number) => {
|
|
|
+ uni.showLoading({
|
|
|
+ title: `图片下载中...${percentage}%`,
|
|
|
+ mask: true
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
// 使用Promise处理HTTP请求,避免嵌套Promise
|
|
|
return new Promise<boolean>((resolve) => {
|
|
|
uni.request({
|
|
@@ -142,46 +152,121 @@ export const downloadDataFromAPI = async (productCode: string, callback ?: () =>
|
|
|
if (lastId != null) {
|
|
|
let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
|
|
|
if (recordList != null && recordList.length > 0) {
|
|
|
- for(var i =0; i< recordList.length; i++) {
|
|
|
- const record: MediaRecordData = recordList[i] as MediaRecordData;
|
|
|
- console.log("==================>", record)
|
|
|
-
|
|
|
- // 获取各个字段的值
|
|
|
- const senum = record.senum;
|
|
|
- const photoitem = record.photoitem;
|
|
|
- const part = record.part;
|
|
|
- const partno = record.partno;
|
|
|
- const pk = record.pk;
|
|
|
- const descb = record.descb;
|
|
|
- const num = record.num;
|
|
|
- const urlspl = record.urlspl;
|
|
|
- const imgname = record.imgname;
|
|
|
- const urlpdt = record.urlpdt;
|
|
|
- const createtime = record.createtime;
|
|
|
- const createuser = record.createuser;
|
|
|
- const updatetime = record.updatetime;
|
|
|
- const updateuser = record.updateuser;
|
|
|
-
|
|
|
- let exampleList = record?.['photoFiles_example'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
|
|
|
- var exampleidArr = [] as string[]
|
|
|
- if(exampleList!=null && exampleList.length > 0) {
|
|
|
- for(var j =0; j < exampleList.length; j++) {
|
|
|
- const example: PhotoFilesExample = exampleList[j] as PhotoFilesExample;
|
|
|
- if(example._id != null) {
|
|
|
- exampleidArr.push(example._id)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- const exampleid = exampleidArr.join(",");
|
|
|
-
|
|
|
- // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
|
|
|
- var values = `${senum === null ? '1' : `'${senum}'`}, ${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,'', ${urlspl === null ? 'null' : `'${urlspl}'`},${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(values);
|
|
|
- }
|
|
|
- // 延迟显示完成提示,确保所有数据保存完成
|
|
|
- setTimeout(() => {
|
|
|
- uni.showToast({ title: `下载完成`, icon: 'success' });
|
|
|
- }, 500);
|
|
|
+ const totalRecords = recordList.length;
|
|
|
+ let processedRecords = 0;
|
|
|
+
|
|
|
+ // 使用async/await处理循环中的异步操作
|
|
|
+ const processAllRecords = async () => {
|
|
|
+ for(var i =0; i < recordList.length; i++) {
|
|
|
+ // 更新进度
|
|
|
+ processedRecords++;
|
|
|
+ const progressPercentage = Math.floor((processedRecords / totalRecords) * 100);
|
|
|
+ showProgress(progressPercentage);
|
|
|
+
|
|
|
+ const record: MediaRecordData = recordList[i] as MediaRecordData;
|
|
|
+ // 获取各个字段的值
|
|
|
+ const senum = record.senum;
|
|
|
+ const photoitem = record.photoitem;
|
|
|
+ const part = record.part;
|
|
|
+ const partno = record.partno;
|
|
|
+ const pk = record.pk;
|
|
|
+ const descb = record.descb;
|
|
|
+ const num = record.num;
|
|
|
+ const urlspl = record.urlspl;
|
|
|
+ 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);
|
|
|
+
|
|
|
+ // 创建图片下载Promise
|
|
|
+ const downloadPromise = new Promise<void>((resolve, reject) => {
|
|
|
+ // 使用uni.downloadFile下载图片
|
|
|
+ uni.downloadFile({
|
|
|
+ url: `http://192.168.5.124:8080/api/images/getImage?fileId=${example._id}`,
|
|
|
+ header: {
|
|
|
+ 'token': apiToken
|
|
|
+ },
|
|
|
+ success: (res) => {
|
|
|
+ if (res.statusCode === 200) {
|
|
|
+ // 图片下载成功,使用uni.saveImageToPhotosAlbum保存到相册
|
|
|
+ uni.saveImageToPhotosAlbum({
|
|
|
+ filePath: res.tempFilePath,
|
|
|
+ success: () => {
|
|
|
+ console.log('图片保存成功:', res.tempFilePath);
|
|
|
+ // 将保存路径添加到数组中
|
|
|
+ imagePathsArr.push(res.tempFilePath);
|
|
|
+ resolve();
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('保存图片失败:', err);
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ console.error('下载图片失败,状态码:', res.statusCode);
|
|
|
+ reject(new Error(`下载图片失败,状态码: ${res.statusCode}`));
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error('请求图片失败:', err);
|
|
|
+ reject(err);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ imageDownloadPromises.push(downloadPromise);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 等待所有图片下载完成
|
|
|
+ try {
|
|
|
+ await Promise.all(imageDownloadPromises);
|
|
|
+ } catch (error) {
|
|
|
+ console.error('图片下载失败:', error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 拼接图片ID和路径
|
|
|
+ const exampleid = exampleidArr.join(",");
|
|
|
+ const imagePaths = imagePathsArr.join(",");
|
|
|
+ console.log("=========================================>");
|
|
|
+ console.log(imagePaths);
|
|
|
+
|
|
|
+ // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
|
|
|
+ var values = `${senum === null ? '1' : `'${senum}'`}, ${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');
|
|
@@ -190,28 +275,31 @@ export const downloadDataFromAPI = async (productCode: string, callback ?: () =>
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
+ 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);
|
|
|
}
|
|
|
- resolve(true);
|
|
|
},
|
|
|
fail: (err) => {
|
|
|
- console.error('请求失败:', err);
|
|
|
+ console.error('请求数据失败:', err);
|
|
|
+ uni.hideLoading();
|
|
|
uni.showToast({ title: `请求失败: ${err.errMsg}`, icon: 'error' });
|
|
|
resolve(false);
|
|
|
- },
|
|
|
- complete: () => {
|
|
|
- console.log('请求完成');
|
|
|
- uni.hideLoading();
|
|
|
}
|
|
|
});
|
|
|
});
|
|
|
- // } catch (error) {
|
|
|
- // console.error(error);
|
|
|
- // uni.showToast({ title: '下载失败,请重试', icon: 'error' });
|
|
|
- // uni.hideLoading();
|
|
|
- // return false;
|
|
|
- // }
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error);
|
|
|
+ uni.showToast({ title: '下载失败,请重试', icon: 'error' });
|
|
|
+ uni.hideLoading();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|