WeChat applet realizes chat room function

WeChat applet realizes chat room function

This article shares the specific code of WeChat applet to implement the chat room through examples for your reference. The specific content is as follows

1. Achieve effect display

2.room.wxml

<view class="container" style="{{containerStyle}}">
  <chatroom
    style="width: 100%; height: 100%"
    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: 'Chat Room',

    // functions for used in chatroom components
    onGetUserInfo: null,
    getOpenID: null,
  },

  onLoad: function() {
    // Get user information wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // Already authorized, you can directly call getUserInfo to get the avatar nickname without a pop-up window 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 function() {
    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: 'Instant Messaging Demo',
      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;
}

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • WeChat applet implements chat room
  • Detailed explanation of WeChat applet development chat room—real-time chat, support for image preview
  • Example code for implementing the WeChat applet websocket chat room
  • WeChat applet chat room simple implementation
  • WeChat applet implements simple chat room

<<:  Tutorial on installing Docker in Windows 10 subsystem Ubuntu (WSL) (with pictures and text)

>>:  Summary of MySQL common SQL statements including complex SQL queries

Recommend

CSS sets the box container (div) height to always be 100%

Preface Sometimes you need to keep the height of ...

Linux redis-Sentinel configuration details

download Download address: https://redis.io/downl...

Stealing data using CSS in Firefox

0x00 Introduction A few months ago, I found a vul...

How to upload projects to Code Cloud in Linux system

Create a new project test1 on Code Cloud Enter th...

Write a shopping mall card coupon using CSS in three steps

Today is 618, and all major shopping malls are ho...

About Vue virtual dom problem

Table of contents 1. What is virtual dom? 2. Why ...

Vue+Element UI realizes the encapsulation of drop-down menu

This article example shares the specific code of ...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

avue-crud implementation example of multi-level complex dynamic header

Table of contents Preface Background data splicin...

Summary of MySQL database usage specifications

Introduction: Regarding MySQL database specificat...

Learn asynchronous programming in nodejs in one article

Table of Contents Introduction Synchronous Asynch...

MySQL permission control detailed explanation

Table of contents mysql permission control Permis...

How to configure user role permissions in Jenkins

Jenkins configuration of user role permissions re...

Why MySQL can ignore time zone issues when using timestamp?

I have always wondered why the MySQL database tim...