微信小程序聊天室(云开发)

                   

 

在写聊天是之前我们可以先看一需要建四个云数据表,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)
                }
              })
            }
          })
        }
      })
  },

 没有的话就将对应基本信息传到云函

  • 9
    点赞
  • 122
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值