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

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

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)
              }
            }
          })
        }
      })
    });
  },

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值