【小程序】小程序项目别的页面初始化拿到的值为两种状态
app.js文件
onLaunch: function () {console.log('app-onLaunch')
this.getUserInfo();
},
onShow:function(){
console.log('app-onShow')
},
// 获取用户信息,登录
getUserInfo: function (callBack) {
let that = this;
if (this.globalData.userInfo) {
typeof cb == 'function' && cb(this.globalData.userInfo)
} else {
wx.login({
success: function (res1) {
console.log('res11111111111111111',res1);
if (res1.code) {
//授权
wx.getUserInfo({
success: res => {
// 可以将 res 发送给后台解码出 unionId
//console.log('ryy-getUserInfo', res.userInfo)
that.globalData.userInfo = res.userInfo
wx.request({
url: that.config.HOST + '/user/userLogin.do',
data: {
code: res1.code, //返回openid
headimgurl: res.userInfo.avatarUrl,
nickname: res.userInfo.nickName,
city: res.userInfo.city,
appid: that.globalData.AppId,
province: res.userInfo.province,
shop_id: '10001',
},
header: {
'content-type': 'application/json'
},
success: function (res) {
console.log('tets1', res)
if (res && res.statusCode == 200) {
wx.setStorageSync('session_id', res.data.data.session_id)
that.globalData.header.Cookie = 'JSESSIONID=' + res.data.data.session_id;
that.globalData.is_vip = res.data.data.is_vip;
console.log('that.globalData-------------------++++++++++++++++++++', that.globalData)
//console.log('that.globalData.header', that.globalData.header.Cookie)
//console.log('ryy-qest' + JSON.stringify(res.data), res.data.data)
//有些操作需要登陆之后才能获取,但又是和login验证同时进行的话就需要加入到回调函数
if (callBack) {
callBack()
}
}
}
})
},
globalData: {userInfo: null,
openid:null,
user_id:null,
code:null,
header:{Cookie:null},
session_id:null,
img_domain:null,
telephone:null,
is_vip:null,
getInit:{},
},
其他page文件下的index.js页面
放在onload或者是其他生命周期内打印不出来cookie,一个是空值,一个是有值的
旁边出来一个感叹号提示:Value below was evaluated just now
但如果打印,console.log(app.globalData.header.Cookie)出来的是null
但是在自定义方法里边比如点击获取验证码里就能够获取到cookie的值
实在是不知道如何解决该问题好好,第一次做小程序,希望能有人指点一下
回答
估计全局没有存储上吧,应该是异步的事,我以前碰到过是由于异步还没有赋值成功,就使用,方面起见你可以加一个延时器试试
我想问一下,解决了吗
以上是 【小程序】小程序项目别的页面初始化拿到的值为两种状态 的全部内容, 来源链接: utcz.com/a/81527.html