最近需要做一个微信下载图片到手机的需求,因为涉及到微信的照片权限,因此做个记录
// 鉴权操作 判断是否有保存到相册的权限
// 有就直接下载 没有就弹窗提示给权限
downImg() {Taro.getSetting({success: res => {if(!res.authSetting['scope.writePhotosAlbum']) {Taro.authorize({scope: 'scope.writePhotosAlbum',success: () => {this.doSaveImg()},fail: () => {this.openConfirm()}})} else {this.doSaveImg()}}})
}
// 生成临时路径 保存图片到手机
doSaveImg() {Taro.downloadFile({url: '图片地址',success: res => {Taro.saveImageToPhotosAlbum({filePath: res.tempFilePath,success: () => {showToast({title: '已保存到相册', icon: 'success'})},fail: () => {showToast({title: '保存失败', icon: 'none'})}})}})
}
// 权限弹窗
openConfirm() {Taro.showModal({content: '检测到您没有打开小程序相册权限,是否取设置打开?',showCancel: true,success: res => {if(res.confirm) {// 打开权限Taro.openSetting(success: res => {this.doSaveImg()})}}})
}