dataProcessor.uts 8.5 KB

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