dataProcessor.uts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { getToken, getTokenFromApi } from './auth'
  2. import { saveMediaInfo, saveMediaRecord } 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 (callback ?: () => void) : Promise<boolean> => {
  42. try {
  43. const apiToken = await getTokenFromApi();
  44. uni.showLoading({ title: '数据下载中...' })
  45. return new Promise<boolean>((resolve) => {
  46. //http://127.0.0.1:4523/m1/7190626-6915798-default/m1/download
  47. uni.request({
  48. url: 'http://192.168.43.62:4523/m1/7190626-6915798-default/loadQmImagetask?prodcode=YH07202507000005',
  49. method: 'GET',
  50. header: {
  51. 'token': apiToken
  52. },
  53. success: (res) => {
  54. console.log('请求成功:', res);
  55. console.log('token', apiToken);
  56. console.log(res.data);
  57. let singleObject = res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject
  58. if (singleObject != null && singleObject.code == 666) {
  59. let mediaInfoList = singleObject?.['data'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  60. if (mediaInfoList != null && mediaInfoList.length > 0) {
  61. mediaInfoList.forEach(item => {
  62. if (item != null) {
  63. let data = JSON.parse<MediaInfoData>(item.toJSONString());
  64. if (data != null) {
  65. saveMediaInfo(item).then((res : UTSJSONObject) => {
  66. const lastIdStr = res?.['lastId'] as string | null;
  67. const lastId = lastIdStr != null ? parseInt(lastIdStr) : null;
  68. if (lastId != null) {
  69. let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array<UTSJSONObject>()
  70. console.log(recordList)
  71. if (recordList != null && recordList.length > 0) {
  72. for(var i =0; i< recordList.length; i++) {
  73. // 将recordList[i]断言为MediaRecordData类型
  74. const record: MediaRecordData = recordList[i] as MediaRecordData;
  75. console.log('完整对象:', record);
  76. // 获取各个字段的值
  77. const senum = record.senum; // string类型
  78. const photoitem = record.photoitem; // string类型
  79. const part = record.part; // string类型
  80. const partno = record.partno; // string类型
  81. const descb = record.descb; // string类型
  82. const num = record.num; // number类型
  83. const urlspl = record.urlspl; // string类型
  84. const imgname = record.imgname; // string类型
  85. const urlpdt = record.urlpdt; // string类型
  86. const createtime = record.createtime; // string类型
  87. const createuser = record.createuser; // string类型
  88. const updatetime = record.updatetime; // string类型
  89. const updateuser = record.updateuser; // string类型
  90. //senum,photoitem,productno,part,partno,descb,num,status,date,urlspl,imgname,urlpdt,createtime,createuser,updatetime,updateuser,pid
  91. // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来
  92. 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}`
  93. saveMediaRecord(values)
  94. uni.showToast({ title: `下载完成`, icon: 'success' });
  95. }
  96. }
  97. } else {
  98. console.log('保存媒体信息成功,但未获取到主键ID');
  99. }
  100. })
  101. }
  102. }
  103. });
  104. }
  105. } else {
  106. uni.showToast({ title: `请求失败: ${singleObject.msg}`, icon: 'error' });
  107. }
  108. resolve(true);
  109. },
  110. fail: (err) => {
  111. console.error('请求失败:', err);
  112. uni.showToast({ title: `请求失败: ${err.errMsg}`, icon: 'error' });
  113. resolve(false);
  114. },
  115. complete: () => {
  116. console.log('请求完成');
  117. uni.hideLoading();
  118. }
  119. });
  120. });
  121. } catch (error) {
  122. console.error('下载数据总异常:', error);
  123. uni.showToast({ title: '下载失败,请重试', icon: 'error' });
  124. uni.hideLoading();
  125. return false;
  126. }
  127. }