You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
82 lines
3.1 KiB
82 lines
3.1 KiB
import base64 from '@/utils/base64.js'; |
|
export function getImageCache(filePath,fileMd51) { |
|
//console.log('caoxx') |
|
// #ifdef APP-PLUS |
|
return new Promise((resolve, reject) => { |
|
//console.log(filePath); |
|
let fileMd5 = ""; |
|
try{ |
|
fileMd5 = Base64.encode(filePath); |
|
//console.log(fileMd5); |
|
}catch(e){ |
|
fileMd5 = filePath; |
|
} |
|
|
|
// 图片缓存key值 |
|
let storageKey = 'IMAGE_CACHE_INFO_' + fileMd5 |
|
// 首先获取本地存储的数据,查询是否有对应文件路径,如果有缓存内容,直接返回 |
|
const cacheFileInfo = uni.getStorageSync(storageKey) |
|
if (cacheFileInfo) { |
|
//console.log("已缓存为:" + cacheFileInfo) |
|
resolve(cacheFileInfo) |
|
return; |
|
//return cacheFileInfo |
|
} else { |
|
//console.log("未缓存,进行下载保存") |
|
// 如果没有,执行下载,并存储起来后 |
|
uni.downloadFile({ |
|
url: filePath, |
|
success: (res) => { |
|
if (res.statusCode === 200) { |
|
//console.log('下载成功',filePath,res); |
|
// 再进行本地保存 |
|
|
|
uni.saveFile({ |
|
tempFilePath: res.tempFilePath, |
|
success: function(res2) { |
|
//console.log(res2) |
|
var t0 = plus.io.convertLocalFileSystemURL(res2.savedFilePath); |
|
var t0 = res2.savedFilePath // plus.io.convertLocalFileSystemURL(res2.savedFilePath); |
|
//var t0 = 'file://'+ plus.io.convertLocalFileSystemURL(res2.savedFilePath); |
|
//console.log(t0); |
|
//t0 = plus.io.convertAbsoluteFileSystem(res2.savedFilePath); |
|
//console.log(t0); |
|
//t0 = t0 + "test" |
|
uni.setStorageSync(storageKey, t0) |
|
resolve(t0) |
|
//return res2.savedFilePath |
|
return; |
|
}, |
|
fail: function(res2) { |
|
resolve(filePath); |
|
//return filePath |
|
return; |
|
} |
|
}) |
|
|
|
} else { |
|
console.log('下载临时文件失败') |
|
resolve(filePath); |
|
//return filePath |
|
return; |
|
} |
|
}, |
|
fail: (res) => { |
|
console.log(res) |
|
resolve(filePath); |
|
//return filePath |
|
return; |
|
} |
|
}) |
|
} |
|
}) |
|
// #endif |
|
// #ifndef APP-PLUS |
|
return new Promise((resolve, reject) => { |
|
//reject({ |
|
// message: '请在App中使用' |
|
//}) |
|
resolve(filePath); |
|
}) |
|
// #endif |
|
} |