做了个登录页面,在页面中用globalData时为null,数据库里面有信心,每次刷新都要重新登录,这个问题怎么解决,求大师帮帮忙。
me.js代码
const app = getApp();
// const db = wx.cloud.database();
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
console.log(app.globalData.userInfo)
this.setData({
userInfo:app.globalData.userInfo
})
},
// 获取头像
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){
console.log(event.detail.value.nickName);
let nickName =event.detail.value.nickName;
if(!nickName){
wx.showToast({
title:'请填写昵称',
icon:'error'
})
return
}
if(!this.data.avatarUrl){
wx.showToast({
title:'请上传头像',
icon:'error'
})
return
}
var that = this;
console.log(app.globalData.openid);
wx.cloud.database().collection('login_users').where({
_openid:app.globalData.openid
}).get({
success(res){
console.log(res)
if(res.data.length == 0){
//添加记录到数据库
wx.cloud.database().collection('login_users').add({
data:{
num:Date.now(),//!QQ号
avatarUrl:that.data.avatarUrl,
nickName:nickName
},
success(res){
console.log(res)
wx.showToast({
title:'登陆成功',
})
//设置userInfo这个对象
let userInfo = {};
userInfo.avatarUrl = that.data.avatarUrl;
userInfo.nickName = nickName;
that.setData({
userInfo
})
// //第一次登录成功后获取最新用户信息
that.getUseInfo()
}
})
}else{
that.setData({
userInfo:res.data[0]
})
}
}
})
},
loginOut(){
// console.log(event);
app.globalData.userInfo = null
this.setData({
userInfo:null
})
},
//修改个人信息
editUserinfo(){
wx.navigateTo({
url:'edit/edit'
})
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
console.log(app.globalData.userInfo);
this.setData({
userInfo:app.globalData.userInfo
})
},
//获取用户信息
getUseInfo(){
let that = this;
wx.cloud.database().collection('login_users').where({
_openid:app.globalData.openid
}).get({
success(result){
console.log(result)
app.globalData.userInfo = result.data[0]
}
})
}
})
aapp.js代码
App({
onLaunch: function () {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力');
} else {
wx.cloud.init({
env:'',
traceUser: true,
});
}
//获取用户的openid
var that = this;
wx.cloud.callFunction({
name:'getOpenid',
success(res){
console.log(res);
that.globalData.openid = res.result.openid
//查找数据库用户表里面是否有这个用户记录
wx.cloud.database().collection('login_users').where({
_openid:res.result.openid
}).get({
success(result){
console.log(result)
that.globalData.userInfo = result.data[0]
}
})
}
})
},
globalData:{
userInfo:null,
openid:null
}
});
异步问题,先取值后赋值
that.userInfoCallback(result.data[0]),授权之后要重新编译一下,才获取到。这种方式还有个问题就是,没有授权之前,用户信息就会报错,undefined,比如用户头像,用户昵称
// app.js
App({
onLaunch: function () {
if (!wx.cloud) {
console.error('请使用 2.2.3 或以上的基础库以使用云能力');
} else {
wx.cloud.init({
env: '',
traceUser: true,
});
}
this.globalData = {};
this.loadUserInfo();
},
async loadUserInfo() {
//获取用户的openid
var that = this;
return await wx.cloud.callFunction({
name:'logins',
}).then(res => {
console.log(res);
that.globalData.openid = res.result.openid;
wx.cloud.database().collection('login').get().then(result => {
console.log(result);
that.globalData.userInfo = result.data[0];
console.log(that.globalData.userInfo);
})
})
},
is_login(){
console.log(this.globalData.userInfo);
if (this.globalData.userInfo) {
return true
} else {
return false
}
},
})
https://developers.weixin.qq.com/s/kffn9Jmc7HR5
async loadUserInfo() {
var that = this;
return await wx.cloud.database().collection('login')
.where({
_openid:'openid'
}).get().then(result => {
console.log(result);
that.globalData.userInfo = result.data[0];
console.log(that.globalData.userInfo);
})
},