<Image className="qr-code" src={privateDoctor.qrCode} onLongPress={this.onSavePhoto.bind(this)}></Image>
onSavePhoto (e) {
const qrCodePath = this.state.privateDoctor.qrCode
const _this = this
e.stopPropagation();
Taro.getSetting({
success (res) {
if (!res.authSetting['scope.writePhotosAlbum']) {//没有授权
Taro.authorize({
scope:'scope.writePhotosAlbum',
success: () => {
_this.downloadImgToAlbum(qrCodePath);
},
fail: () => {
_this.setState({//显示授权层
openSetting:true
})
}
})
} else { //已授权
_this.downloadImgToAlbum(qrCodePath);
}
}
})
}
downloadImgToAlbum (qrCodePath) {
Taro.showToast({
title:'正在保存,请稍等',
icon:'none',
duration:2000
})
//下载图片
this.downloadHttpImg(qrCodePath).then((res)=>{
this.sharePosteCanvas(res)
})
}
downloadHttpImg =(httpImg) =>{
return new Promise(((resolve) => {
Taro.downloadFile({
url: httpImg,
success: (res) => {
if (res.statusCode ===200) {
resolve(res.tempFilePath)
} else {
Taro.showToast({
title:'图片下载失败!',
icon:'none',
duration:1000
})
}
},
fail: () => {
Taro.showToast({
title:'图片下载失败!',
icon:'none',
duration:1000
})
}
})
}))
}
sharePosteCanvas =(imgUrl)=> {
Taro.saveImageToPhotosAlbum({
filePath: imgUrl,
success () {
Taro.showToast({
title:'图片已保存到相册',
icon:'none',
duration:1000
})
},
fail () {
Taro.showToast({
title:'图片保存失败',
icon:'none',
duration:1000
})
}
})
}