qcDataProcessor.uts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696
  1. import { getToken, getTokenFromApi } from './auth'
  2. import { saveTaskInfo, saveTaskPhoto, saveTaskKeyProcess, saveTaskRecord, saveTaskRecordItem, getLatestTask, removeTaskAndRecord } from '@/api/work'
  3. import { globalConfig } from '@/config'
  4. // 类型定义保持不变
  5. export type ApiResponse = {
  6. code : number;
  7. msg : string;
  8. data : AppTaskInfo;
  9. }
  10. export type AppTaskInfo = {
  11. gxpk : string;
  12. pk_serial: string;
  13. cardno ?: string;
  14. productcode ?: string;
  15. model ?: string;
  16. workorder ?: string;
  17. invname ?: string;
  18. graphid ?: string;
  19. processno ?: string;
  20. ver ?: string;
  21. lastupdatetime ?: string;
  22. qcrecord ?: AppTaskRecord;
  23. photolist ?: AppTaskPhoto[];
  24. keyprocesslist ?: AppTaskKeyProcess[];
  25. }
  26. export type AppTaskRecord = {
  27. pk ?: string;
  28. fk_invcode ?: string;
  29. no ?: string;
  30. invcode ?: string;
  31. invname : string;
  32. processStep ?: string;
  33. fk_processTask ?: string;
  34. checkTarget ?: string;
  35. checknum ?: number;
  36. oknum ?: number;
  37. ngnum ?: number;
  38. status ?: string;
  39. result ?: string;
  40. checkTime ?: string;
  41. cs ?: string;
  42. ts ?: string;
  43. items ?: AppTaskRecordItem[];
  44. }
  45. export type AppTaskRecordItem = {
  46. pk ?: string;
  47. fk_qcRecord ?: string;
  48. fk_prodcode ?: string;
  49. prodno ?: string;
  50. name ?: string;
  51. no ?: string;
  52. nature ?: string;
  53. unit ?: string;
  54. maxNum ?: number;
  55. minNum ?: number;
  56. status ?: string;
  57. memo ?: string;
  58. measuredvalue ?: string;
  59. result ?: string;
  60. cs ?: string;
  61. ts ?: string;
  62. }
  63. export type AppTaskPhoto = {
  64. pk : string;
  65. photographpoint ?: string;
  66. photographdescription ?: string;
  67. photourl ?: string;
  68. photoname ?: string;
  69. fk_qcRecord ?: string;
  70. fk_prodcode ?: string;
  71. prodno ?: string;
  72. fk_creator ?: string;
  73. fks_operator ?: string;
  74. operator ?: string;
  75. processStep ?: string;
  76. fk_processTask ?: string;
  77. cs ?: string;
  78. ts ?: string;
  79. }
  80. export type AppTaskKeyProcess = {
  81. pk ?: string;
  82. pdid ?: string;
  83. testapparatus ?: string;
  84. tableid ?: string;
  85. testrequirelower ?: string;
  86. testrequireupper ?: string;
  87. parametername ?: string;
  88. parameterorder ?: string;
  89. measureunit ?: string;
  90. parameterinstruction ?: string;
  91. parameterid ?: string;
  92. fk_creator ?: string;
  93. fk_prodcode ?: string;
  94. prodno ?: string;
  95. processstep ?: string;
  96. fk_processtask ?: string;
  97. measuredvaluemin ?: string;
  98. measuredvaluemax ?: string;
  99. fks_operator ?: string;
  100. cs ?: string;
  101. ts ?: string;
  102. }
  103. export const showProgress = (index : number, title : string) => {
  104. // 在Android设备上,需要给hideLoading和showLoading之间添加延迟
  105. // 先隐藏之前的加载提示
  106. uni.hideLoading();
  107. // 添加50ms的延迟,确保hideLoading完全执行后再显示新的进度
  108. setTimeout(() => {
  109. uni.showLoading({
  110. title: `正在${title}第${index}个任务数据`,
  111. mask: true
  112. });
  113. }, 50);
  114. }
  115. export const moveFile = (oldPath : string) : Promise<string> => {
  116. return new Promise((resolve, reject) => {
  117. // 生成唯一的文件名
  118. const timestamp = Date.now();
  119. const randomNum = Math.floor(Math.random() * 1000);
  120. const fileExtension = oldPath.substring(oldPath.lastIndexOf('.'));
  121. const newImgName = `download_${timestamp}_${randomNum}${fileExtension}`;
  122. // 定义新的保存路径 - 不放在相册里,放在应用数据目录下的downloadImgs文件夹
  123. const destPath = `${uni.env.USER_DATA_PATH}/downloadImgs/${newImgName}`;
  124. try {
  125. // 确保目标目录存在
  126. const fs = uni.getFileSystemManager();
  127. try {
  128. fs.accessSync(`${uni.env.USER_DATA_PATH}/downloadImgs`);
  129. } catch (e) {
  130. // 目录不存在,创建目录
  131. fs.mkdirSync(`${uni.env.USER_DATA_PATH}/downloadImgs`, true);
  132. }
  133. // 先拷贝文件到新路径
  134. fs.copyFile({
  135. srcPath: oldPath,
  136. destPath: destPath,
  137. success: function () {
  138. // 删除原文件,释放空间
  139. fs.unlink({
  140. filePath: oldPath,
  141. success: function () {
  142. console.log('原文件已删除');
  143. },
  144. fail: function (unlinkErr) {
  145. console.error('删除原文件失败', unlinkErr);
  146. }
  147. });
  148. console.log('文件移动成功,新路径:', destPath);
  149. resolve(destPath);
  150. },
  151. fail: function (copyErr) {
  152. console.error('拷贝文件失败', copyErr);
  153. reject(copyErr);
  154. }
  155. });
  156. } catch (error) {
  157. console.error('文件移动过程发生错误', error);
  158. reject(error);
  159. }
  160. });
  161. }
  162. //声像任务下载
  163. export const downloadDataFromAPI = async (productCode : string, callback ?: () => void) : Promise<boolean> => {
  164. try {
  165. uni.showLoading({ title: '任务开始下载' });
  166. const apiToken = await getTokenFromApi();
  167. if (apiToken == null || apiToken == '') {
  168. uni.hideLoading();
  169. uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
  170. return false;
  171. }
  172. //校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖,
  173. //如果没有则直接保存。如果有存在已经在执行中的产品号,则提示已存在执行中的任务
  174. const infoJson = await getLatestTask(productCode, null);
  175. console.log(infoJson);
  176. if (infoJson?.['data'] != null) {
  177. let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
  178. // let ingNum = parseInt(info?.['pdid'] as string);
  179. //覆盖标识位
  180. let overwiteFlag = ref(false);
  181. // 先检查是否有任务正在执行中
  182. // if (info != null && ingNum > 0) {
  183. // uni.showToast({ title: `当前产品号已有任务在执行中!`, icon: 'error' });
  184. // return false;
  185. // }
  186. // 使用Promise来处理异步流程
  187. let deleteDataPromise = new Promise<boolean>((resolve) => {
  188. // if (info != null && ingNum == 0) {
  189. if (info != null) {
  190. //可以被覆盖,需要有提示框,给用户确认
  191. uni.showModal({
  192. title: '系统提示',
  193. content: '该工序编号已存在任务是否覆盖掉?',
  194. cancelText: '取消',
  195. confirmText: '确定',
  196. success: function (res) {
  197. if (res.confirm) {
  198. // 标记为需要覆盖
  199. overwiteFlag.value = true;
  200. // 执行删除数据操作
  201. removeTaskAndRecord(productCode).then((recordDelResponse) => {
  202. console.log('删除数据响应:', recordDelResponse);
  203. // 删除成功,解析Promise并允许继续执行
  204. // 确保模态框已完全关闭后再解析Promise
  205. setTimeout(() => {
  206. resolve(true);
  207. }, 300);
  208. }).catch((error) => {
  209. console.error('删除数据失败:', error);
  210. uni.showToast({ title: '删除旧数据失败', icon: 'error' });
  211. resolve(false);
  212. });
  213. } else {
  214. // 用户取消覆盖
  215. overwiteFlag.value = false;
  216. resolve(false);
  217. }
  218. }
  219. });
  220. } else {
  221. // 不需要显示确认框,直接解析Promise
  222. resolve(true);
  223. }
  224. });
  225. // 等待删除数据操作完成
  226. const canContinue : boolean = await deleteDataPromise;
  227. if (!canContinue) {
  228. // 如果不能继续(删除失败或用户取消),则终止函数执行
  229. return false;
  230. }
  231. }
  232. // 直接使用async/await处理HTTP请求,避免嵌套Promise
  233. const requestResult = await new Promise<UTSJSONObject>((resolve, reject) => {
  234. uni.request({
  235. url: `${globalConfig.host}${globalConfig.downloadTaskURL}${productCode}`,
  236. method: 'GET',
  237. header: {
  238. 'token': apiToken
  239. },
  240. success: (res) => resolve(res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject),
  241. fail: (err) => reject(err)
  242. });
  243. });
  244. // 处理请求结果
  245. if (requestResult != null && requestResult.code == 666) {
  246. let taskInfo = requestResult?.['data'] as UTSJSONObject ?? {} as UTSJSONObject;
  247. if (taskInfo != null) {
  248. let data = JSON.parse<AppTaskInfo>(taskInfo.toJSONString());
  249. if (data != null) {
  250. // 保存任务信息
  251. const resSave = await saveTaskInfo(taskInfo);
  252. const lastIdStr = resSave?.['lastId'] as string | null;
  253. const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;
  254. if (lastId != null) {
  255. let photoList = taskInfo?.['photolist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  256. if (photoList != null && photoList.length > 0) {
  257. const totalRecords = photoList.length;
  258. // 按顺序处理所有图片记录
  259. for (let i = 0; i < photoList.length; i++) {
  260. // 更新进度
  261. showProgress(i + 1, '下载');
  262. let photoObj = photoList[i] as UTSJSONObject;
  263. // 获取各个字段的值
  264. const pk = photoObj['pk'] as string;
  265. const photographpoint = photoObj['photographpoint'] as string | null;
  266. const photographdescription = photoObj['photographdescription'] as string | null;
  267. const photourl = photoObj['photourl'] as string | null;
  268. const photoname = photoObj['photoname'] as string | null;
  269. const fk_qcRecord = photoObj['fk_qcRecord'] as string | null;
  270. const fk_prodcode = photoObj['fk_prodcode'] as string | null;
  271. const prodno = photoObj['prodno'] as string | null;
  272. const fk_creator = photoObj['fk_creator'] as string | null;
  273. const fks_operator = photoObj['fks_operator'] as string | null;
  274. const operator = photoObj['operator'] as string | null;
  275. const processStep = photoObj['processStep'] as string | null;
  276. const fk_processTask = photoObj['fk_processTask'] as string | null;
  277. const cs = photoObj['cs'] as string | null;
  278. const ts = photoObj['ts'] as string | null;
  279. // 获取图片
  280. let imagePathsArr = [] as string[];
  281. try {
  282. // 串行执行,等待前一张图片处理完成再处理下一张
  283. const tempFilePath = await downloadAndSaveImage(pk, apiToken);
  284. if (tempFilePath != null && tempFilePath != '') {
  285. imagePathsArr.push(tempFilePath);
  286. }
  287. } catch (error) {
  288. console.error(`处理图片时出错:`, error);
  289. // 出错后继续处理下一张图片
  290. }
  291. // 拼接图片ID和路径
  292. const imagePaths = imagePathsArr.join(",");
  293. // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
  294. var values = `${lastId === null ? 0 : lastId},${pk === null ? '' : `'${pk}'`}, ${photographpoint === null ? 'null' : `'${photographpoint}'`}, ${photographdescription === null ? 'null' : `'${photographdescription}'`},${photourl === null ? 'null' : `'${photourl}'`},
  295. ${imagePaths === null ? 'null' : `'${imagePaths}'`},${photoname === null ? 'null' : `'${photoname}'`},${fk_qcRecord === null ? 'null' : `'${fk_qcRecord}'`},${fk_prodcode === null ? 'null' : `'${fk_prodcode}'`}, ${prodno === null ? 'null' : `'${prodno}'`},
  296. ${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}'`}`;
  297. // 立即保存当前图片的任务信息
  298. saveTaskPhoto(values);
  299. }
  300. //保存关键工序
  301. let keyProcessList = taskInfo?.['keyprocesslist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  302. if (keyProcessList != null && keyProcessList.length > 0) {
  303. keyProcessList.forEach(item =>{
  304. item['pdid'] = lastId;
  305. console.log(item);
  306. saveTaskKeyProcess(item);
  307. })
  308. }
  309. //保存检验记录
  310. let recordObj = taskInfo?.['qcrecord'] as UTSJSONObject ?? {} as UTSJSONObject;
  311. if (recordObj != null) {
  312. recordObj['pdid'] = lastId;
  313. const resSave = await saveTaskRecord(recordObj);
  314. const recordIdStr = resSave?.['lastId'] as string | null;
  315. const recordId = recordIdStr != null ? parseInt(recordIdStr) : null;
  316. if (recordId != null) {
  317. let recordItemList = recordObj?.['items'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  318. if (recordItemList != null && recordItemList.length > 0) {
  319. recordItemList.forEach(item =>{
  320. item['psxid'] = recordId;
  321. console.log(item);
  322. saveTaskRecordItem(item);
  323. })
  324. }
  325. }
  326. }
  327. }
  328. } else {
  329. console.log('保存媒体信息成功,但未获取到主键ID');
  330. }
  331. }
  332. }
  333. // 所有记录处理完成,显示完成提示
  334. uni.hideLoading();
  335. uni.showToast({ title: `下载完成`, icon: 'success' });
  336. if (callback != null) {
  337. callback();
  338. }
  339. return true;
  340. } else {
  341. const errorMsg = requestResult?.msg != null ? requestResult.msg : '未知错误';
  342. uni.hideLoading();
  343. uni.showToast({ title: `请求失败: ${errorMsg}`, icon: 'error' });
  344. return false;
  345. }
  346. } catch (error) {
  347. console.error('下载数据时发生错误:', error);
  348. uni.hideLoading();
  349. uni.showToast({ title: '下载失败,请重试', icon: 'error' });
  350. return false;
  351. }
  352. }
  353. // 辅助函数:下载并保存图片
  354. const downloadAndSaveImage = async (pk: string, apiToken: string): Promise<string> => {
  355. return new Promise<string>((resolve, reject) => {
  356. // 使用uni.downloadFile下载图片
  357. uni.downloadFile({
  358. url: `${globalConfig.getImgURL}${pk}`,
  359. header: {
  360. 'token': apiToken
  361. },
  362. success: (res) => {
  363. if (res.statusCode === 200) {
  364. // 等待一小段时间确保文件完全下载
  365. setTimeout(() => {
  366. moveFile(res.tempFilePath).then((newFilePath) => {
  367. console.log('图片已移动并添加到数组:', newFilePath);
  368. // 处理完成后等待1秒再处理下一张
  369. setTimeout(() => {
  370. resolve(newFilePath);
  371. }, 500);
  372. }).catch((error) => {
  373. console.error('文件移动失败,使用原始路径:', error);
  374. // 如果移动失败,使用原始路径作为备选
  375. // 处理完成后等待1秒再处理下一张
  376. setTimeout(() => {
  377. resolve(res.tempFilePath);
  378. }, 1000);
  379. });
  380. }, 500); // 等待500ms确保文件完全下载
  381. } else {
  382. console.error('下载图片失败,状态码:', res.statusCode);
  383. reject(new Error(`下载图片失败,状态码: ${res.statusCode}`));
  384. }
  385. },
  386. fail: (err) => {
  387. console.error('请求图片失败:', err);
  388. reject(err);
  389. }
  390. });
  391. });
  392. }
  393. //声像任务上传
  394. // export const uploadDataToAPI = async (productCode : string, callback ?: () => void) : Promise<boolean> => {
  395. // try {
  396. // //暂定需要上传的数据文件
  397. // //const infoJson = await getLatestRecord(productCode, null);
  398. // const apiToken = await getTokenFromApi();
  399. // if (apiToken == null || apiToken == '') {
  400. // uni.hideLoading();
  401. // uni.showToast({ title: `获取Token失败,请联系技术IT`, icon: 'error' });
  402. // return false
  403. // }
  404. // // 获取数据
  405. // 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);
  406. // let dataList = res?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  407. // console.log(dataList);
  408. // if (dataList == null || dataList.length === 0) {
  409. // uni.hideLoading();
  410. // uni.showToast({ title: '未获取到数据', icon: 'error' });
  411. // return false;
  412. // }
  413. // let doneRecordList = dataList.filter(item => item.getString("status") == '3');
  414. // console.log(doneRecordList);
  415. // if (doneRecordList.length === 0) {
  416. // uni.hideLoading();
  417. // uni.showToast({ title: '上传图片数据为空', icon: 'error' });
  418. // return false;
  419. // }
  420. // // 1. 收集所有需要上传的图片
  421. // let allImagesToUpload : UploadImg[] = [];
  422. // let processStep = 1;
  423. // for (let index = 0; index < dataList.length; index++) {
  424. // const record = dataList[index];
  425. // if (record.getString('urlpdt') == '' || record.getString('urlpdt') == null
  426. // || record.getString('pk') == '' || record.getString('pk') == null) {
  427. // continue
  428. // }
  429. // showProgress(processStep, '准备上传');
  430. // //收集图片信息
  431. // let urlpdtStr = record.getString('urlpdt');
  432. // let pk = record.getString('pk');
  433. // let urlArr = urlpdtStr?.split(",") ?? [];
  434. // for (let j = 0; j < urlArr.length; j++) {
  435. // let path = urlArr[j];
  436. // const fullFilePath = `${uni.env.USER_DATA_PATH}` + path;
  437. // allImagesToUpload.push({ pk: pk ?? '', path: fullFilePath });
  438. // }
  439. // processStep++;
  440. // }
  441. // // 2. 统计总图片数量
  442. // const totalImages = allImagesToUpload.length;
  443. // console.log(`总共需要上传${totalImages}张图片`);
  444. // if (totalImages === 0) {
  445. // uni.hideLoading();
  446. // uni.showToast({ title: '没有需要上传的图片', icon: 'none' });
  447. // return true;
  448. // }
  449. // // 3. 执行第一次上传
  450. // let failedImages : UploadImg[] = [];
  451. // let successCount = 0;
  452. // uni.showLoading({ title: `正在上传图片 (0/${totalImages})` });
  453. // for (let i = 0; i < allImagesToUpload.length; i++) {
  454. // const { pk, path } = allImagesToUpload[i];
  455. // console.log(`开始上传文件: ${path}, 索引: ${i}, apiToken: ${apiToken}, billid: ${pk}`);
  456. // try {
  457. // // 串行执行,等待前一个图片上传完成再处理下一张
  458. // await new Promise<void>((resolve, reject) => {
  459. // // 使用uni.uploadFile进行文件上传
  460. // console.log(`上传路径:${globalConfig.uploadURL}`)
  461. // const uploadTask = uni.uploadFile({
  462. // url: `${globalConfig.uploadURL}`,
  463. // filePath: path,
  464. // name: 'file', // 文件参数名
  465. // header: {
  466. // 'token': apiToken
  467. // },
  468. // formData: {
  469. // 'billid': pk
  470. // },
  471. // success: (uploadRes) => {
  472. // if (uploadRes.statusCode === 200) {
  473. // console.log(`文件${path}上传成功`, uploadRes);
  474. // // 解析响应数据
  475. // const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
  476. // console.log(resData)
  477. // if (resData?.['_id'] != null && resData?.['_id'] != '') {
  478. // successCount++;
  479. // // 等待一小段时间确保文件完全上传并处理完成
  480. // setTimeout(() => {
  481. // resolve();
  482. // }, 1000);
  483. // } else {
  484. // setTimeout(() => {
  485. // reject('响应数据无效');
  486. // }, 500);
  487. // }
  488. // } else {
  489. // console.error(`文件${path}上传失败,状态码:`, uploadRes.statusCode);
  490. // setTimeout(() => {
  491. // reject(new Error(`上传失败,状态码: ${uploadRes.statusCode}`));
  492. // }, 500);
  493. // }
  494. // },
  495. // fail: (err) => {
  496. // console.error(`文件${path}上传失败`, err);
  497. // // 上传失败也继续处理下一张,但记录错误
  498. // setTimeout(() => {
  499. // reject(err);
  500. // }, 500);
  501. // },
  502. // complete: () => {
  503. // // console.log(`文件${path}上传操作完成`);
  504. // // 更新进度
  505. // uni.hideLoading();
  506. // uni.showLoading({ title: `正在上传图片 (${i + 1}/${totalImages})` });
  507. // }
  508. // });
  509. // uploadTask.onProgressUpdate((res) => {
  510. // console.log('上传进度' + res.progress);
  511. // console.log('已经上传的数据长度' + res.totalBytesSent);
  512. // console.log('预期需要上传的数据总长度' + res.totalBytesExpectedToSend);
  513. // });
  514. // });
  515. // } catch (error) {
  516. // // 捕获上传失败的错误,将失败的图片信息添加到错误数组
  517. // console.log(`处理第${i + 1}张图片时出错:`, error);
  518. // failedImages.push({ pk, path });
  519. // // 出错后继续处理下一张图片
  520. // }
  521. // // 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
  522. // if (i < allImagesToUpload.length - 1) {
  523. // await new Promise<void>((resolve) => {
  524. // setTimeout(() => {
  525. // resolve()
  526. // }, 1000)
  527. // })
  528. // }
  529. // }
  530. // // 4. 执行重试逻辑(最多3次)
  531. // const maxRetries = 3;
  532. // let retryImages = [...failedImages]; // 复制初始失败的图片数组
  533. // for (let retryCount = 1; retryCount <= maxRetries; retryCount++) {
  534. // if (retryImages.length === 0) {
  535. // // 如果没有需要重试的图片,提前结束循环
  536. // break;
  537. // }
  538. // console.log(`开始第${retryCount}次重试上传失败的图片,共${retryImages.length}张`);
  539. // uni.hideLoading();
  540. // uni.showLoading({ title: `第${retryCount}次重试 (0/${retryImages.length})` });
  541. // // 创建新的错误数组,用于收集本次重试失败的图片
  542. // let currentFailedImages : UploadImg[] = [];
  543. // let currentSuccessCount = 0;
  544. // // 串行上传失败的图片
  545. // for (let i = 0; i < retryImages.length; i++) {
  546. // const { pk, path } = retryImages[i];
  547. // console.log(`重试上传文件: ${path}, 索引: ${i}, 重试次数: ${retryCount}, apiToken: ${apiToken}, billid: ${pk}`);
  548. // console.log(`重试上传路径:${globalConfig.uploadURL}`)
  549. // await new Promise<void>((resolve) => {
  550. // uni.uploadFile({
  551. // url: `${globalConfig.uploadURL}`,
  552. // filePath: path,
  553. // name: 'file',
  554. // header: {
  555. // 'token': apiToken
  556. // },
  557. // formData: {
  558. // 'billid': pk
  559. // },
  560. // success: (uploadRes) => {
  561. // if (uploadRes.statusCode === 200) {
  562. // console.log(`重试文件${path}上传成功`, uploadRes);
  563. // const resData = JSON.parse(uploadRes.data) as UTSJSONObject;
  564. // if (resData?.['_id'] != null && resData?.['_id'] != '') {
  565. // currentSuccessCount++;
  566. // } else {
  567. // currentFailedImages.push({ pk, path });
  568. // }
  569. // } else {
  570. // currentFailedImages.push({ pk, path });
  571. // }
  572. // console.log(`重试上传完成,当前成功: ${currentSuccessCount}, 当前失败: ${currentFailedImages.length}`);
  573. // resolve();
  574. // },
  575. // fail: (err) => {
  576. // console.error(`重试文件${path}上传失败`, err);
  577. // currentFailedImages.push({ pk, path });
  578. // resolve();
  579. // },
  580. // complete: () => {
  581. // // console.log(`重试文件${path}上传操作完成`);
  582. // // 更新进度
  583. // uni.hideLoading();
  584. // uni.showLoading({ title: `第${retryCount}次重试 (${i + 1}/${retryImages.length})` });
  585. // }
  586. // });
  587. // });
  588. // // 在两次上传之间增加一个短暂的延迟,避免请求过于频繁
  589. // if (i < retryImages.length - 1) {
  590. // await new Promise<void>((resolve) => {
  591. // setTimeout(() => {
  592. // resolve()
  593. // }, 1000)
  594. // })
  595. // }
  596. // }
  597. // // 更新成功数量
  598. // successCount += currentSuccessCount;
  599. // // 更新下一次重试的图片列表为本次失败的图片
  600. // retryImages = currentFailedImages;
  601. // }
  602. // // 5. 显示总结信息
  603. // const finalFailedCount = retryImages.length;
  604. // console.log(`上传总结: 总共${totalImages}张图片, 成功${successCount}张, 失败${finalFailedCount}张`);
  605. // uni.hideLoading();
  606. // // 三次重试后如果仍有失败的图片,显示提示
  607. // if (finalFailedCount > 0) {
  608. // uni.showModal({
  609. // title: '上传提示',
  610. // content: `总共需要上传${totalImages}张图片,成功${successCount}张,失败${finalFailedCount}张。\n经过${maxRetries}次重试后,仍有${finalFailedCount}张图片上传失败,请检查网络后重新上传。`,
  611. // showCancel: false
  612. // });
  613. // } else {
  614. // uni.showToast({
  615. // title: `上传完成!共${totalImages}张图片,全部成功。`,
  616. // icon: 'success'
  617. // });
  618. // }
  619. // if (callback != null) {
  620. // callback();
  621. // }
  622. // if (finalFailedCount === 0) {
  623. // let updatedData = " uploadflag = 1 "
  624. // updateData('app_media_info', updatedData, 'productno', productCode).then((res : UTSJSONObject) => {
  625. // console.log(`更新完上传标识 ${productCode}`)
  626. // });
  627. // }
  628. // return finalFailedCount === 0;
  629. // } catch (error) {
  630. // console.error(error);
  631. // uni.showToast({ title: '上传失败,请重试', icon: 'error' });
  632. // uni.hideLoading();
  633. // return false;
  634. // }
  635. // }