微信小程序保存图片到相册授权被拒绝后重新拉取授权

当用户拒绝微信小程序保存图片到相册的授权后,通过自定义弹框监测并引导用户重新开启设置,确保授权流程顺利进行。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  saveimg: function() {
    var that = this;
    wx.showLoading({
      title: '保存中',
    })
    wx.downloadFile({
      url: that.data.haibaoimg,
      success: function(res) {
        var imgpath = res.tempFilePath;
        if (res.statusCode == 200) {
          wx.getSetting({
            success(res) {
              //第一次进来res.authSetting['scope.writePhotosAlbum']为undefined 
              if (res.authSetting['scope.writePhotosAlbum'] != undefined && res.authSetting['scope.writePhotosAlbum'] == false) {
               //显示自定义弹框,第一次进来不显示自定义
                that.setData({
                  userauth: true
                });
                wx.hideLoading();
              } else {
                wx.saveImageToPhotosAlbum({
                  filePath: imgpath,
                  success: function(res) {
                    wx.hideLoading();
                    wx.showToast({
                      title: '保存成功!'
                    })
                  },
                  fail: function(res) {
                    wx.hideLoading();
                    wx.showToast({
                      title: '保存失败!'
                    })
                  }
                })
              }

            }
          })

        }
      }
    })

  },

自定义弹框:

  <view class="userauth" wx:if="{{userauth}}">
    <view class="auth_content">
      <view class="authtitle">
        提示
      </view>
      <view class="authtext">
        允许保存图片或视频到你的相册?
      </view>
      <view class="authfooter">
        <view bindtap="closeauth">取消</view>
        <view>
          /*利用button调用设置*/
          <button plain="{{true}}" hover-class="none" open-type='openSetting' bindopensetting="getseetting">
            确定
          </button>
        </view>
      </view>
    </view>

  </view>

监听打开设置用户是否打开了授权

  getseetting: function(res) {
    if (res.detail.authSetting['scope.writePhotosAlbum'] == true) {

      this.saveimg();
    } else {
      wx.showToast({
        title: '保存到相册授权未打开!',
        icon: 'none'
      })
    }
    this.setData({
      userauth: false
    });
  },

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值