微信更新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所带来的问题。