插件使用vant,因为小程序用户拒接授权以后,再一次保存需要用户主动触发!!!
<button bindtap="handleDownloadFile"><button>
<van-dialog
title="提示"
show="{{ show }}"
show-cancel-button
message="请在设置中开启微信的存储空间权限"
confirm-button-open-type="openSetting"
bind:cancel="cancel"
bind:opensetting="cancel"
>
</van-dialog>
handleDownloadFile() {
this.setDisabled(true);
// filePath要自定义,否则个别安卓手机无法保存
let fileName = new Date().valueOf()
let filePath = wx.env.USER_DATA_PATH + "/" + fileName + '.JPG'
wx.downloadFile({
url: this.data.url,
filePath: filePath,
success: (res) => {
this.setData({
tempFilePath: res.filePath
});
console.log('res', res)
this.handleSaveImageToPhotosAlbum()
},
fail: (err) => {
console.log(err)
}
});
},
handleSaveImageToPhotosAlbum() {
wx.saveImageToPhotosAlbum({
filePath: this.data.tempFilePath,
success: (res) => {
console.log('保存成功=', res);
// setTimeout是为了防止在苹果手机中一闪而过
wx.hideLoading()
this.onCancel()
setTimeout(() => {
wx.showToast({
title: '保存成功!请到相册中查看',
icon: 'none',
duration: 3000
});
}, 0);
},
fail: (err) => {
console.log('保存失败2=', err);
this.saveError()
}
});
},
saveError () {
/* 验证是否禁用了存储空间 start */
this.setDisabled(false, 'isDisabledSave');
wx.hideLoading()
if (!this.haveWritePotos) {
this.setData({
show: true
})
} else {
wx.showModal({
title: '提示',
content: '保存失败',
showCancel: false
});
}
},