前言
遠安網(wǎng)站建設公司創(chuàng)新互聯(lián)公司,遠安網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經(jīng)驗。已為遠安成百上千家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務好的遠安做網(wǎng)站的公司定做!
小程序開發(fā)中,現(xiàn)在一般都需要獲取微信用戶信息,如頭像/名字等.這樣在用戶第一次進入小程序時,微信端會彈出一個是否同意授權的消息提示框.但是如果用戶第一時間點擊了拒絕,或者用戶手誤點擊了拒絕,如果沒有了后續(xù)的操作,可能你的小程序就不能使用了,也就會失去這樣一位用戶.
所以,微信官方推薦了一個方法,就是在用戶第一次拒絕授權的時候,再給用戶一個選擇的機會.這樣能很好的解決上訴問題.下面以用戶需要授權兩個權限為例,方法如下:
在 APP.JS 先設置兩個全局變量 .用作記錄用戶是否授權
//判斷地理位置是否請求成功 var locationBool; //判斷用戶信息是否請求成功 var userInfoBool;
的 APP({})中
1.獲取用戶當前位置信息(獲取成功后將數(shù)據(jù)存入緩存,方便后面使用)
//獲取地理位置 wxGetlocation: function() { console.log('微信獲取地理') wx.getLocation({ type: 'wgs84', //請求成功 success: function(res) { locationBool = true; wx.setStorage({ key: 'locationWx', data: { applatitude: res.latitude, applongitude: res.longitude } }) }, fail: function() { locationBool = false; } }) },
2.獲取用戶資料信息:
這里需要注意的是: 現(xiàn)在一般開發(fā)小程序都需要做服務器登錄.由于后端的解密方式的選擇問題,后端可以只用用戶code 解密,也有可能需要用到 encryptedData/iv 這兩個參數(shù)拿去后臺解密,換取用戶token 來做更多的操作. 如需獲取encryptedData/iv 這兩個參數(shù), 必須在微信wx.login({})之后再去獲取 否則這兩個參數(shù)可能會出現(xiàn)問題
//獲取用戶信息(獲取之前必須login) wxGetUserInfo: function() { console.log('微信獲取用戶信息') wx.getUserInfo({ //請求成功 withCredentials: true, success: function(data) { userInfoBool = true; wx.setStorage({ key: 'UserInfoWx', data: data }) }, fail: function() { userInfoBool = false; } }) },
logInWx: function() { var _this = this; wx.login({ success: res => { console.log('微信登錄') console.log(res) //如果登錄成功 var usercode = res.code; //用戶code if (res.code) { wx.setStorage({ key: 'usercode', data: usercode }) //請求用戶信息設置緩存(獲得nickname/avatarurl/gender/iv/encryptedData) _this.wxGetUserInfo(); } } }) }
在小程序中,微信提供了這樣一個方法wx.openSetting({}) 他可以彈出小程序設置授權的頁面.下面即是 用戶拒絕授權之后的處理方法.
getPromission: function() { var _this = this; // 位置信息,用戶信息中其中一個數(shù)據(jù)沒有獲取到(--->彈出設置界面) if (locationBool == false || userInfoBool == false) { // 顯示提示彈窗(重新授權) wx.showModal({ title: '用戶未授權', content: '請開啟相應的權限哦~', showCancel: false, success: res => { if (res.confirm) { //點擊取消,重新獲取授權 wx.openSetting({ success: data => { console.log(data) if (data) { if (data.authSetting["scope.userInfo"] == true && data.authSetting["scope.userLocation"] == false) { //再次獲取用戶信息 console.log('只獲取用戶信息') _this.wxGetUserInfo() } else if (data.authSetting["scope.userInfo"] == false && data.authSetting["scope.userLocation"] == true) { //再次獲取位置信息 _this.wxGetlocation() console.log('只獲取位置信息') } else if (data.authSetting["scope.userInfo"] == true && data.authSetting["scope.userLocation"] == true) { //再次獲取用戶和位置信息 _this.wxGetUserInfo() _this.wxGetlocation() console.log('獲取全部信息') } } }, fail: function() { console.info("設置頁面失敗"); } }); } } }); } },
然后在app.js onlaunch中
onLaunch: function(e) { var _this = this _this.wxGetlocation(); //微信登錄 //如果沒有獲取數(shù)據(jù)成功. var timer = setInterval(function() { if (locationBool != undefined && userInfoBool != undefined) { clearInterval(timer) _this.getPromission(); } }, 200) },
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。
分享文章:微信小程序用戶拒絕授權的處理方法詳解
轉載來于:http://bm7419.com/article6/pcgcog.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、動態(tài)網(wǎng)站、網(wǎng)站排名、網(wǎng)站設計公司、Google、軟件開發(fā)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)