在写聊天是之前我们可以先看一需要建四个云数据表,user(用户列表),qunList (群列表),qunUserList(群用户列表),news(消息列表)接下来就是页面布局,这个布局看个人其需求我这里就讲讲大概逻辑,数据的增删改查我上篇文章已经讲过了,今天我就不详细写了
一.登录
1.必须有个授权按钮,授权之后将用户基本信息跟opinId存入缓存,跟数据库
<button class="login" open-type="getUserInfo" bindgetuserinfo="login">授权登录</button>
登录页面加载的时候判断用户是否受过权,我这里用的是缓存中的openId,判断缓存中是否有openId,有直接跳转到首页,
onLoad(options) {
that = this
if (wx.getStorageSync('openId')) {
wx.reLaunch({
url: '../group/group',
})
}
},
没有就点击授权,获取用户基本信息;然后判断数据库中是否有对应用户信息,有的话把基本信息跟opinId存入缓存然后跳转首页
// 登录
login() {
wx.showLoading({
title: '加载中,请稍等...',
})
wx.login({
success: function(res) {
code = res.code
wx.getUserInfo({
success: function(res) {
name = res.userInfo.nickName
hend = res.userInfo.avatarUrl
wx.setStorageSync('userInfo', res.userInfo)
wx.cloud.callFunction({
name: "openId",
success(res) {
wx.setStorageSync('openId', res.result.openid)
openId = res.result.openid
// 判断数据库中是否已经有数据
DB.collection('user').where({
_openId: openId,
})
.get({
success: function(res) {
console.log(1234321, res)
if (res.data.length == 0) {
console.log(1)
that.userAdd()
} else {
wx.reLaunch({
url: '../group/group',
})
}
}
})
},
fail(res) {
console.log('登录失败', res)
}
})
}
})
}
})
},
没有的话就将对应基本信息传到云函