微信小程序,用户拒绝授权后重新授权;uni-app小程序,用户拒绝授权后点击无效;重新进入后拉起位置授权框;
問題:當(dāng)用戶第一次進入小程序,點擊授權(quán)按鈕后,點了拒絕,再次點擊不會出現(xiàn)授權(quán)頁面,只有再次進入小程序的時候,才會出發(fā)請求授權(quán) 。
案例: 假如我們獲取微信位置,第一次點擊的時候彈起授權(quán),用戶點擊的拒絕,當(dāng)用戶再次點擊的時候,如何繼續(xù)彈出授權(quán)?
實現(xiàn)步驟:
1、首先我們需要判斷用戶是否開啟授權(quán)獲取地理位置(wx.getSetting)
2、如果已經(jīng)授權(quán),直接獲取位置(wx.getLocation)
3、如果沒有授權(quán),彈出授權(quán)框(wx.authorize)
— 3.1 點擊確定之后,會走success,獲取位置
— 3.2 點擊拒絕之后,會走fail,這是會發(fā)現(xiàn)當(dāng)我們再次點擊的時候會發(fā)現(xiàn)已經(jīng)彈不出授權(quán)框了,
所以我們要在fail里面做一下處理,需要讓他彈出授權(quán)框
4、fail處理:
由于wx.openSetting必須通過按鈕觸發(fā),所以我們當(dāng)用戶拒絕的時候添加一個彈出框(wx.showModal)
— 4.1 當(dāng)用戶點擊彈框的確定按鈕的時候,調(diào)用wx.openSetting,微信會自動再次拉起位置授權(quán)框;
— 4.2 當(dāng)用戶點擊彈框的取消按鈕的時候,就提示即可。
代碼如下,可直接復(fù)制(前提是你在manifest.json配置好了permission權(quán)限);未配置看這篇
<template><view><button type="" @click="getLocation">獲取位置</button><view>經(jīng)度:{{x}}</view><view>緯度:{{y}}</view></view> </template><script> export default {data () {return {x: 0,y: 0}},methods: {getLocation () {let that = this// 獲取用戶是否開啟 授權(quán)獲取當(dāng)前的地理位置、速度的權(quán)限。uni.getSetting({success (res) {console.log(res)// 如果沒有授權(quán)if (!res.authSetting['scope.userLocation']) {// 則拉起授權(quán)窗口uni.authorize({scope: 'scope.userLocation',success () {//點擊允許后--就一直會進入成功授權(quán)的回調(diào) 就可以使用獲取的方法了uni.getLocation({type: 'wgs84',success: function (res) {that.x = res.longitudethat.y = res.latitudeconsole.log(res)console.log('當(dāng)前位置的經(jīng)度:' + res.longitude)console.log('當(dāng)前位置的緯度:' + res.latitude)uni.showToast({title: '當(dāng)前位置的經(jīng)緯度:' + res.longitude + ',' + res.latitude,icon: 'success',mask: true})}, fail (error) {console.log('失敗', error)}})},fail (error) {//點擊了拒絕授權(quán)后--就一直會進入失敗回調(diào)函數(shù)--此時就可以在這里重新拉起授權(quán)窗口console.log('拒絕授權(quán)', error)uni.showModal({title: '提示',content: '若點擊不授權(quán),將無法使用位置功能',cancelText: '不授權(quán)',cancelColor: '#999',confirmText: '授權(quán)',confirmColor: '#f94218',success (res) {console.log(res)if (res.confirm) {// 選擇彈框內(nèi)授權(quán)uni.openSetting({success (res) {console.log(res.authSetting)}})} else if (res.cancel) {// 選擇彈框內(nèi) 不授權(quán)console.log('用戶點擊不授權(quán)')}}})}})} else {// 有權(quán)限則直接獲取uni.getLocation({type: 'wgs84',success: function (res) {that.x = res.longitudethat.y = res.latitudeconsole.log(res)console.log('當(dāng)前位置的經(jīng)度:' + res.longitude)console.log('當(dāng)前位置的緯度:' + res.latitude)uni.showToast({title: '當(dāng)前位置的經(jīng)緯度:' + res.longitude + ',' + res.latitude,icon: 'success',mask: true})}, fail (error) {uni.showToast({title: '請勿頻繁調(diào)用!',icon: 'none',})console.log('失敗', error)}})}}})}}, } </script><style> </style>總結(jié)
以上是生活随笔為你收集整理的微信小程序,用户拒绝授权后重新授权;uni-app小程序,用户拒绝授权后点击无效;重新进入后拉起位置授权框;的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab vl_feat,matla
- 下一篇: 使用std::thread线程相关函数,