微信小程序定位模块或者访问权限拒绝一般要如何处理

当用户拒绝微信授权后,可以使用wx.getSetting检查授权状态,然后通过wx.openSetting引导用户进入设置界面打开权限。在用户授权后,利用wx.getLocation获取地理位置。若设备位置服务未开启,需提示用户开启。
摘要由CSDN通过智能技术生成

微信授权拒绝之后无法再次调用起授权弹框,所以可以通过调用设置权限按钮来打开权限信息

问题如下所示:

在这里插入图片描述

解决办法

具体思路:

**wx.getSetting()**查看授权情况
**wx.openSetting()**调用设置界面让用户打开权限
**wx.getLocation()**获取位置信息

代码实现:

1、微信小程序getLocation获取地理位置授权,首先需要在app.json中添加配置:

  "permission": {
      "scope.userLocation": {
        "desc": "你的位置信息将用于小程序定位,以便更好的体验功能"
      }
  },

2、wxml代码:

<view class="container">
  <view class="map-point-select" bindtap="mapSetting">获取地址信息</view>
</view>

3、js代码:

// index.js
// 获取应用实例
const app = getApp()

Page({
  data: {},
  onLoad() {},
  mapSetting: function () {
    let that = this;
    // 获取用户当前设置
    wx.getSetting({
      success: (res) => {
        // 微信授权拒绝之后无法再次调用起授权弹框,所以可以通过调用设置权限按钮来打开权限信息
        if (res.authSetting['scope.userLocation'] != undefined && res.authSetting['scope.userLocation'] != true) {
          wx.showModal({
            title: '',
            content: '我们需要获取你的地理位置,请确认授权',
            success: function (res) {
              if (res.cancel) {
                wx.showToast({
                  title: '您已拒绝授权',
                  icon: 'none'
                })
              } else if (res.confirm) {
                // 调起客户端小程序设置界面,返回用户设置的操作结果
                wx.openSetting({
                  success: function (dataAu) {
                    if (dataAu.authSetting["scope.userLocation"] == true) {
                      that.getLocation(dataAu);
                    } else {
                      wx.showToast({
                        title: '授权失败',
                        icon: 'none'
                      })
                    }
                  }
                })
              }
            }
          })
        }
        // 初始化进入,未授权
        else if (res.authSetting['scope.userLocation'] == undefined) {
          that.getLocation(res);
        }
        // 已授权
        else if (res.authSetting['scope.userLocation']) {
          that.getLocation(res);
        }
      }
    });
  },

  /**
   * 获取地理信息
   */
  getLocation: function (userLocation) {
    let that = this;
    wx.getLocation({
      // gcj02:返回国测局坐标,即经纬度加密后的坐标。
      type: "gcj02",
      success: function (res) {
        let latitude = res.latitude;
        let longitude = res.longitude;

        console.log(res);
      },
      fail: function (res) {
        if (res.errMsg === 'getLocation:fail:auth denied') {
          wx.showToast({
            title: '您已拒绝授权',
            icon: 'none'
          })
          return
        } 
        // 使用可选链简化代码并避免在访问不存在的属性时出现错误
        if (!userLocation?.authSetting?.['scope.userLocation']) {
          that.mapSetting();
        } else if (userLocation.authSetting?.['scope.userLocation']) {
          wx.showModal({
            title: '',
            content: '请在系统设置中打开定位服务',
            showCancel: false,
            success: result => {
            }
          })
        } else {
          wx.showToast({
            title: '授权失败',
            icon: 'none'
          })
        }
      }
    });
  },
})

4、如果设备未开启位置信息服务,授权成功后在wx.getLocation()方法中也会获取失败,需要在fail方法中提示用户开启手机位置信息。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值