官方教材:
为优化用户体验,使用 wx.getUserInfo 接口直接弹出授权框的开发方式将逐步不再支持。从2018年4月30日开始,小程序与小游戏的体验版、开发版调用 wx.getUserInfo 接口,将无法弹出授权询问框,默认调用失败。正式版暂不受影响。
官方在4月30日调整了开发者获取用户信息的接口。需要用户来主动点击。
示例代码:
!-- 如果只是展示用户头像昵称,可以使用 open-data / 组件 --open-data type="userAvatarUrl"/open-dataopen-data type="userNickName"/open-data!-- 需要使用 button 来授权登录 --button wx:if="{{canIUse}}" open-type="getUserInfo" bindgetuserinfo="bindGetUserInfo"授权登录/buttonview wx:else请升级微信版本/view注意:使用button中的bindgetuserinfo需要小程序1.3.0以上的版本库
js
//jsPage({ data: { canIUse: wx.canIUse('button.open-type.getUserInfo') }, onLoad: function() { // 查看是否授权 wx.getSetting({ success: function(res){ if (res.authSetting['scope.userInfo']) { // 已经授权,可以直接调用 getUserInfo 获取头像昵称 wx.getUserInfo({ success: function(res) { console.log(res.userInfo) } }) } } }) }, bindGetUserInfo: function(e) { console.log(e.detail.userInfo) }})
我的代码:
wxml:button class='code' open-type='getUserInfo' bindgetuserinfo='getuserinfo'js: getuserinfo: function (e) { var that = this; if (e.detail.encryptedData !== undefined) { if (!app.globalData.unionId) {//此处我判断是unionid,没有unionid的话应该要openid wx.request({ url: 'url', method: 'POST', data: { encryptedData: e.detail.encryptedData, iv: e.detail.iv, }, success: function (data) { //存在全局变量中 //遇到官方php代码在小程序无法解析情况 if (typeof (data.data) == 'object') { app.globalData.openid = data.data.openId; app.globalData.userInfo = data.data; app.globalData.unionId = data.data.unionId; } else { var data1 = JSON.parse(data.data.trim()); app.globalData.openid = data1.openId; app.globalData.userInfo = data1; app.globalData.unionId = data1.unionId; } that.getuserinfo(e)//成功重新调用,来进入code事件 } }) } else { that.code()//执行别的js } } }PHP代码
?phpinclude_once "wxBizDataCrypt.php";//官方demo$post= file_get_contents("php://input");//接收POST数据$postdata=json_decode($post,TRUE);$appid = 'appid';$sessionKey = 'sessionkey';//登陆时存储在服务器上。5分钟有效$encryptedData = $postdata['encryptedData'];$iv = $postdata['iv'];$pc = new WXBizDataCrypt($appid, $sessionKey);$errCode = $pc-decryptData($encryptedData, $iv, $data );if ($errCode == 0) { print($data . "");} else { print($errCode . "");}官方demo下载

https://developers.weixin.qq.com/miniprogram/dev/api/signature.html#wxchecksessionobject














