<view class="qrcardbox">
<image src="/common/images/about_us_qr.png" data-url="/common/images/about_us_qr.png" bindlongpress='saveweixinImage'></image>
<text class="long">【长按保存二维码图片】</text>
</view>
// 点击保存图片
saveweixinImage(e) {
let url = e.currentTarget.dataset.url;
let that = this;
//用户需要授权
//wx.getSetting获取用户的当前设置。(不管同意与否)
wx.getSetting({
success: (res) =>{
//authSetting用户授权设置信息(用户授权结果)
//是否授权保存到相册 boolean scope.writePhotosAlbum
//如果值为undefined:以前没有授权过或者以前第一次点了拒绝之类的
if(res.authSetting['scope.writePhotosAlbum'] == undefined) {
// wx.authorize是否开启授权(去申请获取权限) 掉起授权弹窗
//首先得开启authorize,才能去判断到底是true或者false
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: () =>{
//同意授权
that.saveImg1(url)
},
fail:(res) =>{
}
})
} else if(res.authSetting['scope.writePhotosAlbum'] == false) {
that.authConfirm()
} else {
//已经授权了
that.saveImg1(url);
}
},
fail:(res) =>{
console.log(res)
that.authConfirm()
}
})
},
//去获取用户权限
authConfirm() {
let that = this
wx.showModal({
content: '检测到您没打开保存图片权限,是否去设置打开?',
confirmText: "确认",
cancelText: "取消",
success:function(res) {
console.log(res)
if(res.confirm) {
// 唤醒小程序自带右上角的设置界面
wx.openSetting({
success(res) {
if(res.authSetting['scope.writePhotosAlbum']){
//that.saveImg1()
} else {
wx.showToast({
title:'您没有授权,无法保存到相册',
icon:'none'
})
}
}
})
} else {
wx.showToast({
title: '您没有授权,无法保存到相册',
icon: 'none'
})
}
}
})
},
//保存到本地
saveImg1(url) {
//获取图片信息
wx.getImageInfo({
src: url,
success: (res) =>{
let path = res.path;
wx.saveImageToPhotosAlbum({
filePath: path,
success: (res) => {
if (res.errMsg == 'saveImageToPhotosAlbum:ok') {
wx.showToast({
title: '保存成功',
icon: 'success'
})
}
},
fail: (res) => {
}
})
},
fail:(res) =>{}
})
},
wx.authorize()唤醒授权窗口,如图(位置信息):
wx.openSetting() 唤醒小程序自带设置界面,如图: