async saveThePhoto() {
let that=this;
wx.getSetting({//获取权限
success(res) {
if (res.authSetting["scope.writePhotosAlbum"]) {//已经授权过
that.saveImageToAlbumFn();
}else {
wx.authorize({
scope:"scope.writePhotosAlbum",
success(result) {//用户手动点击了同意
that.saveImageToAlbumFn();
},
fail(result_err) {//用户点击了拒绝,再次弹窗一个授权的提示框,否则用户拒绝后,无法再次授权【同意】
that.setState({
openSetting:true,
})
}
});
}
},
fail(err) {
console.log(err);
}
});
}
//保存至相册
saveImageToAlbumFn =()=>{
let {codeImg}=this.state;
const fileManager = wx.getFileSystemManager();
let timestamp =new Date().getTime();
let that=this;
fileManager.writeFile({//下载文件资源到本地,客户端直接发起一个HTTP GET 请求,返回文件的本地临时路径
filePath:`${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
data:codeImg.slice(22),
encoding:"base64",
success(result) {
that.closeOpenSet();//关闭相册授权框
Taro.saveImageToPhotosAlbum({
filePath:`${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
}).then(getImageInfoResult => {
if (getImageInfoResult.errMsg ==='saveImageToPhotosAlbum:ok') {
Taro.showToast({
title:'图片已保存到相册',
icon:'none',
duration:1000
})
}else {
Taro.showToast({
title:'图片保存失败',
icon:'none',
duration:1000
})
}
});
},
fail(result_res){
console.log(result_res)
}
});
}
//关闭 相册授权框
closeOpenSet = () => {
this.setState({
openSetting:false
})
}
=================================================================================
<AtModal isOpened={openSetting} className='appOrderMod' onClose={this.closeOpenSet} >
<AtModalContent>保存二维码需要授权'保存图片到相册'</AtModalContent>
<View className='app_btns'>
<View className='btn_'><AtButton className='btn mySecon' circle size='small' type='secondary' onClick={this.closeOpenSet}>取消</AtButton></View>
<View className='btn_'><AtButton className='btn' circle size='small' type='primary' openType='openSetting' onOpenSetting={() =>this.saveThePhoto()}>确定</AtButton></View>
</View>
</AtModal>