app.js异步问题,在我的页面中,可以拿到app.globalData的信息,但是app.globalData.userInfo拿不到,下面是全部代码,要怎么改?求帮助
app.js代码:
// app.js
App({
onLaunch:async function () {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力');
} else {
wx.cloud.init({
env: 'yingshiji-1g4z5k3lc74cf9e2',
traceUser: true,
});
}
this.globalData = {};
await this.loadUserInfo();
console.log('是否有openid',this.globalData.openid);
console.log('是否有用户信息',this.globalData.userInfo);
},
async loadUserInfo(){
var that = this;
//获取用户的openid
let useropenid = await wx.cloud.callFunction({
name:'logins',
})
console.log('获取openid',useropenid);
that.globalData.openid = await useropenid.result.appid;
return await wx.cloud.database().collection('login').where({
_openid:'that.globalData.openid'
}).get().then(res=>{
console.log(res);
that.globalData.userInfo = res.data[0];
})
},
is_login(){
console.log(this.globalData.userInfo);
if (this.globalData.userInfo) {
return true
} else {
return false
}
},
})
login WXML代码
<button class="but" open-type="chooseAvatar" bindchooseavatar="getAvatar">
<image class="face" src="{{avatarUrl?avatarUrl:'../../images/avatar.png'}}"></image>
</button>
<form bindsubmit="login">
<input class="nick" type="nickname" placeholder="请输入昵称" name="nickName" />
<button form-type="submit" type="primary">登陆</button>
</form>
login .js代码
```javascript
// pages/login/login.js
const app = getApp();
const db = wx.cloud.database();
Page({
data: {
},
onLoad(options) {
},
// 获取头像
getAvatar(event) {
console.log(event.detail.avatarUrl); //临时头像路劲地址
let avatarUrl = event.detail.avatarUrl; //临时地址
let suffix = /\.[^\.]+$/.exec(avatarUrl)[0];
wx.cloud.uploadFile({
cloudPath: 'loginImages/' + new Date().getTime() + suffix,
filePath: avatarUrl //上传到云端的路劲,
}).then(res => {
console.log(res.fileID);
this.setData({
avatarUrl: res.fileID
})
})
},
login(event) {
var that = this;
console.log(event.detail.value.nickName);
let nickName = event.detail.value.nickName;
// let avatarUrl = that.data.avatarUrl;
if (!nickName) {
wx.showToast({
title: '请填写昵称',
icon: 'error'
})
return
}
if (!this.data.avatarUrl) {
wx.showToast({
title: '请上传头像',
icon: 'error'
})
return
}
console.log(app.globalData.openid);
db.collection('login').where({
_openid: app.globalData.openid
}).get().then(res => {
console.log(res)
if (res.data.length == 0) {
//添加记录到数据库
db.collection('login').add({
data: {
num: Date.now(), //!QQ号
avatarUrl: that.data.avatarUrl,
nickName: nickName
},
success(res) {
console.log(res)
wx.showToast({
title: '登陆成功',
})
setTimeout(function () {
wx.navigateBack({})
}, 1500)
}
})
} else {
console.log("用户已存在")
that.setData({
userInfo: res.data[0]
})
}
})
}
})
我的页面user-center。WXML代码:
```xml
<view class="userinfo-group">
<!-- 已登陆 -->
<view class="user-group" wx:if="{{userInfo != null}}">
<image class="avatra" src="{{avatarUrl?avatarUrl:'../../images/avatar.png'}}"></image>
<view class="right-group">
<view class="nickname">用户名:{{nickName}}</view>
<view class="ID">用户ID:dfd9765490</view>
</view>
</view>
<!-- 未登陆 -->
<block wx:if="{{userInfo == null}}">
<view class="user-group" bind:tap="onuserInfo">
<image class="avatra" src="../../images/avatar.png"></image>
<view class="right-group">
<view class="nickname">点击登录</view>
<view class="ID">登录获取更多权限</view>
</view>
</view>
</block>
<view class="list-group">
<view class="balance">余额:¥ 0 元</view>
<view class="recharge">充值</view>
</view>
<view class="me-release">我发布的信息</view>
</view>
我的页面user-center.js代码:
// pages/user-center/user-center.js
const app = getApp();
const db = wx.cloud.database();
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log('是否有app信息',app.globalData)// 有app的信息
console.log('是否有用户信息',app.globalData.userInfo)// 获取用户信息和openid为undefined
console.log(app.is_login());
if(app.is_login()){
this.setData({
userInfo: app.globalData.userInfo,
avatarUrl: app.globalData.userInfo.avatarUrl,
nickName: app.globalData.userInfo.nickName
})
}
},
onuserInfo(){
wx.navigateTo({
url:'/pages/login/login'
})
},
})