dataProcessor.uts 7.7 KB

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