从低版本(3.9.7)升级到3.9.11,保存图片到相册没问题。
之后卸载微信,不保留数据,重新安装,再次下载多图会导致闪退。
保存到相册代码如下:
// 去掉图片链接所带参数
getOriginImgUrl(url) {
return url.split('?')[0];
},
getPhotosAlbumAuthorize() {
return new Promise((resolve, reject) => {
wx.getSetting({
success: (res) => {
//检查是否有访问相册的权限,如果没有则通过wx.authorize方法授权
if (!res.authSetting['scope.writePhotosAlbum']) {
console.log('没有获取授权');
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () => {
//用户点击允许获取相册信息后进入下载保存逻辑
resolve();
},
fail() {
reject();
wx.showModal({
title: '未获取相册授权',
content: '请点击右上角胶囊按钮,打开设置,将相册权限打开',
confirmText: '确定',
showCancel: false
});
}
});
} else {
console.log('已获取授权');
resolve();
}
}
});
});
},
handleDownload() {
this.getPhotosAlbumAuthorize().then(() => {
this.sequenceDownload();
}).catch(() => {});
},
sequenceDownload() {
const filesUrl = this.data.banners.map(url => {
return this.getOriginImgUrl(url);
});
const proInfo = this.data.detail.description;
if (filesUrl.length) {
wx.showLoading({
title: '下载中',
});
const allPromises = filesUrl.map(url => this.downloadFile(url));
Promise.all(allPromises).then(() => {
wx.hideLoading({
noConflict: true
});
// 复制商品描述到剪切板
wx.setClipboardData({
data: proInfo,
success () {}
});
//保存成功弹出提示,告知一下用户
wx.showModal({
title: '提示',
content: '已保存到相册,商品描述已经复制到剪切板',
confirmColor: '#E0B77A',
confirmText: '知道了',
showCancel: false
});
}).catch(err => {
wx.hideLoading({
noConflict: true
});
console.log(err);
wx.showToast({
icon: 'none',
title: '下载保存失败,请重试'
});
});
}
},
getFileName(url) {
const splitArr = url.split('/');
return splitArr[splitArr.length - 1];
},
downloadFile(url) {
return new Promise((resolve, reject) => {
wx.downloadFile({
url: url,
success: (res) => {
var savePath = wx.env.USER_DATA_PATH + '/' + this.getFileName(url);
const fs = wx.getFileSystemManager();
fs.saveFile({ //下载成功后保存到本地
tempFilePath: res.tempFilePath,
filePath: savePath,
success() {
//获取了相册的访问权限,使用 wx.saveImageToPhotosAlbum 将图片保存到相册中
wx.saveImageToPhotosAlbum({
filePath: savePath,
success: () => {
resolve();
// 保存完成后删除本地缓存文件
fs.unlink({
filePath: savePath,
fail(errmsg) {
console.log('remove file fail:', errmsg);
}
});
},
fail(err) {
reject(err);
}
});
},
fail(err2) {
reject(err2);
}
});
},
fail: (err) => {
console.log('download error', err);
reject(err);
}
});
});
},
补充代码片段https://developers.weixin.qq.com/s/hLkOtAmf71TJ