Taro-react 中小程序用户授权地理位置以及拒绝授权的处理

1.使用 Taro.getSetting 使用户进行授权地理位置信息

    使用成功结果中authSetting["scope.userLocation"]  为true 弹出提示用户授权信息

    否则用户拒绝授权 

2.使用Taro.getLocation获取用户地理位置,成功与失败的操作,具体看代码

// 获取用户地理位置授权
    Taro.getSetting({
      success: (res) => {
        res.authSetting = {
          "scope.userInfo": true,
          "scope.userLocation": true
        }

        // 用户同意授权
        if (res.authSetting["scope.userLocation"]) {
          // 获取用户地理位置的经度和纬度
          Taro.getLocation({
            type: "gcj02",
            success: function (res) {
              gpsLong = res.longitude
              gpsLat = res.latitude
              setLongitude(res.longitude);
              setLatitude(res.latitude);
              init();
            },
  //用户拒绝授权
            fail: (res) => {
              isAuthorized()
            },
          });
        }
      },
      fail: () => {
      },
    });
//用户拒绝授权,提醒用户需要开启位置授权
const isAuthorized = ()=>{
    Taro.showModal({
      title: "温馨提示",
      content: "需要获取您的位置信息,请允许",
      success: (tip) => {
        if (tip.confirm) {
          Taro.openSetting({
            success: (data) => {
              if (data.authSetting["scope.userLocation"]) {
                Taro.getLocation({
                  success: (res) => {
                    gpsLong = res.longitude
                    gpsLat = res.latitude
                    setLongitude(res.longitude);
                    setLatitude(res.latitude);
                    init();
                  },
                });
              }
            },
          });
        } else if (tip.cancel) {
          Taro.navigateBack();
        }
      },
    });
  }

taro授权用户摄像头 ,用户是否开启摄像头 通过 authSetting中的scope.camera 来判断

Taro.getSetting({
      success: (res) => {
        if (res.authSetting["scope.camera"] === undefined) {
          Taro.authorize({
            scope: "scope.camera",
            success: () => {
            },
            fail: () => {
              Taro.showModal({
                title: "授权请求",
                content: "尚未允许使用摄像头,请点击确定进行授权",
                success: (tip) => {
                  if (tip.confirm) {
                    Taro.openSetting({
                      success: (e) => {
                        console.log("eeeee", e);
                      },
                    });
                  } else if (tip.cancel) {
                    useAuthorizationCamera();
                  }
                },
              });
            },
          });
        }
      },
      fail: () => {
      },
    });

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值