|
@@ -1,5 +1,5 @@
|
|
|
import { getToken, getTokenFromApi } from './auth'
|
|
|
-import { saveTaskInfo, saveTaskPhoto, saveTaskKeyProcess, saveTaskRecord, saveTaskRecordItem, getLatestTask, removeTaskAndRecord } from '@/api/work'
|
|
|
+import { saveTaskInfo, saveTaskPhoto, saveTaskKeyProcess, saveTaskRecord, saveTaskRecordItem, getLatestTask, removeTaskAndRecord, getList, updateData } from '@/api/work'
|
|
|
import { globalConfig } from '@/config'
|
|
|
|
|
|
|
|
@@ -12,7 +12,7 @@ export type ApiResponse = {
|
|
|
|
|
|
export type AppTaskInfo = {
|
|
|
gxpk : string;
|
|
|
- pk_serial: string;
|
|
|
+ pk_serial : string;
|
|
|
cardno ?: string;
|
|
|
productcode ?: string;
|
|
|
model ?: string;
|
|
@@ -20,7 +20,7 @@ export type AppTaskInfo = {
|
|
|
invname ?: string;
|
|
|
graphid ?: string;
|
|
|
processno ?: string;
|
|
|
- ver ?: string;
|
|
|
+ ver ?: string;
|
|
|
lastupdatetime ?: string;
|
|
|
qcrecord ?: AppTaskRecord;
|
|
|
photolist ?: AppTaskPhoto[];
|
|
@@ -108,6 +108,27 @@ export type AppTaskKeyProcess = {
|
|
|
ts ?: string;
|
|
|
}
|
|
|
|
|
|
+export type UploadImg = {
|
|
|
+ sxid : string,
|
|
|
+ pk : string,
|
|
|
+ path : string
|
|
|
+}
|
|
|
+
|
|
|
+export type Task = {
|
|
|
+ pdid : number,
|
|
|
+ gxpk : string,
|
|
|
+ cardno : string,
|
|
|
+ productcode : string,
|
|
|
+ model : string,
|
|
|
+ workorder : string,
|
|
|
+ invname : string,
|
|
|
+ graphid : string,
|
|
|
+ processno : string,
|
|
|
+ gxno : string,
|
|
|
+ ver : string,
|
|
|
+ lastupdatetime : string
|
|
|
+}
|
|
|
+
|
|
|
export const showProgress = (index : number, title : string) => {
|
|
|
// 在Android设备上,需要给hideLoading和showLoading之间添加延迟
|
|
|
// 先隐藏之前的加载提示
|
|
@@ -128,10 +149,10 @@ export const moveFile = (oldPath : string) : Promise<string> => {
|
|
|
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();
|
|
@@ -141,7 +162,7 @@ export const moveFile = (oldPath : string) : Promise<string> => {
|
|
|
// 目录不存在,创建目录
|
|
|
fs.mkdirSync(`${uni.env.USER_DATA_PATH}/downloadImgs`, true);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 先拷贝文件到新路径
|
|
|
fs.copyFile({
|
|
|
srcPath: oldPath,
|
|
@@ -183,11 +204,10 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
|
|
|
return false;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖,
|
|
|
//如果没有则直接保存。如果有存在已经在执行中的产品号,则提示已存在执行中的任务
|
|
|
const infoJson = await getLatestTask(productCode, null);
|
|
|
- console.log(infoJson);
|
|
|
if (infoJson?.['data'] != null) {
|
|
|
let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
|
|
|
// let ingNum = parseInt(info?.['pdid'] as string);
|
|
@@ -237,7 +257,7 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
resolve(true);
|
|
|
}
|
|
|
});
|
|
|
-
|
|
|
+
|
|
|
// 等待删除数据操作完成
|
|
|
const canContinue : boolean = await deleteDataPromise;
|
|
|
if (!canContinue) {
|
|
@@ -269,17 +289,17 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
const resSave = await saveTaskInfo(taskInfo);
|
|
|
const lastIdStr = resSave?.['lastId'] as string | null;
|
|
|
const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;
|
|
|
-
|
|
|
+
|
|
|
if (lastId != null) {
|
|
|
let photoList = taskInfo?.['photolist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
|
|
|
if (photoList != null && photoList.length > 0) {
|
|
|
const totalRecords = photoList.length;
|
|
|
-
|
|
|
+
|
|
|
// 按顺序处理所有图片记录
|
|
|
for (let i = 0; i < photoList.length; i++) {
|
|
|
// 更新进度
|
|
|
showProgress(i + 1, '下载');
|
|
|
-
|
|
|
+
|
|
|
let photoObj = photoList[i] as UTSJSONObject;
|
|
|
// 获取各个字段的值
|
|
|
const pk = photoObj['pk'] as string;
|
|
@@ -297,40 +317,40 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
const fk_processTask = photoObj['fk_processTask'] as string | null;
|
|
|
const cs = photoObj['cs'] as string | null;
|
|
|
const ts = photoObj['ts'] as string | null;
|
|
|
-
|
|
|
+
|
|
|
// 获取图片
|
|
|
let imagePathsArr = [] as string[];
|
|
|
try {
|
|
|
// 串行执行,等待前一张图片处理完成再处理下一张
|
|
|
const tempFilePath = await downloadAndSaveImage(pk, apiToken);
|
|
|
- if (tempFilePath != null && tempFilePath != '') {
|
|
|
+ if (tempFilePath != null && tempFilePath != '') {
|
|
|
imagePathsArr.push(tempFilePath);
|
|
|
}
|
|
|
} catch (error) {
|
|
|
console.error(`处理图片时出错:`, error);
|
|
|
// 出错后继续处理下一张图片
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 拼接图片ID和路径
|
|
|
const imagePaths = imagePathsArr.join(",");
|
|
|
// 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
|
|
|
var values = `${lastId === null ? 0 : lastId},${pk === null ? '' : `'${pk}'`}, ${photographpoint === null ? 'null' : `'${photographpoint}'`}, ${photographdescription === null ? 'null' : `'${photographdescription}'`},${photourl === null ? 'null' : `'${photourl}'`},
|
|
|
${imagePaths === null ? 'null' : `'${imagePaths}'`},${photoname === null ? 'null' : `'${photoname}'`},${fk_qcRecord === null ? 'null' : `'${fk_qcRecord}'`},${fk_prodcode === null ? 'null' : `'${fk_prodcode}'`}, ${prodno === null ? 'null' : `'${prodno}'`},
|
|
|
${fk_creator === null ? 'null' : `'${fk_creator}'`},${fks_operator === null ? 'null' : `'${fks_operator}'`}, ${operator === null ? 'null' : `'${operator}'`}, ${processStep === null ? 'null' : `'${processStep}'`}, ${fk_processTask === null ? 'null' : `'${fk_processTask}'`}, ${cs === null ? 'null' : `'${cs}'`}, ${ts === null ? 'null' : `'${ts}'`}`;
|
|
|
-
|
|
|
+
|
|
|
// 立即保存当前图片的任务信息
|
|
|
saveTaskPhoto(values);
|
|
|
}
|
|
|
//保存关键工序
|
|
|
let keyProcessList = taskInfo?.['keyprocesslist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
|
|
|
if (keyProcessList != null && keyProcessList.length > 0) {
|
|
|
- keyProcessList.forEach(item =>{
|
|
|
+ keyProcessList.forEach(item => {
|
|
|
item['pdid'] = lastId;
|
|
|
console.log(item);
|
|
|
saveTaskKeyProcess(item);
|
|
|
})
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
//保存检验记录
|
|
|
let recordObj = taskInfo?.['qcrecord'] as UTSJSONObject ?? {} as UTSJSONObject;
|
|
|
if (recordObj != null) {
|
|
@@ -341,7 +361,7 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
if (recordId != null) {
|
|
|
let recordItemList = recordObj?.['items'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
|
|
|
if (recordItemList != null && recordItemList.length > 0) {
|
|
|
- recordItemList.forEach(item =>{
|
|
|
+ recordItemList.forEach(item => {
|
|
|
item['psxid'] = recordId;
|
|
|
console.log(item);
|
|
|
saveTaskRecordItem(item);
|
|
@@ -355,7 +375,7 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// 所有记录处理完成,显示完成提示
|
|
|
uni.hideLoading();
|
|
|
uni.showToast({ title: `下载完成`, icon: 'success' });
|
|
@@ -378,7 +398,7 @@ export const downloadDataFromAPI = async (productCode : string, callback ?: () =
|
|
|
}
|
|
|
|
|
|
// 辅助函数:下载并保存图片
|
|
|
-const downloadAndSaveImage = async (pk: string, apiToken: string): Promise<string> => {
|
|
|
+const downloadAndSaveImage = async (pk : string, apiToken : string) : Promise<string> => {
|
|
|
return new Promise<string>((resolve, reject) => {
|
|
|
// 使用uni.downloadFile下载图片
|
|
|
uni.downloadFile({
|
|
@@ -392,7 +412,7 @@ const downloadAndSaveImage = async (pk: string, apiToken: string): Promise<strin
|
|
|
setTimeout(() => {
|
|
|
moveFile(res.tempFilePath).then((newFilePath) => {
|
|
|
console.log('图片已移动并添加到数组:', newFilePath);
|
|
|
-
|
|
|
+
|
|
|
// 处理完成后等待1秒再处理下一张
|
|
|
setTimeout(() => {
|
|
|
resolve(newFilePath);
|
|
@@ -420,277 +440,349 @@ const downloadAndSaveImage = async (pk: string, apiToken: string): Promise<strin
|
|
|
}
|
|
|
|
|
|
|
|
|
-//声像任务上传
|
|
|
-// export const uploadDataToAPI = async (productCode : string, callback ?: () => void) : Promise<boolean> => {
|
|
|
-
|
|
|
-// try {
|
|
|
-// //暂定需要上传的数据文件
|
|
|
-// //const infoJson = await getLatestRecord(productCode, null);
|
|
|
-// const apiToken = await getTokenFromApi();
|
|
|
-// if (apiToken == null || apiToken == '') {
|
|
|
-// uni.hideLoading();
|
|
|
-// uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
|
|
|
-// return false
|
|
|
-// }
|
|
|
-
|
|
|
-// // 获取数据
|
|
|
-// const res = await getJoinList('app_media_record as r', 'app_media_info as i', 'r.*,i.productno, i.uploadFlag', 'r.pid=i.pdid', 'i.productno', productCode, 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 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({ 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 { pk, path } = allImagesToUpload[i];
|
|
|
-// console.log(`开始上传文件: ${path}, 索引: ${i}, apiToken: ${apiToken}, billid: ${pk}`);
|
|
|
-
|
|
|
-// 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
|
|
|
-// },
|
|
|
-// formData: {
|
|
|
-// 'billid': pk
|
|
|
-// },
|
|
|
-// success: (uploadRes) => {
|
|
|
-// if (uploadRes.statusCode === 200) {
|
|
|
-// console.log(`文件${path}上传成功`, uploadRes);
|
|
|
-// // 解析响应数据
|
|
|
-// const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
|
|
|
-// console.log(resData)
|
|
|
-// if (resData?.['_id'] != null && resData?.['_id'] != '') {
|
|
|
-// successCount++;
|
|
|
-// // 等待一小段时间确保文件完全上传并处理完成
|
|
|
-// setTimeout(() => {
|
|
|
-// resolve();
|
|
|
-// }, 1000);
|
|
|
-// } else {
|
|
|
-// setTimeout(() => {
|
|
|
-// reject('响应数据无效');
|
|
|
-// }, 500);
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// console.error(`文件${path}上传失败,状态码:`, uploadRes.statusCode);
|
|
|
-// setTimeout(() => {
|
|
|
-// reject(new Error(`上传失败,状态码: ${uploadRes.statusCode}`));
|
|
|
-// }, 500);
|
|
|
-// }
|
|
|
-// },
|
|
|
-// fail: (err) => {
|
|
|
-// console.error(`文件${path}上传失败`, err);
|
|
|
-// // 上传失败也继续处理下一张,但记录错误
|
|
|
-// 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({ pk, path });
|
|
|
-// // 出错后继续处理下一张图片
|
|
|
-// }
|
|
|
-
|
|
|
-// // 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
|
|
|
-// 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 { pk, path } = retryImages[i];
|
|
|
-// console.log(`重试上传文件: ${path}, 索引: ${i}, 重试次数: ${retryCount}, apiToken: ${apiToken}, billid: ${pk}`);
|
|
|
-// 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);
|
|
|
-// const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
|
|
|
-// if (resData?.['_id'] != null && resData?.['_id'] != '') {
|
|
|
-// currentSuccessCount++;
|
|
|
-// } else {
|
|
|
-// currentFailedImages.push({ pk, path });
|
|
|
-// }
|
|
|
-// } else {
|
|
|
-// currentFailedImages.push({ pk, path });
|
|
|
-// }
|
|
|
-// console.log(`重试上传完成,当前成功: ${currentSuccessCount}, 当前失败: ${currentFailedImages.length}`);
|
|
|
-// resolve();
|
|
|
-// },
|
|
|
-// fail: (err) => {
|
|
|
-// console.error(`重试文件${path}上传失败`, err);
|
|
|
-// currentFailedImages.push({ pk, path });
|
|
|
-// 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();
|
|
|
-// }
|
|
|
+//检验任务上传
|
|
|
+export const uploadDataToAPI = async (gxpk : 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 infoJson = await getLatestTask(gxpk, null);
|
|
|
+ // console.log(infoJson)
|
|
|
+ if (infoJson != null) {
|
|
|
+ let taskInfoJson = infoJson?.['data'] as UTSJSONObject ?? {};
|
|
|
+
|
|
|
+ if (taskInfoJson == null) {
|
|
|
+ uni.hideLoading();
|
|
|
+ uni.showToast({ title: '未获取到数据', icon: 'error' });
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ let taskInfo = JSON.parse<Task>(taskInfoJson.toJSONString());
|
|
|
+ let canContinueFlag = true;
|
|
|
+
|
|
|
+ // 1. 收集所有需要上传的图片
|
|
|
+ let res = await getList('app_task_photo', 'pdid', taskInfo?.pdid.toString(), 'uploadFlag', '0', null)
|
|
|
+ let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
|
|
|
+ if (dataList != null && dataList.length > 0) {
|
|
|
+ //处理图片对象数组
|
|
|
+ let allImagesToUpload : UploadImg[] = [];
|
|
|
+ let processStep = 1;
|
|
|
+
|
|
|
+ for (let index = 0; index < dataList.length; index++) {
|
|
|
+ const record = dataList[index];
|
|
|
+
|
|
|
+ if (record.getString('photourl') == '' || record.getString('photourl') == null
|
|
|
+ || record.getString('pk') == '' || record.getString('pk') == null) {
|
|
|
+ continue
|
|
|
+ }
|
|
|
+
|
|
|
+ showProgress(processStep, '准备上传');
|
|
|
+ //收集图片信息
|
|
|
+ let photourlStr = record.getString('photourl');
|
|
|
+ let sxid = record.getString('sxid');
|
|
|
+ let pk = record.getString('pk');
|
|
|
+ let urlArr = photourlStr?.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
|
|
|
+ },
|
|
|
+ formData: {
|
|
|
+ 'billid': pk
|
|
|
+ },
|
|
|
+ success: (uploadRes) => {
|
|
|
+ if (uploadRes.statusCode === 200) {
|
|
|
+ console.log(`文件${path}上传成功`, uploadRes);
|
|
|
+ // 解析响应数据
|
|
|
+ const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
|
|
|
+ console.log(resData)
|
|
|
+ if (resData?.['_id'] != null && resData?.['_id'] != '') {
|
|
|
+ successCount++;
|
|
|
+ //更新数据库记录
|
|
|
+ let updatedData = " uploadflag = 1 "
|
|
|
+ updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
|
|
|
+ console.log(`更新图片记录的上传标识 ${sxid}`)
|
|
|
+ });
|
|
|
+ // 等待一小段时间确保文件完全上传并处理完成
|
|
|
+ setTimeout(() => {
|
|
|
+ resolve();
|
|
|
+ }, 1000);
|
|
|
+ } else {
|
|
|
+ setTimeout(() => {
|
|
|
+ reject('响应数据无效');
|
|
|
+ }, 500);
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ console.error(`文件${path}上传失败,状态码:`, uploadRes.statusCode);
|
|
|
+ let updatedData = " uploadflag = 0 "
|
|
|
+ updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
|
|
|
+ console.log(`上传失败更新图片记录的上传标识 ${sxid}`)
|
|
|
+ });
|
|
|
+ setTimeout(() => {
|
|
|
+ reject(new Error(`上传失败,状态码: ${uploadRes.statusCode}`));
|
|
|
+ }, 500);
|
|
|
+ }
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error(`文件${path}上传失败`, err);
|
|
|
+ // 上传失败也继续处理下一张,但记录错误
|
|
|
+ let updatedData = " uploadflag = 0 "
|
|
|
+ updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
|
|
|
+ console.log(`上传错误更新图片记录的上传标识 ${sxid}`)
|
|
|
+ });
|
|
|
+ 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 });
|
|
|
+ // 出错后继续处理下一张图片
|
|
|
+ }
|
|
|
+
|
|
|
+ // 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
|
|
|
+ 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);
|
|
|
+ const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
|
|
|
+ if (resData?.['_id'] != null && resData?.['_id'] != '') {
|
|
|
+ currentSuccessCount++;
|
|
|
+ //更新数据库
|
|
|
+ let updatedData = " uploadflag = 1 "
|
|
|
+ updateData('app_task_photo', updatedData, 'sxid', sxid).then((res : UTSJSONObject) => {
|
|
|
+ console.log(`更新图片记录的上传标识 ${sxid}`)
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ currentFailedImages.push({ sxid, pk, path });
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ currentFailedImages.push({ sxid, pk, path });
|
|
|
+ }
|
|
|
+ console.log(`重试上传完成,当前成功: ${currentSuccessCount}, 当前失败: ${currentFailedImages.length}`);
|
|
|
+ resolve();
|
|
|
+ },
|
|
|
+ fail: (err) => {
|
|
|
+ console.error(`重试文件${path}上传失败`, err);
|
|
|
+ currentFailedImages.push({ sxid, pk, path });
|
|
|
+ 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) {
|
|
|
+ canContinueFlag = false
|
|
|
+ }
|
|
|
+ return finalFailedCount === 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!canContinueFlag) {
|
|
|
+ uni.showToast({
|
|
|
+ title: `图片上传失败,请重新上传`,
|
|
|
+ icon: 'error'
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ taskInfoJson.set('photolist', Array<UTSJSONObject>())
|
|
|
+
|
|
|
+
|
|
|
+ //1.填充关键工序数据
|
|
|
+ let keyRes = await getList('app_task_keyprocess', 'pdid', taskInfo?.pdid.toString(), null, null, null)
|
|
|
+ let keyList = keyRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
|
|
|
+ if (keyList != null && keyList.length > 0) {
|
|
|
+ let keyObjectList = [] as AppTaskKeyProcess[]
|
|
|
+ keyList.forEach(item => {
|
|
|
+ let keyObject = JSON.parse<AppTaskKeyProcess>(item.toJSONString());
|
|
|
+ if (keyObject != null){
|
|
|
+ keyObjectList.push(keyObject)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ taskInfoJson.set('keyprocesslist', keyObjectList)
|
|
|
+ }
|
|
|
+
|
|
|
+ //2.填充检验任务数据
|
|
|
+ let qcRecoerdRes = await getList('app_task_record', 'pdid', taskInfo?.pdid.toString(), null, null, null)
|
|
|
+ let qcRecordList = qcRecoerdRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
|
|
|
+ if (qcRecordList != null && qcRecordList.length > 0) {
|
|
|
+ let qcRecordJson = qcRecordList[0]
|
|
|
+ let sxid = qcRecordJson.getString("sxid")
|
|
|
+
|
|
|
+ // 3.填充检验任务item 数据据
|
|
|
+ let qcRecoerdItemRes = await getList('app_task_record_item', 'psxid', sxid, null, null, null)
|
|
|
+ let qcRecordItemList = qcRecoerdItemRes?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
|
|
|
+ if (qcRecordItemList != null && qcRecordItemList.length > 0) {
|
|
|
+ let itemList = [] as AppTaskRecordItem[]
|
|
|
+ keyList.forEach(item => {
|
|
|
+ let itemObj = JSON.parse<AppTaskRecordItem>(item.toJSONString());
|
|
|
+ if (itemObj != null){
|
|
|
+ itemList.push(itemObj)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ qcRecordJson.set('items', itemList)
|
|
|
+ }
|
|
|
+ let qcRecord = JSON.parse<AppTaskRecord>(qcRecordJson.toJSONString());
|
|
|
+ taskInfoJson.set("qcrecord", qcRecord)
|
|
|
+ }
|
|
|
+
|
|
|
+ let requestTask = JSON.parse<AppTaskInfo>(taskInfoJson.toJSONString());
|
|
|
+ console.log(requestTask)
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
-// 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();
|
|
|
-// return false;
|
|
|
-// }
|
|
|
-// }
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (error) {
|
|
|
+ console.error(error);
|
|
|
+ uni.showToast({ title: '上传失败,请重试', icon: 'error' });
|
|
|
+ uni.hideLoading();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|