封装下载函数:
downloadAllPic (imgData) { wx.showLoading({ title: '图片下载中' }) let uploadNum = 0 let picNum = 0 imgData.forEach((item, index, arr) => { if (item.imgUrl) { picNum++ wx.downloadFile({ url: item.imgUrl, success: function (res) { // 图片保存到本地 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (data) { uploadNum++ if (uploadNum === picNum) { wx.hideLoading() wx.showModal({ title: '提示', content: '图片已保存到您的手机相册', showCancel: false }) } }, fail: function (err) { console.log(err) if (err.errMsg === 'saveImageToPhotosAlbum:fail auth deny') { console.log('当初用户拒绝,再次发起授权') wx.openSetting({ success(settingdata) { console.log(settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { console.log('获取权限成功,给出再次点击图片保存到相册的提示。') } else { console.log('获取权限失败,给出不给权限就无法正常使用的提示') } } }) } }, complete(res) { console.log(res) } }) } }) } })}
使用:
const allPicList = [{imgName: '图一',imgUrl: 'https://file.lsjlt.com/upload/f/202309/25/cz5iep3cwi3.jpg'},{imgName: '图二',imgUrl: 'https://file.lsjlt.com/upload/f/202309/25/cz5iep3cwi3.jpg'}]downloadAllPic(allPicList)
来源地址:https://blog.csdn.net/aaa123aaasqw/article/details/130621068