uni-app 图片临时存本地

目前清空缓存的方法有问题

// 针对微信小程序   为借口获取的图片进行 缓存 
// 注意:需要时刻关注缓存大小 最大 7M ;
// 1.获取网络图片 不存在缓存  保存缓存   2.存在缓存 需要判断本地图片是否存在 ,存在使用本地,不存在使用网络
var storageSize = 0; //用于记录缓存大小 
const storageSizeMax = 6; //最大图片缓存 单位 m
//图片缓存名称  zdy-processing-img  
// 获取本地缓存的大小  默认  单位M
function getStorageSize() {
	uni.getStorageInfo({
		success(res) {
			let size = res.currentSize;
			storageSize = Math.floor(size / 1024 / 1024 * 100) / 100;
			// console.log(storageSize, 'm');
		}
	})
}
// 清理缓存
export function clearStorage() {
	console.log('清空缓存');
	uni.removeStorageSync('zdy-processing-img');
	//重新获取并显示清除后的缓存大小
	getStorageSize();
}
//根据文件本地路径检查缓存文件是否存在  path 为 图片本地地址
function isHave(path) {
	return new Promise((resolve, reject) => {
		uni.getSavedFileInfo({
			filePath: path,
			success: (res) => {
				if (res.size > 0) {
					//打开
					resolve(true)
				} else {
					resolve(false)
				}
			},
			fail() {
				resolve(false)
			}
		})
	})
}


//删除文件
function removefile(path) {

	uni.removeSavedFile({
		filePath: path,
		success() {
			console.log('删除成功');
		},
		fail(err) {
			console.log('删除失败', err);
		}
	});
}

//删除全部文件
function removefileAll() {
	
	if (uni.getStorageSync('zdy-processing-img')) {
		let processingList = JSON.parse(uni.getStorageSync('zdy-processing-img'));
		let templist=[...processingList.values()];
		
		console.log(templist);
		templist.map(item=>{
			uni.removeSavedFile({
				filePath: item,
				success() {
					console.log('删除成功');
				},
				fail(err) {
					console.log('删除失败', err);
				}
			});
		})
		
		clearStorage();
		
	}
}


//文件下载
//下载文件
function downLoadFile(url, key) {
	uni.downloadFile({
		url: url,
		success(res) {
			//保存到本地
			// console.log(res.tempFilePath, 'res.tempFilePath');
			uni.saveFile({
				tempFilePath: res.tempFilePath,
				success: (res2) => {
					// console.log(res2, 'bbendi1');
					let savedFilePath = res2.savedFilePath;

					let processingList = {};
					if (uni.getStorageSync('zdy-processing-img')) {
						processingList = JSON.parse(uni.getStorageSync('zdy-processing-img'))
					}
					processingList[key] = savedFilePath;
					// console.log('下载成功', processingList);
					uni.setStorageSync('zdy-processing-img', JSON.stringify(processingList))
				},
				fail: (err) => {
					// console.log(err, '保存失败');
					removefileAll();
				}
			});
		},
		fail(res) {
			console.log(err, '下载失败');
		}
	})
}
// 图片文件处理
//url 图片地址  key 图片唯一识别码
export function processing(url, key) {
	getStorageSize(); //先获取当前缓存大小
	if (storageSize >= storageSizeMax) {
		//图片缓存大小过大
		clearStorage();
		return url;
	} else if (uni.getStorageSync('zdy-processing-img')) {
		// 当图片缓存满足
		let processingList = JSON.parse(uni.getStorageSync('zdy-processing-img'));
		if (processingList[key] && isHave(processingList[key])) {
			// 存在这个key
			console.log('存在key');
			return processingList[key]
		} else {
			// 不存在这个key
			downLoadFile(url, key)
			return url;
		}
	} else {
		downLoadFile(url, key)
		return url;
	}
}

你可能感兴趣的:(vue,uni-app,uni-app,前端,javascript)