import { getToken, getTokenFromApi } from './auth' import { saveMediaInfo, saveMediaRecord } from '@/api/work' // 类型定义保持不变 export type ApiResponse = { code : number; msg : string; data : MediaInfoData[]; } export type MediaInfoData = { pk : string; workorder ?: string; invname ?: string; productcode ?: string; cardno ?: string; model ?: string; graphid ?: string; ver ?: string; phase ?: string; processno ?: string; createtime ?: string; createuser ?: string; updatetime ?: string; updateuser ?: string; qmImageTaskClist ?: MediaRecordData[]; } export type MediaRecordData = { senum ?: string; photoitem ?: string; part ?: string; partno ?: string; descb ?: string; num ?: number; urlspl ?: string; imgname ?: string; urlpdt ?: string; createtime ?: string; createuser ?: string; updatetime ?: string; updateuser ?: string; } export const downloadDataFromAPI = async (callback ?: () => void) : Promise => { try { const apiToken = await getTokenFromApi(); uni.showLoading({ title: '数据下载中...' }) return new Promise((resolve) => { //http://127.0.0.1:4523/m1/7190626-6915798-default/m1/download uni.request({ url: 'http://192.168.43.62:4523/m1/7190626-6915798-default/loadQmImagetask?prodcode=YH07202507000005', method: 'GET', header: { 'token': apiToken }, success: (res) => { console.log('请求成功:', res); console.log('token', apiToken); console.log(res.data); let singleObject = res?.['data'] as UTSJSONObject ?? {} as UTSJSONObject if (singleObject != null && singleObject.code == 666) { let mediaInfoList = singleObject?.['data'] as UTSJSONObject[] ?? Array() if (mediaInfoList != null && mediaInfoList.length > 0) { mediaInfoList.forEach(item => { if (item != null) { let data = JSON.parse(item.toJSONString()); if (data != null) { saveMediaInfo(item).then((res : UTSJSONObject) => { const lastIdStr = res?.['lastId'] as string | null; const lastId = lastIdStr != null ? parseInt(lastIdStr) : null; if (lastId != null) { let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array() console.log(recordList) if (recordList != null && recordList.length > 0) { for(var i =0; i< recordList.length; i++) { // 将recordList[i]断言为MediaRecordData类型 const record: MediaRecordData = recordList[i] as MediaRecordData; console.log('完整对象:', record); // 获取各个字段的值 const senum = record.senum; // string类型 const photoitem = record.photoitem; // string类型 const part = record.part; // string类型 const partno = record.partno; // string类型 const descb = record.descb; // string类型 const num = record.num; // number类型 const urlspl = record.urlspl; // string类型 const imgname = record.imgname; // string类型 const urlpdt = record.urlpdt; // string类型 const createtime = record.createtime; // string类型 const createuser = record.createuser; // string类型 const updatetime = record.updatetime; // string类型 const updateuser = record.updateuser; // string类型 //senum,photoitem,productno,part,partno,descb,num,status,date,urlspl,imgname,urlpdt,createtime,createuser,updatetime,updateuser,pid // 使用三目运算符判断,当值为null时直接插入null,否则用单引号括起来 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}` saveMediaRecord(values) uni.showToast({ title: `下载完成`, icon: 'success' }); } } } else { console.log('保存媒体信息成功,但未获取到主键ID'); } }) } } }); } } else { uni.showToast({ title: `请求失败: ${singleObject.msg}`, icon: 'error' }); } resolve(true); }, fail: (err) => { console.error('请求失败:', err); uni.showToast({ title: `请求失败: ${err.errMsg}`, icon: 'error' }); resolve(false); }, complete: () => { console.log('请求完成'); uni.hideLoading(); } }); }); } catch (error) { console.error('下载数据总异常:', error); uni.showToast({ title: '下载失败,请重试', icon: 'error' }); uni.hideLoading(); return false; } }