小程式中彈起的授權框使用者點了拒絕授權如何處理
阿新 • • 發佈:2018-12-19
小程式中一些資訊需要使用者確認授權後我們才能獲取到,如位置,使用者資訊等等,有些授權框使用者點選拒絕授權後一段時間內不會再彈出,但我們又必須需要獲取相應資訊,這種狀況如何解決呢?
在調起授權彈窗方法的失敗回撥函式裡使用 wx.getSetting()=>在其成功回撥裡獲取到授權的狀態=>再呼叫wx.openSetting()方法會開啟調起過的授權項 取消的授權可以再次選確認授權(不一定非要完全和這個不走一樣,可以按自己需求進行刪減或增加)
下面以獲取使用者當前地理位置為例,貼上程式碼及效果。
wx.getLocation({ type: 'wgs84', success(res) { that.userlatitude = res.latitude; that.userlongitude = res.longitude; wx.request({ url: Url, data: { lat: that.userlatitude, lon: that.userlongitude }, header: { 'content-type': 'application/json' // 預設值 }, success(res) { that.addressInfo = res.data.address_info; that.distance = res.data.distance; wx.setStorage({ key: 'distance', data: that.distance }); that.$apply(); console.log(res); }, fail() { } }); }, fail() { //客戶拒絕授權時 wx.getSetting({ success(res) { var statu = res.authSetting; if (!statu['scope.userLocation']) { wx.showModal({ title: '是否授權當前位置', content: '需要獲取您的地理位置,請確認授權,否則配送服務將無法正常使用', success(tip) { if (tip.confirm) { wx.openSetting({ success(data) { if (data.authSetting["scope.userLocation"] === true) { wx.showToast({ title: '授權成功', icon: 'success', duration: 1000 }) //授權成功之後,再呼叫wx.getLocation獲取地址資訊 wx.getLocation({ type: 'wgs84', success(res) { that.userlatitude = res.latitude; that.userlongitude = res.longitude; wx.request({ url: Url, data: { lat: that.userlatitude, lon: that.userlongitude }, header: { 'content-type': 'application/json' // 預設值 }, success(res) { that.addressInfo = res.data.address_info; that.distance = res.data.distance; wx.setStorage({ key: 'distance', data: that.distance }); that.$apply(); } }); } }) } } }) } } }) } } }); } });
效果如下:(這是我調獲取使用者位置授權 使用者點了取消後的, 由於我前面還獲取了使用者資訊,所以會有兩項)