***小程序wx.getUserInfo不能彈出授權窗口後的解決方案
微信更新api後,wx.getUserInfo在開發和體驗版本都不能彈出授權窗口。微信文檔說明:
註意:此接口有調整,使用該接口將不再出現授權彈窗,請使用 <button open-type="getUserInfo"></button> 引導用戶主動進行授權操作
- 當用戶未授權過,調用該接口將直接報錯
- 當用戶授權過,可以使用該接口獲取用戶信息
對此,給出以下解決方案。
wx.getUserInfo({ withCredentials: true, success: function (res) { //此處為獲取微信信息後的業務方法 }, fail: function () { //獲取用戶信息失敗後。請跳轉授權頁面 wx.showModal({ title: ‘警告‘, content: ‘尚未進行授權,請點擊確定跳轉到授權頁面進行授權。‘, success: function (res) { if (res.confirm) { console.log(‘用戶點擊確定‘) wx.navigateTo({ url: ‘../tologin/tologin‘, }) } } }) } })
調取該方法失敗後跳轉到授權頁面。
授權頁面加入
<button open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo">授權登錄</button>
並在js中,加入這個方法
bindGetUserInfo: function(e){ var that = this; //此處授權得到userInfo console.log(e.detail.userInfo); //接下來寫業務代碼
//最後,記得返回剛才的頁面 wx.navigateBack({ delta: 1 }) }
至此,即可完成引導用戶手動授權的過程。解決此次更新api所帶來的問題。
轉自:https://blog.csdn.net/xishuchen0520/article/details/80410861
【關於微信小程序登錄信息】 微信即將不再支持wx.getUserInfo() 授權彈出框 2018年5月12日
轉自:https://blog.csdn.net/u013408059/article/details/80291239
先看一張圖: 這是地址:點擊打開鏈接
顯示微信可能處於安全考慮,這個方法已經不能用於自動獲取用戶信息了,
大致意思就是:
以前這個方法 如果用戶未授權獲取用戶信息, 你調用這個wx.getUserInfo() , 小程序會彈出一個授權的彈出框。類似:
然後你再去
this.globalData.userInfo = res.userInfo,去獲取用戶信息但是!!!
現在的意思是: 你直接調用這個方法依舊可以獲取用戶信息, 但是如果你之前沒有點擊過那個授權的小彈出框,這個方法就直接報錯了,需要你直接去引導用戶去授權。
那麽怎麽授權呢,
說的很明顯,你需要一個button去彈出這個授權框框,引導用戶去授權。
這個button怎麽看呢,無非就是寫一個點擊事件,button必須設置一個屬性:open-type=‘getUserInfo‘ ,設置好了之後,你點擊這個button就會彈出這個授權框,然後你在調用wx.getUserInfo() 就會獲取信息啦,方法吧。
記錄一下。
wx.getUserInfo({ success: res => { console.log(123); // 可以將 res 發送給後臺解碼出 unionId this.globalData.userInfo = res.userInfo
// 由於 getUserInfo 是網絡請求,可能會在 Page.onLoad 之後才返回 // 所以此處加入 callback 以防止這種情況 if (this.userInfoReadyCallback) { this.userInfoReadyCallback(res) } } , fail:res=>{ // 獲取失敗的去引導用戶授權 })
怎麽查詢授權信息呢:微信同樣給了答案,
https://developers.weixin.qq.com/miniprogram/dev/api/setting.html#wxgetsettingobject
判斷這個玩意下面的userInfo ;
你以為這樣就完了?我查閱了這個玩意兒的屬性表:
whf??!!!
===============2018年5月16日20:28:11============
下面這個就不要看了,微信社區中回復或這是他們失誤沒去掉, 大家以後繼續用就行.
顯示廢棄了!:
我是不知道怎麽弄了。大不了就不去查詢授權信息了,直接查詢userinfo是不是非空不就得了。。。。
===============2018年5月16日20:28:14===========
以上。
===========================================================================
拒絕授權返回信息:
{
"type": "getuserinfo",
"timeStamp": 6012,
"target": {
"id": "",
"offsetLeft": 0,
"offsetTop": 380,
"dataset": {}
},
"currentTarget": {
"id": "",
"offsetLeft": 0,
"offsetTop": 380,
"dataset": {}
},
"detail": {
"errMsg": "getUserInfo:fail auth deny"
}
}
授權之後的返回信息格式:
{
"type":"getuserinfo",
"timeStamp":123686,
"target":{
"id":"",
"offsetLeft":0,
"offsetTop":380,
"dataset":{
}
},
"currentTarget":{
"id":"",
"offsetLeft":0,
"offsetTop":380,
"dataset":{
}
},
"detail":{
"errMsg":"getUserInfo:ok",
"rawData":"{}",
"userInfo":{
"nickName":"王**",
"gender":1,
"language":"zh_CN",
"city":"Haidian",
"province":"Beijing",
"country":"China",
"avatarUrl":""
},
"signature":"",
"encryptedData":"",
"iv":""
}
}
總之還是去看文檔吧,比較良心了。
微信公眾平臺userinfo信息格式
微信小程序~wx.getUserInfo逐漸廢棄,小程序登錄過程優化
很多的時候我們在做小程序應用的時候,希望用戶在使用小程序前進行登錄授權,之前登錄後通過wx.getUserInfo直接彈出授權的登錄方式官方的意思是將不再支持,而是讓用戶通過下面的方式授權用戶信息
<button open-type="getUserInfo" bindgetuserinfo="getUserInfoAction">授權用戶信息</button> 這樣的話當小程序在使用前一定需要用戶登錄,或者已經進行到需要用戶登錄的操作時;這樣的話就需要一個button授權頁面。這種改變,感覺個人還是喜歡默認彈層的的授權方式,這個方式可能一時不適應吧,有種排斥。 下面是通過button授權的方式做的一個登錄:這裏我只是展示了login.js 與 index.js 過程,有不對或者不好的地方歡迎加群交流指正。 login.js1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
const ajax = require("../../common/ajax.js")
const tips = require("../../common/tips.js")
Page({
/**
* 頁面的初始數據
*/
data: {
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
let that = this;
},
getUserInfoAction(res){
let that = this;
const encryptedData = res.detail.encryptedData;
const iv = res.detail.iv;
if (encryptedData && iv){
// console.log("允許")
that.login().then((login)=>{
const params = {
"code": login.code,
"encryptedData": encryptedData,
"iv": iv,
"type": "small_wechat"
}
ajax.posts(params, "api/passport/thirdSign").then((res) => {
let userinfo = {
avatar: res.data.data.avatar,
nickname: res.data.data.nickname,
token: res.data.data.token,
user_id: res.data.data.user_id
}
wx.setStorageSync("userinfo", userinfo);
// console.log(wx.getStorageSync("userinfo"));
if (wx.getStorageSync("userinfo")){
wx.redirectTo({
url: ‘/page/index/index‘
})
}
}).catch((errMsg) => {
tips.showToast("網絡連接失敗", "none")
console.log(errMsg)
})
}).catch((errMsg) => {
console.log("登錄:" + errMsg)
})
}else{
// console.log("拒絕")
tips.showToast("請授權公開信息,登錄小程序", "none")
}
},
login(){
// 登錄
let promise = new Promise((resolve, reject) => {
wx.login({
success: function (res) {
if (res.code) {
resolve(res)
} else {
tips.showToast("登錄失敗", "none")
}
},
fail: function (err) {
reject(err)
}
})
})
return promise;
}
})
|
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
const tips = require("../../common/tips.js")
Page({
/**
* 頁面的初始數據
*/
data: {
},
/**
* 生命周期函數--監聽頁面加載
*/
onLoad: function (options) {
let that = this;
if (!wx.getStorageSync("userinfo")) {
//是否登錄
that.isloginindex()
}
},
isloginindex() {
//是否進入首頁
if (wx.getStorageSync("userinfo")) {
console.log("登錄")
} else {
//無信息
console.log("否登錄")
wx.redirectTo({
url: ‘/page/login/login‘
})
}
}
})
|
***小程序wx.getUserInfo不能彈出授權窗口後的解決方案