微信小程序 获取位置权限,用户拒绝授权后重新引导用户授权

在微信小程序开发中,有很多场景需要强依赖用户位置信息,但获取位置信息必须要显式的经过用户授权,然而很多用户对授权比较敏感,可能会点击拒绝,本文介绍如何用户拒绝授权后重新引导用户授权

第一步:请求用户进行位置信息的授权,我们需要使用uni.authorize实现

        1.如果用户之前已经授权过,那么会直接回调success

        2.如果用户是第一次进入,则会弹出提示框提醒用户授权

                2.1 如果用户点击允许,则会执行接下来获取用户位置信息的逻辑

                2.2 如果用户点击拒绝,由于场景业务依赖于位置,必须要用户授权,此时弹出提示框引导用户授权,拒绝后再次弹窗,反复弹窗直到授权为止

代码贴在下面:

// 获取位置权限
getLocationDetail(){
  let that = this
  return new Promise((resolve,reject)=>{
	  // 请求用户授权,第一次进入会有位置授权的提示
	  uni.authorize({
		  scope: 'scope.userLocation',
		  success() {
			 console.log("用户成功授权位置信息,接下来去执行位置相关逻辑就可以了")
			 that.getUserLocation()
		  },
		  fail() {
		      console.log("用户拒绝授权位置信息,再次提示用户授权")
			  that.showRefuseLocationPermission()
		  }
	  })
	})
},

第二步:如果用户点击允许的情况下,执行获取用户位置信息的逻辑

代码如下:

// 获取位置信息--用户授权后执行
getUserLocation() {
    const that = this;
    uni.getLocation({
        type: 'gcj02',
        success(res) {
            console.log("获取用户位置成功")
            that.location = res
            // 解析位置信息等操作。。
            resolve(res)
        },
        fail(res) {
            console.log('获取用户位置失败,其他问题')
            reject(res)
        },
    })
},

第三步 : 用户如果点击拒绝

如果用户点击拒绝,就会进入到uni.authorize的fail的回调函数里,我们必须在这里处理拒绝授权的情况,因为我们必须要得到用户的授权,所以就在这里弹出提示框引导用户进入设置页面授权,如果用户依然拒绝,则反复弹窗,直到用户授权,

代码贴在下面:

// 用户拒绝授权的展示
showRefuseLocationPermission() {
    const that = this;
    wx.showModal({
        title: "提示",
        content: "需要获取用户位置权限",
        confirmText: "前往设置",
        showCancel: false,
        success(res) {
            if (res.confirm) {
                uni.openSetting({
                    success: (res) => {
                        console.log("打开设置成功", res);
                        if (res.authSetting['scope.userLocation']) {
                            console.log('成功授权userLocation')
                            that.getUserLocation()
                        } else {
                            console.log('用户未授权userLocation')
                            // 递归调用本函数,(函数套函数)
                            that.showRefuseLocationPermission()
                        }
                    },
                    fail: (err) => {
                        console.log("打开设置失败", err)
                    }
                })
            }
        }
    })
},

至此整个流程就结束了,以下是整体完整的代码:

// 获取位置权限
getLocationDetail() {
    let that = this
    return new Promise((resolve, reject) = >{
        // 请求用户授权,会有位置授权的提示
        uni.authorize({
            scope: 'scope.userLocation',
            success() {
                console.log("用户授权位置信息")
                that.getUserLocation()
            },
            fail() {
                that.showRefuseLocationPermission()
            }
        })
    })
},

// 获取位置信息--用户授权后执行
getUserLocation() {
    const that = this;
    uni.getLocation({
        type: 'gcj02',
        success(res) {
            console.log("获取用户位置成功") 
            that.location = res 
            // 解析位置信息等操作。。
            resolve(res)
        },
        fail(res) {
            console.log('获取用户位置失败,其他问题') 
            reject(res)
        },
    })
},
// 用户拒绝授权的展示
showRefuseLocationPermission() {
    const that = this;
    wx.showModal({
        title: "提示",
        content: "需要获取用户位置权限",
        confirmText: "前往设置",
        showCancel: false,
        success(res) {
            if (res.confirm) {
                uni.openSetting({
                    success: (res) = >{
                        console.log("打开设置成功", res);
                        if (res.authSetting['scope.userLocation']) {
                            console.log('成功授权userLocation') 
                            that.getUserLocation()
                        } else {
                            console.log('用户未授权userLocation')
                            // 递归调用本函数,(函数套函数)
                            that.showRefuseLocationPermission()
                        }
                    },
                    fail: (err) = >{
                        console.log("打开设置失败", err)
                    }
                })
            }
        }
    })
},
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值