吉祥物缓存,替换安卓弹窗
This commit is contained in:
+1
-1
@@ -140,7 +140,7 @@ export const queryCurrentUser = (data) => request({
|
||||
// 生成唯一文件名
|
||||
const generateUniqueFilename = (pathName) => {
|
||||
if (!pathName || typeof pathName !== 'string') {
|
||||
throw new Error('无效的头像路径');
|
||||
throw new Error('无效的路径');
|
||||
}
|
||||
|
||||
// 直接使用原始文件名(带扩展名)
|
||||
|
||||
+41
-14
@@ -42,6 +42,17 @@ export const setTraMascotInfo = (data) => {
|
||||
};
|
||||
|
||||
// 查询当前已经选择的吉祥物
|
||||
// 检查文件是否存在
|
||||
const checkFileExists = (filePath) => {
|
||||
return new Promise((resolve) => {
|
||||
uni.getFileInfo({
|
||||
filePath,
|
||||
success: () => resolve(true),
|
||||
fail: () => resolve(false)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询当前已经选择的吉祥物
|
||||
* 出参:
|
||||
@@ -64,21 +75,37 @@ export const queryCurrentValidMascotInfo = (data) => {
|
||||
body
|
||||
}) => {
|
||||
if (body.imgAddr) {
|
||||
// setTimeout(() => {
|
||||
// const base_url = get_base_url()
|
||||
// const url = base_url + body.imgAddr
|
||||
// const img = new Image(); // 创建Image对象
|
||||
// img.onload = () => {
|
||||
// console.log(`图片缓存成功: ${url}`);
|
||||
// };
|
||||
// // 图片加载失败
|
||||
// img.onerror = () => {
|
||||
// console.error(`图片缓存失败: ${url}`);
|
||||
// };
|
||||
// img.src = url; // 开始加载图片(设置src触发请求,浏览器会自动缓存)
|
||||
// })
|
||||
console.log('body.imgAddrmascotNo');
|
||||
const base_url = get_base_url()
|
||||
const pathName = body.imgAddr
|
||||
const fileUrl = base_url + pathName
|
||||
const localFilePath = '_doc/mascot/' + body.mascotNo + '.png'
|
||||
console.log('fileUrl', fileUrl);
|
||||
// 将 Promise 回调函数声明为 async
|
||||
return new Promise(async (resolve, reject) => {
|
||||
// 现在可以在这里使用 await 了
|
||||
const exists = await checkFileExists(localFilePath)
|
||||
if (exists) {
|
||||
console.log('已存在,无需下载')
|
||||
resolve({...body, imgAddr:localFilePath})
|
||||
} else {
|
||||
console.log('开始下载吉祥物:', fileUrl)
|
||||
const dtask = plus.downloader.createDownload(fileUrl, {
|
||||
filename: localFilePath
|
||||
}, (d, status) => {
|
||||
if (status === 200) {
|
||||
console.log("保存路径:", d.filename)
|
||||
resolve({...body, imgAddr:d.filename})
|
||||
} else {
|
||||
console.error("文件下载失败:", status)
|
||||
reject(new Error(`下载失败,状态码: ${status}`))
|
||||
}
|
||||
})
|
||||
|
||||
dtask.start()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
return body
|
||||
})
|
||||
};
|
||||
Reference in New Issue
Block a user