onLaunch: function () {//生命周期函数--监听小程序初始化
var that = this;
//先验证微信自身登陆态是否过期
wx.checkSession({
success: function (res) {
//从缓存获取user_session
var user_session = wx.getStorageSync('user_session');
if (user_session == '') { //取不到user_session需要登陆
that.login();
}
else {
that.check(user_session); //取到user_session需要验证是否有效
}
},
fail: function (finfo) {
//wx session 到期,需要重新登陆
that.login()
}
})
console.log(111111111111)
},
设置完session缓存 在它回调里进行下面的操作不就行
把【在index页面拿缓存session】这件事做成异步的,就可以了。简单说就是:发一个“拿session”的命令,然后在异步回调函数里“使用session”。
因为onLaunch时检查 wx.checkSession的过程中会设置session缓存,但是我在index页面会有拿缓存session来发请求获取初始数据,因为还没设置完session缓存,index拿不到缓存session,发请求就失败了。
是因为onlaunch里的函数是异步的么?
很正常啊,哪里有问题?
wx.checkSession() 是异步的。