import { getToken, getTokenFromApi } from './auth' import { saveMediaInfo, saveMediaRecord, getLatestRecord, removeInfoAndRecord } 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 (productCode: string, callback ?: () => void) : Promise => { try { //校验是否已经存在未执行的产品号,若已经存在则提示用户该产品号是否需要被覆盖, //如果没有则直接保存。如果有存在已经在执行的产品号,则提示已存在执行中的任务 const infoJson = await getLatestRecord(productCode, null); let info = infoJson?.['data'] as UTSJSONObject ?? {} as UTSJSONObject let ingNum = parseInt(info?.['statusRecordCount'] as string); //覆盖标识位 let overwiteFlag = ref(false); // 先检查是否有任务正在执行中 if (info != null && ingNum > 0) { uni.showToast({ title: `当前产品号已有任务在执行中!`, icon: 'error' }); return false; } // 使用Promise来处理异步流程 let deleteDataPromise = new Promise((resolve) => { if(info != null && ingNum == 0) { //可以被覆盖,需要有提示框,给用户确认 uni.showModal({ title: '系统提示', content: '该产品号已存在任务是否覆盖掉?', cancelText: '取消', confirmText: '确定', success: function (res) { if (res.confirm) { // 标记为需要覆盖 overwiteFlag.value = true; // 执行删除数据操作 let pid = info?.['pdid'] as string; removeInfoAndRecord(pid).then((recordDelResponse) => { console.log('删除数据响应:', recordDelResponse); // 删除成功,解析Promise并允许继续执行 // 确保模态框已完全关闭后再解析Promise setTimeout(() => { resolve(true); }, 300); }).catch((error) => { console.error('删除数据失败:', error); uni.showToast({ title: '删除旧数据失败', icon: 'error' }); resolve(false); }); } else { // 用户取消覆盖 overwiteFlag.value = false; resolve(false); } } }); } else { // 不需要显示确认框,直接解析Promise resolve(true); } }); // 等待删除数据操作完成 const canContinue: boolean = await deleteDataPromise; if (!canContinue) { // 如果不能继续(删除失败或用户取消),则终止函数执行 return false; } // 继续执行后续代码... const apiToken = await getTokenFromApi(); uni.showLoading({ title: '数据下载中...' }); // 使用Promise处理HTTP请求,避免嵌套Promise return new Promise((resolve) => { uni.request({ url: `http://192.168.43.62:4523/m1/7190626-6915798-default/loadQmImagetask?prodcode=${productCode}`, method: 'GET', header: { 'token': apiToken }, success: (res) => { 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((resSave : UTSJSONObject) => { const lastIdStr = resSave?.['lastId'] as string | null; const lastId = lastIdStr != null ? parseInt(lastIdStr) : null; if (lastId != null) { let recordList = data?.['qmImageTaskClist'] as UTSJSONObject[] ?? Array(); if (recordList != null && recordList.length > 0) { for(var i =0; i< recordList.length; i++) { const record: MediaRecordData = recordList[i] as MediaRecordData; // 获取各个字段的值 const senum = record.senum; const photoitem = record.photoitem; const part = record.part; const partno = record.partno; const descb = record.descb; const num = record.num; const urlspl = record.urlspl; const imgname = record.imgname; const urlpdt = record.urlpdt; const createtime = record.createtime; const createuser = record.createuser; const updatetime = record.updatetime; const updateuser = record.updateuser; // 使用三目运算符判断,当值为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); } // 延迟显示完成提示,确保所有数据保存完成 setTimeout(() => { uni.showToast({ title: `下载完成`, icon: 'success' }); }, 500); } } else { console.log('保存媒体信息成功,但未获取到主键ID'); } }) } } }); } } else { const errorMsg = singleObject.msg != null ? singleObject.msg : '未知错误'; uni.showToast({ title: `请求失败: ${errorMsg}`, 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; } }