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

Implement a simple data response system

Table of contents 1. Dep 2. Understand obverser 3...

jQuery achieves the effect of advertisement scrolling up and down

This article shares the specific code of jQuery t...

How to mount a disk in Linux and set it to automatically mount on boot

Knowing that everyone's time is precious, I w...

7 native JS error types you should know

Table of contents Overview 1. RangeError 2. Refer...

Sample code for implementing 3D book effect with CSS

Without further ado, let's take a look at the...

Solution to nginx-ingress-controller log persistence solution

Recently I saw an article on a public account tha...

Detailed explanation of Apache SkyWalking alarm configuration guide

Apache SkyWalking Apache SkyWalking is an applica...

Detailed explanation of Nginx static file service configuration and optimization

Root directory and index file The root directive ...

How to implement Linux deepin to delete redundant kernels

The previous article wrote about how to manually ...

Practice of realizing Echarts chart width and height adaptation in Vue

Table of contents 1. Install and import 2. Define...

Steps to install RocketMQ instance on Linux

1. Install JDK 1.1 Check whether the current virt...

VMware15/16 Detailed steps to unlock VMware and install MacOS

VMware version: VMware-workstation-full-16 VMware...

Detailed explanation of Vue development Sort component code

Table of contents <template> <ul class=&...