参考:https://blog.csdn.net/qq_29483485/article/details/105421098
<view class="savecode" bindtap='saveToPhone'> 保存二维码 </view>
// 保存图片 saveToPhone(e){ // let imgSrc = e.currentTarget.dataset.url; //要保存的图片url // console.log(imgSrc) wx.showLoading({ title: '保存中...' }) wx.downloadFile({ //下载文件资源到本地 url: this.data.imgUrl, success: function (res) { console.log(res); //图片保存到本地 wx.saveImageToPhotosAlbum({ filePath: res.tempFilePath, success: function (data) { wx.hideLoading() wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) }, fail: function (err) { if (err.errMsg === "saveImageToPhotosAlbum:fail auth deny" || err.errMsg === "saveImageToPhotosAlbum:fail:auth denied") { console.log("当初用户拒绝,再次发起授权") wx.showModal({ title: '提示', content: '需要您授权保存相册', showCancel: false, success: modalSuccess => { wx.openSetting({ success(settingdata) { console.log("settingdata", settingdata) if (settingdata.authSetting['scope.writePhotosAlbum']) { wx.showModal({ title: '提示', content: '获取权限成功,再次点击图片即可保存', showCancel: false, }) } else { wx.showModal({ title: '提示', content: '获取权限失败,将无法保存到相册哦~', showCancel: false, }) } }, fail(failData) { console.log("failData", failData) }, complete(finishData) { console.log("finishData", finishData) } }) } }) } }, complete(res) { console.log(res); wx.hideLoading() } }) } }) },
说明:普通扫码到手机无法保存到手机上,真机测试扫码可以保存到本地,模拟器上也可以保存到本地