微信小程式實現聊天室功能
阿新 • • 發佈:2021-06-16
本文通過例項為大家分享了微信小程式實現聊天室的具體程式碼,供大家參考,具體內容如下
1.實現效果展示
2.room.wxml
<view class="container" style="{{containerStyle}}">
<chatroom
style="width: 100%; height: 100%YAapD"
envId="{{chatRoomEnvId}}"
collection="{{chatRoomCollection}}"
groupId="{{chatRoomGroupId}}"
groupName="{{chatRoomGroupName}}"
userInfo="{{userInfo}}"
onGetUserInfo="{{onGetUserInfo}}"
getOpenID="{{getOpenID}}"
></chatroom>
</view>
3.room.js
const app = getApp() Page({ data: { avatarUrl: './user-unlogin.png',userInfo: null,logged: false,takeSession: false,requestResult: '',// chatRoomEnvId: 'release-f8415a',chatRoomCollection: 'chatroom',chatRoomGroupId: 'demo',chatRoomGroupName: '聊天室',// functions for used in chatroom components onGwww.cppcns.cometUserInfo: null,getOpenID: null,},onLoad: function() { // 獲取使用者資訊 wx.getSetting({ success: res => { if (res.authSetting['scope.userInfo']) { // 已經授權,可以直接呼叫 getUserInfo 獲取頭像暱稱,不會彈框 wx.getUserInfo({ success: res => { this.setData({ avatarUrl: res.userInfo.avatarUrl,userInfo: res.userInfo }) } }) } } }) this.setData({ onGetUserInfo: this.onGetUserInfo,getOpenID: this.getOpenID,}) wx.getSystemInfo({ success: res => { console.log('system info',res) if (res.safeArea) { const { top,bottom } = res.safeArea this.setData({ containerStyle: `padding-top: ${(/ios/i.test(res.system) ? 10 : 20) + top}px; padding-bottom: ${程式設計客棧20 + res.windowHeight - bottom}px`,}) } },}) },getOpenID: async functiohttp://www.cppcns.comn() { if (this.openid) { return this.openid } const { result } = await wx.cloud.callFunction({ name: 'login',}) return result.openid },onGetUserInfo: function(e) { if (!this.logged && e.detail.userInfo) { this.setData({ logged: true,avatarUrl: e.detail.userInfo.avatarUrl,userInfo: e.detail.userInfo }) } },onShareAppMessage() { return { title: '即時通訊 Demohttp://www.cppcns.com',path: '/pages/im/room/room',} },})
4.room.json
{ "usingComponents": { "chatroom": "/components/chatroom/chatroom" } }
5.room.wxss
.container { height: 100%; position: absolute; top: 0; bottom: 0; left: 0; right: 0; padding-top: 80rpx; padding-bottom: 30rpx; }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。