[Wepy] 微信小程序保存图片到相册 且用户拒绝后二次授权方法

二次授权因wx.opensetting官方已废弃,所以这里用最新的方案按钮解决

代码如下

wxml:

<button wx:if="{{authBtn}}" open-type="openSetting" bindopensetting='checkAuth'>重新授权相册</button>
data = {
    authBtn: false,
}

methods = {
    saveImg(data) {
        let that = this
        wx.getSetting({ //获取设置
          success(res) {
              wx.authorize({//获取相册授权信息
                scope: 'scope.writePhotosAlbum',
                success() {//已授权
                  wx.saveImageToPhotosAlbum({ //保存到相册
                    filePath: `images/morePage/code-${data}.jpg`,
                    success() {
                      wx.showToast({
                        title: '保存成功',
                        icon: 'success',
                      })
                    },
                    fail() {// 一般保存出错会出现(可删除)
                      that.imgAuthBtn = true
                      wx.showToast({
                        title: '未知错误请反馈',
                        icon: 'none',
                      })
                    }
                  })
                },
                fail() {//未授权
                  that.imgAuthBtn = true
                  that.$apply()
                  wx.showToast({
                    title: '保存失败请授权',
                    icon: 'none',
                  })
                }
              })
            }
        })
      },
      checkImgAuthFun(res) { //二次以上检验是否授权图片
        let that = this;
        if (!res.detail.authSetting['scope.writePhotosAlbum']) { //二次以上授权, 如果未授权
          that.imgAuthBtn = true //显示授权按钮          
          wx.showToast({
            title: '授权失败',
            icon: 'none',
          })
        } else {
          that.imgAuthBtn = false //不显示授权按钮
          wx.showToast({
            title: '授权成功',
            icon: 'none',
          })
        }
      },
}
发布了127 篇原创文章 · 获赞 150 · 访问量 48万+

猜你喜欢

转载自blog.csdn.net/qq_40259641/article/details/102163853
今日推荐