/**
* 文件名缓存
*/
let _FILE_CACHE_KEY = 'DEFAULT_FILE_CACHE';
let _FILE_MAP_ = null;
let _DOWNLOAD_MAP_ = new Map();
/**
* 获取缓存数据
*/
const _getStorage = () => {
let result = uni.getStorageSync(_FILE_CACHE_KEY);
if (!result) {
uni.setStorage({
key: _FILE_CACHE_KEY,
data: []
})
result = [];
}
return result;
}
const isEmpty = (str) => {
return str === null || str === undefined || !str
}
/**
* 初始化
* key--String--本地缓存key,分成了图片,文件,等等
* expiration--Number--过期时间,touchDate距今expiration+1天,就删除
*/
const init = ({
key,
expiration
}) => {
// 设置key
if (!isEmpty(key) && typeof key === 'string') {
_FILE_CACHE_KEY = key;
}
// 获取缓存数据
// let array = _getStorage();
let array = [];
// 根据时间判断排序, 删除不再使用的数据
orderAndFilterFileList(array, expiration);
// 获取文件id,文件filePath,map
_FILE_MAP_ = getFileMap(array);
console.log('_FILE_MAP_', _FILE_MAP_)
return _FILE_MAP_;
}
/**
* 获取fileId,filePath对应map
* @param {Array} fileList
*/
const getFileMap = (fileList) => {
let map = new Map();
fileList.forEach((fileItem) => {
map.set(fileItem.fileId, fileItem.filePath);
})
return map;
}
/**
* 根据时间判断排序, 删除不再使用的数据
*/
const orderAndFilterFileList = (fileList, expiration) => {
let index = -1;
if (fileList && Array.isArray(fileList)) {
fileList.sort((a, b) => {
return b.touchDate - a.touchDate;
})
let boundaryDate = new Date().setHours(24, 0, 0, 0) - expiration * 24 * 60 * 60 * 1000;
for (var i = 0; i < fileList.length; i++) {
let element = fileList[i];
let touchDate = element.touchDate;
if (touchDate <= boundaryDate) {
index = i;
}
}
}
if (index > 0) {
fileList = fileList.slice(0, index);
let discardList = fileList.slice(index);
discardList.forEach(fileItem => {
uni.removeSavedFile({
filePath: fileItem.filePath
})
})
}
}
/**
* 缓存中新增文件
*/
const addFile = (fileId, savedFilePath) => {
uni.getStorage({
key: _FILE_CACHE_KEY,
complete: (res) => {
let array = [];
if (res.data) {
array = res.data;
}
array.push({
fileId,
filePath: savedFilePath,
touchDate: new Date().getTime(),
})
uni.setStorage({
key: _FILE_CACHE_KEY,
data: array,
success: (res) => {
}
})
}
})
}
/**
* 摸一下文件,更新文件缓存的touchDate
* @param {String} fileId
*/
const touchFile = (fileId) => {
if (_FILE_MAP_.get(fileId)) {
uni.getStorage({
key: _FILE_CACHE_KEY,
success: (res) => {
let array = [];
if (res.data) {
array = res.data;
}
array.map((item) => {
if (item.fileId === fileId) {
item.touchDate = new Date().getTime();
}
return item;
})
uni.setStorage({
key: _FILE_CACHE_KEY,
data: array
})
}
})
} else {
throw Error(`无法touchFile,找不到fileId=${fileId}对应的文件`);
}
}
/**
*
*/
const saveFile = (fileId, fileUrl, startDownload) => {
return new Promise((resolve, reject) => {
if (_FILE_MAP_.get(fileId)) {
touchFile(fileId);
resolve(_FILE_MAP_.get(fileId));
} else {
let arr = _DOWNLOAD_MAP_.get(fileId);
if (!arr) {
_DOWNLOAD_MAP_.set(fileId, []);
console.log('_DOWNLOAD_MAP_', _DOWNLOAD_MAP_)
let downloadTask = uni.downloadFile({
url: fileUrl,
complete(data) {
if (data && data.statusCode === 200) {
// #ifdef APP-PLUS
uni.saveFile({
tempFilePath: data.tempFilePath,
success(res) {
_FILE_MAP_.set(fileId, res.savedFilePath);
addFile(fileId, res.savedFilePath);
fireEvents(fileId, data.tempFilePath);
resolve(res.savedFilePath);
},
fail(err) {
reject(err);
}
})
// #endif
// #ifdef H5
_FILE_MAP_.set(fileId, data.tempFilePath);
fireEvents(fileId, data.tempFilePath);
resolve(data.tempFilePath);
// #endif
} else {
fireEvents(fileId, null);
reject(data);
}
}
})
} else {
// 同时多个下载,保存到map
arr.push({
resolve,
reject
});
}
startDownload && startDownload(downloadTask);
}
})
}
/**
* 同时多个下载,成功event触发
*/
const fireEvents = (fileId, filePath) => {
let arr = _DOWNLOAD_MAP_.get(fileId);
if (arr && arr.length) {
arr.forEach((item) => {
if (filePath) {
item.resolve && item.resolve(filePath);
} else {
item.reject && item.reject();
}
})
_DOWNLOAD_MAP_.delete(fileId);
}
}
/**
* 获取文件缓存路径
*/
const getCacheFilePath = (fileId) => {
return _FILE_MAP_.get(fileId)
}
export default {
init,
saveFile,
touchFile,
getCacheFilePath
}