当第一次的时候使用wx.getSetting的时候会发现没有scope.writePhotosAlbum这个对象,所以并不知道是否授权 在这里插入图片描述
<view class="ui-share-box">
<view class="ui-thumb">
<image mode="scaleToFill" src="{{shareThumb}}"></image>
</view>
<view class="ui-button-box main-justify">
<button class="main-center cross-center"
@tap.stop="getSaveImage">
<text class="icon-share"></text>保存图片
</button>
<button class="main-center cross-center"
open-type="share"><text class="icon-share"></text>分享给好友</button>
</view>
</view>
<view class="ui-toast-box" :class="{'is-show': isShowToast}">
<view class="ui-toast-content dir-top main-justify">
<view class="ui-content">
<view class="cell-title">温馨提示</view>
<view class="cell-content">去设置允许访问相册</view>
</view>
<button open-type="openSetting"
bindopensetting='getSetting'>确定</button>
</view>
</view>
data = {
isShowToast: false,
shareThumb: '',
}
__wxSetting() {
let self = this
wx.getSetting({
success(res) {
if (_.isUndefined(res.authSetting['scope.writePhotosAlbum'])) {
self.__saveImage()
} else if (!res.authSetting['scope.writePhotosAlbum']){
self.isShowToast = true
} else {
self.__saveImage()
}
self.$apply()
}
})
}
__saveImage() {
let self = this
wx.saveImageToPhotosAlbum({
filePath: self.shareThumb,
success(res) {
wx.showModal({
title: '温馨提示',
content: '已保存到系统相册中',
showCancel: false
})
},
fail(res) {
wx.showModal({
title: '温馨提示',
content: '保存失败',
showCancel: false
})
}
})
}
methods = {
getSaveImage() {
let self = this
self.__wxSetting()
},
getSetting(event) {
let self = this
self.isShowToast = false
if (!event.detail.authSetting['scope.writePhotosAlbum']) {
wx.showModal({
title: '温馨提示',
content: '你关闭了访问相册的权限,无法保存,请允许访问相册',
showCancel: false
})
} else {
wx.showToast({
icon: 'success',
title: `授权成功`,
success(res) {
self.__saveImage()
}
})
}
}
}
|
请发表评论