美文网首页
小程序相册授权

小程序相册授权

作者: wangwing | 来源:发表于2019-04-10 10:16 被阅读0次

生成海报保存到手机相册,
用户拒绝后就无法再次使用未授权时弹出的授权页面,

遇到的问题:
触发openSetting需要通过tap的调用

  1. 通过button来调用
  2. 则是通过点击showModal的回调来调用openSetting, 主要讲通过showModal这种方式

我用的是taro框架,所以一开始在程序中Promise 的来处理回调,报错:openSetting:fail can only be invoked by user TAP gesture. (官方回答:不能使用promise的方式去处理回调,需要改成直接普通的回调方式, promise是异步的,“点击行为允许调用”这个机制要求是同步的)

Taro.getSetting().then((res)=>{
    console.log(res)
    // 假如用户未授权,则弹出授权框让用户选择
    if(!res.authSetting['scope.writePhotosAlbum']){
       Taro.authorize({
           scope:'scope.writePhotosAlbum'
       }).then(()=>{
          // 授权成功,则执行生成海报的程序
       }).catch((err)=>{
             // 如果用户拒绝了授权,则弹出提示框提示用户,用户点击弹出框确认按钮,则通过Taro.openSetting去重新授权相册
             console.log(err)
             // 拒绝后,错误提示有这几种,看别人都做if判断,我这边就不错判断
             // err.errMsg == 'authorize:fail:auth deny' 这种是android上返回的(vivo)
             // err.errMsg == 'authorize:fail auth deny' 这种是ios上第一次拒绝后返回的,还有开发者工具上也是返回这种
             // err.errMsg == 'authorize:fail authorize no response' 这种是ios拒绝后,用户再次点击后返回的信息
             Taro.showModal({
                 title:'提示',
                 content: '若不微信授权,则无法使用保存图片或视频到您的相册',
                 success: s=>{
                    Taro.openSetting({
                         success(settingdata) {
                         if (settingdata.authSetting['scope.writePhotosAlbum']) {
                            // 重新授权成功,则执行生成海报的程序
                         }else {
                            // 用户再次拒绝授权,则提示用户
                              Taro.showToast({
                                title: '获取权限失败,无法使用保存图片或视频到您的相册',
                                icon: 'none'
                              })
                            }
                          }
                        })
                      }
                    })
                  })
              }else{
                  console.log("b")
                  that.createImg(user);
              }
              that.setState({
                  isCanvas: true   
              })
         })

相关文章

网友评论

      本文标题:小程序相册授权

      本文链接:https://www.haomeiwen.com/subject/kdigiqtx.html