dataProcessor.uts 5.0 KB

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