list里面有8张图片 只在保存完最后一张时才弹窗提示 而不是保存一张就弹窗一次 请问这个应该怎么改?
this.data.list.map((res,index)=>{
wx.downloadFile({
url: res.image,
success: function (res) {
var that = index
if (res.statusCode === 200) {
let img = res.tempFilePath;
wx.saveImageToPhotosAlbum({
filePath: img,
success(res) {
console.log(that)
tt.showToast({
title: '保存成功',
icon: 'none',
duration: 500
})
}
});
}
}
})
})
###
转为 Promise 形式后利用 Promise.all 统一处理。
wx.downloadFileAsync = (options = {}) => {
return new Promise((resolve, reject) => {
let successFunc = options.success,
failFunc = options.fail;
options.success = (res) => {
successFunc && successFunc(res);
resolve(res);
};
options.fail = (err) => {
failFunc && failFunc(err);
reject(err);
};
wx.downloadFile(options);
});
}
Promise.all(
this.data.list.map(res => wx.downloadFileAsync({ url: res.image })
).then(reses => {
if (reses.every(res => res.statusCode === 200)) {
console.log('全部下载成功');
}
});
P.S. 徒手写的,没测,思路能看懂就行。只写了 downloadFileAsync
的部分,saveImageToPhotosAlbum
你自己照着写。
徒手写怕有问题的话,微信小程序官方自带了 Promise 化工具库:https://developers.weixin.qq....
第三方实现的话可以看:https://github.com/fudiwei/wx...
###写个flag,每保存成功一次加一,flag为list.length的时候弹框,最简单的思路