用户聊天部分

3.多人测试时,两个用户都不在同个表上它们会各自创建表(思路没想对,导致两个人各聊各的)

解决方法:第一,要让两个人在同一张表上,先判断这个作品是否由本人发布,

const openid = wx.getStorageSync('openid')
    const {
      avatarUrl,
      nickName
    } = wx.getStorageSync('userInfo')//用户的数据
    const {
      acount,
      _openid
    } = this.data.info//作品发布者数据
    if(openid===_openid){
      wx.showToast({
        title: '该作品由您发布哟~',
        icon:'none'
      })
    }else{
    ....
    }

然后从缓存中dbId去获取存储的聊天表id(在talk消息页面会显示主动聊天和被动接受的消息框),如果dbId的长度为0或者遍历的结果为空则表明未聊过天可创建表,否则就跳转到之前的表id进行数据渲染,这里采用try…catch去跳出forEach循环,其实感觉写复杂了,暂时只想到这个方法。

 //else接下来部分
 const dbId = wx.getStorageSync('dbId')
    try{
      if (dbId.length === 0) {
        //  抛出错误跳出循环
          throw Error()
        }
      dbId.forEach(item => {
        // console.log(item)
        db.collection('chat_record').where({
          _id: item
        }).get({
          success: (res) => {
            // console.log(res)
            if (res.data.length === 0) {
            //  抛出错误跳出循环
              throw Error()
            } else {
              // 长度不为0就进行表内容判断
              const {
                userA_id,
                userB_id
              } = res.data[0]
              // 在每一张数据表里进行查询可以拿到自己缓存里的openid去和userA和userB判断,另一个就是发布作者的id
              // ps:如果同一个作者发布多个作品,且已经和该作者沟通过创建表,就会返回之前的聊天记录
              if (openid === userA_id && _openid === userB_id || openid === userB_id && _openid === userA_id) {
                // 证明已聊过天或者已创建表
                // 跳转到消息页面
                // console.log(111)
                wx.navigateTo({
                  url: `../chat/chat?id=${item}`,
                })
                
              } else {
                // 添加数据,建立沟通表
                db.collection('chat_record').add({
                  data: {
                    // 从本地的缓存信息里面获取
                    userA_id: openid, //个人id
                    userA_account_id: nickName, //账号
                    userA_avatarUrl: avatarUrl, //头像
                    // 对方信息
                    userB_id: _openid, //个人id
                    userB_account_id: acount, //账号
                    userB_avatarUrl: this.data.info.avatarUrl, //头像
  
                    record: [],
                    // friend_status : false,
                    time: formatTime(new Date())
                  },
                  success(res) {
                    // console.log(res._id)数据表id
                    const dbId = wx.getStorageSync('dbId')
                    if (dbId) {
                      dbId.push(res._id)
                      wx.setStorageSync('dbId', dbId)
                    } else {
                      const arr = []
                      arr.push(res._id)
                      wx.setStorageSync('dbId', arr)
                    }
  
                    // 跳转到消息页面
                    wx.navigateTo({
                      url: `../chat/chat?id=${res._id}`,
                    })
  
                  }
                })
              }
            }
          }
        })
      })
    }catch{
      // 跳出循环后的操作
      // 添加数据,建立沟通表
      db.collection('chat_record').add({
        data: {
          // 从本地的缓存信息里面获取
          userA_id: openid, //个人id
          userA_account_id: nickName, //账号
          userA_avatarUrl: avatarUrl, //头像
          // 对方信息
          userB_id: _openid, //个人id
          userB_account_id: acount, //账号
          userB_avatarUrl: this.data.info.avatarUrl, //头像

          record: [],
          // friend_status : false,
          time: formatTime(new Date())
        },
        success(res) {
          // console.log(res._id)数据表id
          const dbId = wx.getStorageSync('dbId')
          if (dbId) {
            dbId.push(res._id)
            wx.setStorageSync('dbId', dbId)
          } else {
            const arr = []
            arr.push(res._id)
            wx.setStorageSync('dbId', arr)
          }

          // 跳转到消息页面
          wx.navigateTo({
            url: `../chat/chat?id=${res._id}`,
          })

        }
      })
      // console.log(11)
    }

而talk消息页面显示的数据是从数据库中查找主动聊天和被动聊天的,简单来说就是把出现了个人的openid的表都显示出来

 onLoad(options) {
    // 从本地缓存用id取得聊天记录
    // 判断openid等于userA还是uesrB另一个就是对方数据
     // 如果chat_record表里有自己的openid则显示
	// 把主动聊和被动聊的一起显示
    const openid=wx.getStorageSync('openid')
      let arr=[]
      setTimeout(()=>{
    db.collection('chat_record').where({
      userA_id:openid,
    }).get({
      success:res=>{
                  if(res.data.length!==0){
               res.data.forEach(item=>{
                //  console.log(item)
                arr.unshift(item)
               })
             }
      }
    })
    db.collection('chat_record').where({
      userB_id:openid,
    }).get({
      success:res=>{
                  if(res.data.length!==0){
               res.data.forEach(item=>{
                //  console.log(item)
                arr.unshift(item)
               })
             }
      }
    })
       setTimeout(()=>{
         this.setData({
           chatList:arr
         })
         let dbid=[]
         arr.forEach(item=>{
          //  console.log(item._id)
          dbid.push(item._id)
         })
        wx.setStorageSync('dbId', dbid)
       },1000)
      
      })

  },

在chat聊天页面中主要清楚本人用户是userA还是userB ,从chat_record表中获取数据,然后进行数据渲染

onLoad(options) {
    // console.log(options.id)
    this.setData({
      recordId: wx.getStorageSync('openid'),//个人id
      chatID:options.id//聊天表id
  })
    this.getChatList()
    this.getFriendInfo()
  },

------------------------------------------------
getChatList() {
    const that = this;
    wx.cloud.database().collection('chat_record').doc(that.data.chatID).watch({
      onChange: function (snapshot) {
        // console.log(snapshot)
        if (snapshot.docs.length > 0) {
          that.setData({
            chatList: snapshot.docs[0].record
          })
          that.setData({
            scrollLast: "toView"
          })
        }

      },
      onError: function (err) {
        console.log(err)
      }
    })
  },
  getFriendInfo() {
    const that = this;
    // const friend_account_id=''
    // const friend_avatarUrl=''
    wx.cloud.database().collection('chat_record').where({
      _id:that.data.chatID
    }).get({
        success:(res)=>{
          // console.log(res)
            const {userA_id,userB_account_id,userB_avatarUrl,userA_account_id,userA_avatarUrl}=res.data[0]
            if(that.data.recordId==userA_id){
              // 赋值形式无法执行if else判断
                // friend_account_id = userB_account_id;
                // friend_avatarUrl = userB_avatarUrl;
                that.setData({
                  friend_account_id:userB_account_id,
                  friend_avatarUrl:userB_avatarUrl,
                  myself_account_id:userA_account_id,
                  myself_avatarUrl:userA_avatarUrl
              })
                // console.log("A")
                // 证明b是对方
            }else{
                // friend_account_id =userA_account_id
                // friend_avatarUrl = userA_avatarUrl
                that.setData({
                  friend_account_id:userA_account_id,
                  friend_avatarUrl:userA_avatarUrl,
                  myself_account_id:userB_account_id,
                  myself_avatarUrl:userB_avatarUrl,
              })
                // console.log("B")
            }

        }
    })
},

最后遇到了个小问题,就是chat_record表只能由创建者修改,要注意云开发中的数据权限,改成所有用户都可读可写

请添加图片描述
至于为啥要用这种方法。。。因为没学会websocket咋用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值