app.js页面App({ globalData: { userInfo: “”, js_code: “”, openid: “”, session_key: “” }, getUserInfo: function (cb) { var that = this; if (that.globalData.userInfo) { typeof cb == “function” && cb(that.globalData.userInfo) } else { wx.login({ success: res => { that.globalData.js_code = res.code wx.getUserInfo({ success: res => { that.globalData.userInfo = res.userInfo; typeof cb == “function” && cb(that.globalData.userInfo); // 请求官方接口,获取openid和session_key wx.request({ url: “https://api.weixin.qq.com/sns/jscode2session”, data: { appid: “********”, secret: “*******”, js_code: that.globalData.js_code, grant_type: “authorization_code” }, success: function (res) { that.globalData.openid = res.data.openid; that.globalData.session_key = res.data.session_key; }, fail: function () { } }) } }) } }) } }})index.js页面Page({ data: { userInfos:{} }, onLoad: function (options) { var that = this; app.getUserInfo(); console.log(app.globalData); }})
在index中打印app.globalData结果是下图,而直接调用全局变量会得到初始值"",应该怎么解决?怎么获取下面的数据?