App({ appData: { userInfo:{ user_portraitUrl: "", user_nick: "", user_gender: 0, // 1:男; 2:女; 0:未知 user_city: "", user_province: "", country: "" }, callBlack:{ openid:"", session_key:"", unionid:"" } }, onLaunch:function(){ var obj = this; // 获取 wx.login({ success: function (res) { // 判断是否登录成功 if (res.code) { // 登录成功后获取回调结果(含openID) wx.request({ url: 'https://api.weixin.qq.com/sns/jscode2session', data:{ appid: "appid", // 这里填写自己的appid secret: "secret", // 这里填写自己的secret js_code: res.code, grant_type: "authorization_code" }, success:function(res){ obj.appData.callBlack.openid = res.data.openid; obj.appData.callBlack.session_key = res.data.session_key; } }); wx.getUserInfo({ success: function (success_res) { // 登陆成功后对应取值 obj.appData.userInfo.user_portraitUrl = success_res.userInfo.avatarUrl; obj.appData.userInfo.user_nick = success_res.userInfo.nickName; obj.appData.userInfo.user_gender = success_res.userInfo.gender; obj.appData.userInfo.user_city = success_res.userInfo.city; obj.appData.userInfo.user_province = success_res.userInfo.province; obj.appData.userInfo.country = success_res.userInfo.country; } }) } else { console.log('获取用户登录态失败!' + res.errMsg) } } }) } })
wx.request获取回调信息的url地址请填写自己服务器的地址;
wx.getUserInfo 登录小程序并获取 code , 在 success 回调中获取userInfo;