async saveThePhoto() {
let that=this;
wx.getSetting({//獲取權(quán)限
? ? success(res) {
if (res.authSetting["scope.writePhotosAlbum"]) {//已經(jīng)授權(quán)過
? ? ? ? that.saveImageToAlbumFn();
}else {
wx.authorize({
scope:"scope.writePhotosAlbum",
success(result) {//用戶手動(dòng)點(diǎn)擊了同意
? ? ? ? ? ? that.saveImageToAlbumFn();
},
fail(result_err) {//用戶點(diǎn)擊了拒絕,再次彈窗一個(gè)授權(quán)的提示框,否則用戶拒絕后,無法再次授權(quán)【同意】
? ? ? ? ? ? that.setState({
openSetting:true,
})
}
});
}
},
fail(err) {
console.log(err);
}
});
}
//保存至相冊(cè)
saveImageToAlbumFn =()=>{
let {codeImg}=this.state;
const fileManager = wx.getFileSystemManager();
let timestamp =new Date().getTime();
let that=this;
fileManager.writeFile({//下載文件資源到本地玖像,客戶端直接發(fā)起一個(gè)HTTP GET 請(qǐng)求,返回文件的本地臨時(shí)路徑
? ? filePath:`${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
data:codeImg.slice(22),
encoding:"base64",
success(result) {
that.closeOpenSet();//關(guān)閉相冊(cè)授權(quán)框
? ? ? Taro.saveImageToPhotosAlbum({
filePath:`${wx.env.USER_DATA_PATH}/qrcode_${timestamp}.png`,
}).then(getImageInfoResult => {
if (getImageInfoResult.errMsg ==='saveImageToPhotosAlbum:ok') {
Taro.showToast({
title:'圖片已保存到相冊(cè)',
icon:'none',
duration:1000
? ? ? ? ? })
}else {
Taro.showToast({
title:'圖片保存失敗',
icon:'none',
duration:1000
? ? ? ? ? })
}
});
},
fail(result_res){
console.log(result_res)
}
});
}
//關(guān)閉 相冊(cè)授權(quán)框
closeOpenSet = () => {
this.setState({
openSetting:false
? })
}
=================================================================================
<AtModal isOpened={openSetting} className='appOrderMod' onClose={this.closeOpenSet} >
<AtModalContent>保存二維碼需要授權(quán)'保存圖片到相冊(cè)'</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>