<button open-type="openSetting" bindopensetting="handleSetting" wx:if="{{isPic}}">授权保存</button>
<button bindtap="saveImg" wx:else>保存图片</button>
Page({
data: {
isPic: false
},
onLoad: function (options) {
var that = this;
var scene = decodeURIComponent(options.scene);
},
getConfirm(){
},
saveImg(){
let that = this;
console.log(1)
wx.getSetting({
success: function(res){
//不存在相册授权
if (!res.authSetting['scope.writePhotosAlbum']){
wx.authorize({
scope: 'scope.writePhotosAlbum',
success: function(){
that.savePhoto();
that.setData({
isPic: false
})
},
fail: function(err){
that.setData({
isPic: true
})
}
})
}else{
that.savePhoto();
}
}
})
},
handleSetting(e){
var that = this;
if (!e.detail.authSetting['scope.writePhotosAlbum']){
wx.showModal({
title: '警告',
content: '不授权无法保存',
showCancel: false
})
that.setData({
isPic: true
})
}else{
wx.showToast({
title: '保存成功'
})
that.setData({
isPic: false
})
}
},
savePhoto(){
let urlStr = 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534762319109&di=8974aafb91846a92be2eee8db6d90399&imgtype=0&src=http%3A%2F%2Fe.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fb151f8198618367a2e8a46ee23738bd4b31ce586.jpg';
var that = this;
wx.downloadFile({
url: urlStr,
success: function (res) {
wx.saveImageToPhotosAlbum({
filePath: res.tempFilePath,
success: function (data) {
wx.showToast({
title: '保存成功',
icon: 'success',
duration: 1500
})
}
})
}
})
},
bindGetUserInfo(e){
if(!e.detail.userInfo){
console.log('用户点击了取消')
}else{
console.log(e.detail.userInfo)
}
},
onShareAppMessage: function () {
return {
title: '来自休休漠漠',
desc: '这是一个测试分享',
path: '/pages/test/test',
success: function(res){
console.log(res)
},
fail: function(res){
console.log(res)
}
}
}
})
此方法是用按钮的形式获得授权,wx.openSetting()方法将弃用