uniapp检测小程序相关授权是否开启,未开启提示开启

uniapp检测小程序相关授权是否开启
正常开发的小程序,必定需要在提示授权的时候用户必须选择”授权“才能保证小程序整体功能以及流程的正常使用,
但是众所周知,这个授权提示,弹出一次过后就不会再弹出了,用户又不知道去怎么回事,就成了影响用户体验的bug,
所以需要加点操作,在所有功能之前检测涉及的授权是否全部开启,如果没有开启就跳转去设置权限的页面,然后设置
回来,会走onShow这个生命周期,需要getSetting再判断一次这些权限的状态。
在这里插入图片描述

方法一:(直接uni.authorize,然后检测某个权限,如果没有,就开启showModal,但是这里就会有bug,因为没有hidenModal
会出现页面产生多个showModal的情况,需要一个一个去点掉,很恶心,尝试过自定义状态记录这个showModal,让他只展示
一个,但是发现onShow会在我这个业务流程里执行多次才可以,达不到效果,但是偏偏这里有个比较好的东西就是可以在showM
odal的确认或失败的事件里面直接调用openSetting的方法,然后即使返回页面,还可以看到授权修改之后的结果)

async checkAllowCamera(){
    this.currentAllow = 'camera'
    let isOk = false
    let that = this
    await	new Promise(async r => {	
      uni.authorize({
        scope:'scope.camera',
        success: (res) => {
          console.log('摄像头授权开了');
          isOk = true
          r()
        },
        fail: (res) => {
        	//	当权限没有打开  showModal
          that.allowCameraRe = true
          uni.showModal({
            content:'检测到您未开启摄像头权限,请保持摄像头权限为开启状态',
            confirmText: "去开启",
            showCancel: false,
            success: (res) => {
                if(res.confirm){
                  uni.openSetting({
                    success: (res) => {
                      console.log(res);
                      if(res.authSetting['scope.camera']==false){
                        that.allowCameraRe = false
                        //没开就再重新调用这个方法
                        checkAllowCamera()
                      }else if(res.authSetting['scope.camera']){
                        isOk = true
                        that.allowCameraRe = true
                        r()
                      }
                    }
                  })
                }else{
                  uni.navigateBack({
                    delta:1
                  })
                }
            }
          })
          }
      })
    })
    this.allowCamera = isOk
    return isOk
  }

**方法二:**避免这个多个showModal的发生,手动写一个弹框,监听状态,还是要先uni.authorize检测是否开启,然后控制自定
义的弹框显隐,然后弹框中会有一个特殊的按钮,绑定着openSetting的事件,但是这个也有个问题,不能比较好的在回调监听
权限修改之后的结果,需要手动在onShow写个方法去检测,然后再控制一次弹框

//先检测一次
async checkAllowCamera(){
    this.currentAllow = 'camera'
    let isOk = false
    let that = this
    await	new Promise(async r => {	
      uni.authorize({
        scope:'scope.camera',
        success: (res) => {
          console.log('摄像头11111');
          isOk = true
          that.showModalVisible = false
          r()
        },
        fail: (res) => {
          that.showModalVisible = true
          that.allowCameraRe = true
        }
      })
    })
    this.allowCamera = isOk
    return isOk
  }
async checkAllowRecoder(){
    this.currentAllow = 'recorder'
    let isOk = false
    let that = this
    await	new Promise(async r => {	
      if(!this.allowRecoder){
        uni.authorize({
        scope:'scope.record',
        success: (res) => {
          isOk = true
          that.showModalVisible = false   //这是个控制自定模态框显隐的状态
          r()
          console.log('麦克风11111');
        },
        fail: (res) => {
          console.log('为什么走进来麦克风失败')
          that.showModalVisible = true
          that.allowRecoderRE = true
        }
      })
      }
    })
    this.allowRecoder = isOk
    this.allowRecoderRE = true
    return isOk
  }
<!-- 手写一个提示未授权的showModal   -->
    <div class="confirm-wrap confirm-wraps" v-if="showModalVisible">
      <div class="confirm">
        <!-- <div class="confirm-title">即将拨打</div> -->
        <div class="confirm-content">
          检测到您有未开启的权限,为保证功能正常使用,请保持摄像头以及麦克风(录音)权限均为开启状态
        </div>
        <div class="confirm-btn">
          <button open-type="openSetting" @bindopensetting="openSetting">去开启</button>
        </div>
      </div>
    </div>
//点击上面的按钮就会调用
//手动写的模态框跳转权限页面  但是没有回调
  async openSetting(){
    let that = this
    await	new Promise(async r => {	
      uni.openSetting({
        success: (res) => {
          console.log('怎么没打印'+res);
          if(res.authSetting['scope.camera']==false){
            that.showModalVisible = true
          }else if(res.authSetting['scope.camera']){
            that.showModalVisible = false
            r()
          }
        }
      })
    })
  }
 //加个事件用于检测权限的开启状况  因为这里要监控摄像头以及麦克风两个权限,这个方法加在小程序功能开始之前
  async checkAllowRepeat(){
    let that = this
    let isOk = false
    await	new Promise(async r => {	
      uni.getSetting({
        success(res){
          console.log('重新看')
          console.log(res)
          if(that.currentAllow == 'camera'){
            if(res.authSetting['scope.camera']==false){
              if(that.allowCameraRe){
                that.showModalVisible = true
              }
            }else if(res.authSetting['scope.camera']){
              if(that.allowCameraRe){
                that.showModalVisible = false
              }
            }else{
              if(that.allowCameraRe){
                that.showModalVisible = true
              }
            }
          }else{
            if(res.authSetting['scope.record']==false){
              if(that.allowRecoderRE){
                that.showModalVisible = true
              }
            }else if(res.authSetting['scope.record']){
              if(that.allowRecoderRE){
                that.showModalVisible = false
              }
            }else{
              if(that.allowRecoderRE){
                that.showModalVisible = true
              }
            }
          }
        }
      })
    })
    if(that.showModalVisible==false){
      isOk = true
    }
    return isOk
  }

**方法三:**是有一个直接getSetting的方法,但是有问题,似乎第一次拿到之后,进行更改之后就看不到已修改的权限了,很奇怪,直接放弃试用,这个方法不先去检测授权,直接拿getSetting,输出的res.authorize中有时没有我想要的摄像头以及麦克风的权限,很奇怪的问题,所以没有实现
效果图:
一进页面就检测各项权限,只要有任何权限(摄像头或录音)未开启,就会弹出提示,必须开启,否则不能正常使用
1,点击“去开启”回调转到权限控制页面
在这里插入图片描述

2,权限控制界面
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_小郑有点困了

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值