dataProcessor.uts 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { getToken, getTokenFromApi } from './auth'
  2. import { saveMediaInfo, saveMediaRecord, getLatestRecord, removeInfoAndRecord } from '@/api/work'
  3. // 类型定义保持不变
  4. export type ApiResponse = {
  5. code : number;
  6. msg : string;
  7. data : MediaInfoData[];
  8. }
  9. export type MediaInfoData = {
  10. pk : string;
  11. workorder ?: string;
  12. invname ?: string;
  13. productcode ?: string;
  14. cardno ?: string;
  15. model ?: string;
  16. graphid ?: string;
  17. ver ?: string;
  18. phase ?: string;
  19. processno ?: string;
  20. createtime ?: string;
  21. createuser ?: string;
  22. updatetime ?: string;
  23. updateuser ?: string;
  24. qmImageTaskClist ?: MediaRecordData[];
  25. }
  26. export type MediaRecordData = {
  27. senum ?: string;
  28. photoitem ?: string;
  29. part ?: string;
  30. partno ?: string;
  31. descb ?: string;
  32. num ?: number;
  33. urlspl ?: string;
  34. imgname ?: string;
  35. urlpdt ?: string;
  36. createtime ?: string;
  37. createuser ?: string;
  38. updatetime ?: string;
  39. updateuser ?: string;
  40. }
  41. export const downloadDataFromAPI = async (productCode: string, callback ?: () => void) : Promise<boolean> => {
  42. try {
  43. //校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖,
  44. //如果没有则直接保存。如果有存在已经在执行的产品号,则提示已存在执行中的任务
  45. const infoJson = await getLatestRecord(productCode, null);
  46. let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
  47. let ingNum = parseInt(info?.['statusRecordCount'] as string);
  48. //覆盖标识位
  49. let overwiteFlag = ref(false);
  50. // 先检查是否有任务正在执行中
  51. if (info != null && ingNum > 0) {
  52. uni.showToast({ title: `当前产品号已有任务在执行中!`, icon: 'error' });
  53. return false;
  54. }
  55. // 使用Promise来处理异步流程
  56. let deleteDataPromise = new Promise<boolean>((resolve) => {
  57. if(info != null && ingNum == 0) {
  58. //可以被覆盖,需要有提示框,给用户确认
  59. uni.showModal({
  60. title: '系统提示',
  61. content: '该产品号已存在任务是否覆盖掉?',
  62. cancelText: '取消',
  63. confirmText: '确定',
  64. success: function (res) {
  65. if (res.confirm) {
  66. // 标记为需要覆盖
  67. overwiteFlag.value = true;
  68. // 执行删除数据操作
  69. let pid = info?.['pdid'] as string;
  70. removeInfoAndRecord(pid).then((recordDelResponse) => {
  71. console.log('删除数据响应:', recordDelResponse);
  72. // 删除成功,解析Promise并允许继续执行
  73. // 确保模态框已完全关闭后再解析Promise
  74. setTimeout(() => {
  75. resolve(true);
  76. }, 300);
  77. }).catch((error) => {
  78. console.error('删除数据失败:', error);
  79. uni.showToast({ title: '删除旧数据失败', icon: 'error' });
  80. resolve(false);
  81. });
  82. } else {
  83. // 用户取消覆盖
  84. overwiteFlag.value = false;
  85. resolve(false);
  86. }
  87. }
  88. });
  89. } else {
  90. // 不需要显示确认框,直接解析Promise
  91. resolve(true);
  92. }
  93. });
  94. // 等待删除数据操作完成
  95. const canContinue: boolean = await deleteDataPromise;
  96. if (!canContinue) {
  97. // 如果不能继续(删除失败或用户取消),则终止函数执行
  98. return false;
  99. }
  100. // 继续执行后续代码...
  101. const apiToken = await getTokenFromApi();
  102. uni.showLoading({ title: '数据下载中...' });
  103. // 使用Promise处理HTTP请求,避免嵌套Promise
  104. return new Promise<boolean>((resolve) => {
  105. uni.request({
  106. url: `http://192.168.43.62:4523/m1/7190626-6915798-default/loadQmImagetask?prodcode=${productCode}`,
  107. method: 'GET',
  108. header: {
  109. 'token': apiToken
  110. },
  111. success: (res) => {
  112. let singleObject = res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject;
  113. if (singleObject != null && singleObject.code == 666) {
  114. let mediaInfoList = singleObject?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  115. if (mediaInfoList != null && mediaInfoList.length > 0) {
  116. mediaInfoList.forEach(item => {
  117. if (item != null) {
  118. let data = JSON.parse<MediaInfoData>(item.toJSONString());
  119. if (data != null) {
  120. saveMediaInfo(item).then((resSave : UTSJSONObject) => {
  121. const lastIdStr = resSave?.['lastId'] as string | null;
  122. const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;
  123. if (lastId != null) {
  124. let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array<UTSJSONObject>();
  125. if (recordList != null && recordList.length > 0) {
  126. for(var i =0; i< recordList.length; i++) {
  127. const record: MediaRecordData = recordList[i] as MediaRecordData;
  128. // 获取各个字段的值
  129. const senum = record.senum;
  130. const photoitem = record.photoitem;
  131. const part = record.part;
  132. const partno = record.partno;
  133. const descb = record.descb;
  134. const num = record.num;
  135. const urlspl = record.urlspl;
  136. const imgname = record.imgname;
  137. const urlpdt = record.urlpdt;
  138. const createtime = record.createtime;
  139. const createuser = record.createuser;
  140. const updatetime = record.updatetime;
  141. const updateuser = record.updateuser;
  142. // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
  143. var values = `${senum === null ? '1' : `'${senum}'`}, ${photoitem === null ? 'null' : `'${photoitem}'`}, ${data?.['productcode'] === null ? 'null' : `'${data?.['productcode']}'`},${part === null ? 'null' : `'${part}'`},${partno === null ? 'null' : `'${partno}'`},${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}`;
  144. saveMediaRecord(values);
  145. }
  146. // 延迟显示完成提示,确保所有数据保存完成
  147. setTimeout(() => {
  148. uni.showToast({ title: `下载完成`, icon: 'success' });
  149. }, 500);
  150. }
  151. } else {
  152. console.log('保存媒体信息成功,但未获取到主键ID');
  153. }
  154. })
  155. }
  156. }
  157. });
  158. }
  159. } else {
  160. const errorMsg = singleObject.msg != null ? singleObject.msg : '未知错误';
  161. uni.showToast({ title: `请求失败: ${errorMsg}`, icon: 'error' });
  162. }
  163. resolve(true);
  164. },
  165. fail: (err) => {
  166. console.error('请求失败:', err);
  167. uni.showToast({ title: `请求失败: ${err.errMsg}`, icon: 'error' });
  168. resolve(false);
  169. },
  170. complete: () => {
  171. console.log('请求完成');
  172. uni.hideLoading();
  173. }
  174. });
  175. });
  176. } catch (error) {
  177. console.error('下载数据总异常:', error);
  178. uni.showToast({ title: '下载失败,请重试', icon: 'error' });
  179. uni.hideLoading();
  180. return false;
  181. }
  182. }