const app = getApp()
const loginUrl = 'http://127.0.0.1:8090/api/mini/login';
Page({
data: {
userInfo: {},
hasUserInfo: false,
canIUse: wx.canIUse('button.open-type.getUserInfo')
},
onLoad: function () {
if (app.globalData.userInfo) {
this.setData({
userInfo: app.globalData.userInfo,
hasUserInfo: true
})
} else if (this.data.canIUse) {
app.userInfoReadyCallback = res => {
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
} else {
wx.getUserInfo({
success: res => {
app.globalData.userInfo = res.userInfo
this.setData({
userInfo: res.userInfo,
hasUserInfo: true
})
}
})
}
},
onReady: function(){
console.log('ready:',this.data.hasUserInfo,this.data)
if (this.data.canIUse && (this.data["hasUserInfo"])) {
wx.switchTab({
url: '/pages/home/home',
})
}
},
getUserInfo: function(e) {
app.globalData.userInfo = e.detail.userInfo
this.setData({
userInfo: e.detail.userInfo,
hasUserInfo: true
})
this.checkSession();
},
checkSession: function(){
wx.checkSession({
success: function () {
console.log('用户已授权')
wx.switchTab({
url: '/pages/home/home',
})
},
fail: function () {
console.log('未授权,开始授权...')
wx.login({
success: function (res) {
wx.request({
url: loginUrl,
data: { code: res.code },
method: 'Get',
header: { 'content-type': 'application/json' },
success: function (res) {
wx.setStorage({
key: 'third_session',
data: res.data.third_session,
})
}
});
}
})
}
})
}
})
onReady函数打印的日志如下:
请问是什么原因导致不一样的,我应该怎么做才能取到data中正确的数据。谢谢,感谢您的解答!
https://zhuanlan.zhihu.com/p/142764985 查看下 Chrome中devtool的延迟加载,主要是基于性能考虑
异步的问题~以下面实际输出为准。