微信小程序用云开发实现多人聊天室
微信小程序用云开发实现多人聊天2020/05/21
用微信云开发实现聊天室 无需后台 新手也能开发直接的聊天室,话不多说直接上效果图和代码(也可以私信我一对一教学哦)
效果图片
js
//index.js
const db = wx.cloud.database()
const chatroomCollection = db.collection("chatroom")
var util = require('../../utils/util.js');
Page({
data: {
isShowUserName: false,
userInfo:null,
textInputValue:"",
chats:[],
openid:""
},
onLoad(){
this.setData({
openid:wx.getStorageSync("openid")
})
},
onReady(){
chatroomCollection.watch({
onChange:this.onChange.bind(this),
onError(err){
console.log(err)
}
})
},
onChange(e){
console.log(e)
let that = this
if(e.type=="init"){
that.setData({
chats:[
...that.data.chats,
...[...e.docs]
]
})
}else{
const chats = [...that.data.chats]
for(const docChange of e.docChanges){
switch(docChange.queueType){
case 'enqueue':
chats.push(docChange.doc