微信小程序授权保存图片到相册

在微信小程序中,做分享朋友圈,保存图片视频到相册,需要授权,会经常遇到。有时用户误操作,第一次拒绝授权弹框后,微信会认为用户拒绝该授权意愿并且不会再次调起授权弹框,这是该怎么办呢?

1、授权情况分析

  • 用户第一次使用,弹出授权
  • 用户已经授权,可进行保存操作
  • 用户已经拒绝,需要调起授权

2、代码实现

checkAuthorize: function (e) {
    var that = this
    wx.getSetting({
      success: function (res) {
        console.log(res)
        //判断是否已经授权
        if (res.authSetting["scope.writePhotosAlbum"]) {
          //已经授权,进行下载图片、视频,后进行保存
          that.downloadVideo(e)
        } else if (!res.authSetting.hasOwnProperty("scope.writePhotosAlbum")) {
          //用户第一次使用,调起授权
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success() {
              //授权成功,进行下载图片、视频,后进行保存
              that.downloadVideo(e)
            }
          })
        } else {
          //已经拒绝授权,去到设置页面授权
          wx.showModal({
            title: "未授权添加到相册",
            content: "下载素材保存到相册,需打开添加到相册的权限开关",
            confirmColor: "#00D2EC",
            confirmText: "去设置",
            success(res){
              if(res.confirm) {
                wx.openSetting({})
              }
            }
          })
        }
      },
    })
  },

  downloadVideo: function (e) {
    var item = this.data.materialList[e.currentTarget.dataset.index]
    var that = this
    this.data.downloadCount = 0
    item.videoList.forEach(video => {
      wx.downloadFile({
        url: video,
        success: function (res) {
          console.log(res)
          wx.saveVideoToPhotosAlbum({
            filePath: res.tempFilePath,
            success(res) {
              that.data.downloadCount++;
              if (that.data.downloadCount == item.videoList.length) {
                that.downloadImage(e)
              }
            }
          })
        }
      })
    });
  },

猜你喜欢

转载自blog.csdn.net/lqw200931116/article/details/123657037
今日推荐