data:{
imageSrc: [{
name: 'levelOne',
src: image + 'levelOne.gif'
},
{
name: 'levelTwo',
src: image + 'levelTwo.gif'
},
{
name: 'levelThree',
src: image + 'levelThree.gif'
}]
},
//缓存所有图片
imageLoad: function() {
var that = this
var imageSrc = this.data.imageSrc
var x = 32 / imageSrc.length
var newPicture = {}
console.log("加载了")
this.setData({
barWidth: 0
})
that.getammeternumber(0, 0, imageSrc.length, newPicture)
},
//递归调用
getammeternumber: function(barWidth, i, length, newPicture) {
var that = this;
var imageSrc = that.data.imageSrc
var x = 32 / imageSrc.length
wx.downloadFile({
url: imageSrc[i].src,
success(res) {
if (res.statusCode === 200) {
// wx.compressImage({
// src: res.tempFilePath, // 图片路径
// quality: 10, // 压缩质量
// success(res) {
console.log("qq", res.tempFilePath)
const fs = wx.getFileSystemManager()
fs.saveFile({
tempFilePath: res.tempFilePath,
success(res) {
console.log('图片缓存成功', res.savedFilePath)
newPicture[imageSrc[i].name] = res.savedFilePath
console.log("i", i, 'length', length)
that.setData({
barWidth: barWidth + x,
})
if (++i < length) {
that.getammeternumber(that.data.barWidth, i, length, newPicture);
} else {
//跳转
console.log("qqqqq", newPicture)
wx.setStorageSync('newPicture', newPicture)
wx.redirectTo({
url: './index',
})
that.setData({
begin: true
})
}
},
fail(res) {
that.clean()
that.checkRe()
}
})
// },
// fail(res) {
// console.log(res)
// }
// })
}
},
fail(res) {
that.clean()
that.checkRe()
}
})
}
//每次选择性清除缓存
clean: function() {
var that = this
wx.getSavedFileList({ // 获取文件列表
success(res) {
//是否有缓存
console.log("清除缓存", res)
that.setData({
loadImage: true
})
res.fileList.forEach((val, key) => { // 遍历文件列表里的数据
// 删除存储的垃圾数据
wx.removeSavedFile({
filePath: val.filePath
});
})
}
})
try {
wx.clearStorageSync()
} catch (e) {
console.log("清除1", e)
}
try {
wx.clearStorageSync()
} catch (e) {
console.log("清除2", e)
}
},