Table of Contents
Batch deletion
Delete all
5. Database design
Internal message content table
Internal message sending form
Message source form
6. API design
Send site message: POST /v1/message
Get the list of site messages: GET /v1/message User-Id: xxx
Read and batch read site messages: PUT /v1/read_messages/:messageIds
Home Backend Development PHP Problem How to implement the private message function in the site in php

How to implement the private message function in the site in php

Sep 10, 2021 am 10:28 AM
php

php method to implement the private message function in the site: 1. Read the request body of the POST request; 2. Call the sub-module and insert the site message sent to the entire site or the user group it belongs to; 3. Get the unread site messages Quantity; 4. Check whether the messageId belongs to the current user; 5. Just achieve batch deletion.

How to implement the private message function in the site in php

The operating environment of this article: windows7 system, PHP7.1 version, DELL G3 computer

How does php realize the private message function in the site?

PHP implementation of site message design ideas and plans

1. Background

Currently using the operation and maintenance platform When users communicate, they rely more on WeChat and email notifications. As a whole product, the operation and maintenance platform also needs a service that can carry out internal communication - in-site messaging.

The design tone of the on-site message

The design tone of the on-site message depends on how users use the on-site message:

  • Users will not stick to the operation and maintenance platform page, wait for message notifications, view the message content, and then jump to the page to be operated.

  1. #In other words, on-site messages are not the first entry point, and the real-time nature of on-site messages is not significant.

  2. Different from many social networking sites (Facebook, Zhihu, Weibo, etc.), users will stay on the main page of the social networking site, constantly refreshing new content, and checking for new messages at the same time (mainly Personal private messages, other people’s replies, etc. are not intended to check system notification messages)

  • Users will decide whether to enter the operation and maintenance platform based on the email notification.

  • If there are a lot of emails, for example, there are multiple work orders that need to be processed by the user at the same time, the user will also perform all work on the "My To Do" page provided by the work order platform.

  • If the email is deleted by mistake and there is no email link to directly enter the module to be operated

  1. Then you can ask for By link/number, go to the specified page

  2. or search directly in the relevant module

The above description means that users basically will not use on-site messages, so under what circumstances will they use on-site messages?

  • Do not send emails, only send message notifications within the site, such as site-wide notifications, editing operations, Comment operations, etc.

  • When specific modules When there is no detailed operation record, you can check the time of occurrence of the in-site message

Currently there are only product message notifications, and the message display is not classified and aggregated. It will be added in the future. When sending site-wide notifications, mentions, likes, comments and other types of in-site messages, you need to consider message aggregation by type.

2. Description of requirements

  1. In-site messages usually need to solve two requirements:

  • In-site messages from users to users, in-site messages from administrators to users: that is, one-to-one sending

  • Administrators to multiple users, users In-site messages for groups and the whole site: that is, sending one-to-many

(there is also an in-site message from users about products, such as feedback and questions about a certain module)

Our current needs are:

1 Administrator sends site messages to multiple users

Does not verify user authenticity

Limit the title length and content length (45 bytes and 150 bytes respectively, corresponding to 15 and 50 Chinese characters)

Limit the pinyin length of the recipient (maximum 50 bytes long)

[Recommended learning: "PHP Video Tutorial"]

2 Users can view their own site information

Click " All, read, unread" filter

Classified by message source: work order platform, resource management, automatic installation, vulnerability platform, fault platform. . .

3 Users can delete and batch delete site messages

4 Users can read, batch read, and mark all site messages as read

5 The message icon at the top of the operation and maintenance platform page

  • displays the number of unread messages. If it exceeds 99, it will display 99

  • . If you put the mouse on it, there will be a drop-down menu. box, displaying the last 10 unread messages (displaying "time", "source", "title")

  • There are two buttons at the bottom of the drop-down box: "More", Load more unread messages; "View all" to jump to the site message list page (it is best to open another window)

  • Click on the unread messages in the drop-down box to pop up box to display details; then delete the record in the unread list, mark it as read in the database, and reduce the number of unread messages in the message icon by one

6 Administrator page:

Update user

Delete message

Statistics

Add module

Add site message type

Send site-wide message

4. System process

Send site-wide message

  1. Read the request body of the POST request

  2. Verification length

  3. Insert into database

  4. Return

Get the list of site messages

  1. Call the sub-module and insert the site messages sent to the entire site or the user group I belong to

  2. Return database data according to query conditions

Get the number of unread messages on the site

  1. Call the sub-module and insert the site message sent to the whole site or the user group I belong to

  2. Return quantity

Batch read

  1. Check whether the messageId belongs to the current user

  2. Set read to 1 in the inbox_message table and modify update_time

All read

update inbox_message set “read”=1, “update_time”=now where “receiver_name”=currentUser() and “read” = 0
Copy after login

Batch deletion

  1. Check whether the messageId belongs to the current user

  2. Set deleted to 1 in the inbox_message table, modify update_time

Delete all

update inbox_message set “deleted”=1, “update_time”=now where “receiver_name”=currentUser() and “deleted” = 0
Copy after login

5. Database design

Internal message content table

CREATE TABLE `inbox_message_text` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `title` varchar(128) NOT NULL DEFAULT '',
  `content` longtext NOT NULL,
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  `send_type` tinyint(4) NOT NULL DEFAULT '0',
  `creator_name` varchar(255) NOT NULL DEFAULT '',
  `deleted` tinyint(4) NOT NULL DEFAULT '0',
  `module_id` bigint(20) NOT NULL,
  `link` varchar(255) NOT NULL DEFAULT '',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Copy after login

In addition to the source of the message (module_name), the internal message itself also has a latitude description , called message type (message_type), such as security messages, activity messages, service messages, etc. Within each major category, it can be divided into subcategories, such as activity messages-promotional activities

messages The source and message type can be orthogonal, that is, the work order platform can also have active messages; the message source can also be a type of message, called "product message"

Internal message sending form

CREATE TABLE `inbox_message` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `message_text_id` bigint(20) NOT NULL,
  `receiver_name` varchar(255) NOT NULL DEFAULT '',
  `read` tinyint(4) NOT NULL DEFAULT '0',
  `deleted` tinyint(4) NOT NULL DEFAULT '0',
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `inbox_message_receiver_name_deleted_read_id` (`receiver_name`,`deleted`,`read`,`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Copy after login

Message source form

CREATE TABLE `inbox_module` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `code` varchar(128) NOT NULL DEFAULT '',
  `name` varchar(128) NOT NULL DEFAULT '',
  `create_time` datetime NOT NULL,
  `update_time` datetime NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `code` (`code`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
Copy after login

6. API design

Send site message: POST /v1/message

request body Content-Type: application/json

{
    "title": "工单审批",
    "content": "XXX提交了变更申请,请审批",
    "to": "sunzhongyuan,shenli,wangya",
    "module_name": "工单平台",
    "link": "xxx"
}
Copy after login

response

{
    "code": 200,
    "data": 32,
    "msg": "OK"
}
Copy after login

Get the list of site messages: GET /v1/message User-Id: xxx

http://127.0.0.1:10085/v1/message?query=message_text_id.module_id.name:xxx&limit=1
Copy after login
{
    "code": 200,
    "data": {
        "data": [
            {
                "id": 1,
                "message_text": {
                    "id": 1,
                    "title": "title 2",
                    "content": "content 2",
                    "create_time": "2018-01-12 11:13:48",
                    "update_time": "2018-01-12 11:13:48",
                    "send_type": 1,
                    "creator_name": "sysadmin",
                    "deleted": 0,
                    "link": "xxx",
                    "Messages": null,
                    "module": {
                        "id": 4,
                        "code": "secure",
                        "name": "xxx",
                        "create_time": "2018-01-11 15:38:01",
                        "update_time": "2018-01-11 15:38:01",
                        "MessageTexts": null
                    }
                },
                "receiver_name": "xxx",
                "read": 0,
                "deleted": 0,
                "create_time": "2018-01-12 11:13:48",
                "update_time": "2018-01-12 11:13:48"
            }
        ],
        "total": 2
    },
    "msg": "OK"
}
Copy after login

Read and batch read site messages: PUT /v1/read_messages/:messageIds

response

{
    "code": 200,
    "data": "OK",
    "msg": "OK"
}
Copy after login

All read PUT:/v1/read_all_messages

response Same as above

Delete and batch delete site messages: PUT /v1/delete_messages/:messageIds

response Same as above

All Delete site messages: PUT /v1/delete_all_messages

response Same as above

Get the message source list: GET /v1/module

response

{
    "code": 200,
    "data": [
        {
            "id": 1,
            "code": "worksheet",
            "name": "工单平台",
            "create_time": "2018-01-11 15:21:38",
            "update_time": "2018-01-11 15:21:38",
            "MessageTexts": null
        },
        {
            "id": 2,
            "code": "cmdb",
            "name": "资源管理",
            "create_time": "2018-01-11 15:22:28",
            "update_time": "2018-01-11 15:22:28",
            "MessageTexts": null
        },
        ...
    ],
    "msg": "OK"
}
Copy after login

7. Test Notes

1 Send site message

  • Pure interface

  • The recipient users are separated by commas, and the authenticity is not verified

  • The recipient users are Length check, 50 bytes

  • title content has length check, respectively 45 and 150 bytes

  • module_name is a List, you must select one from here

2 Other interfaces can be tested through the front-end page

8. Optimization

  • The unread list can be displayed in bold font, while the read list can be displayed in normal font

  • Category the messages on the site and label them with different latitudes , convenient for filtering, searching, and blocking

  • Users can set the message sources of the site messages they are allowed to receive

  • The administrator can control the site-wide messages, All site personnel and message attributes can be added, deleted, modified, and checked, such as canceling a site message so that no one can see it.

  • The administrator can count the number of messages sent within the site, and the The usage of the product, the proportion of messages being read, the time the messages were read, the way the messages were read (click to open or batch operation), etc.

9. Design of key function points

Icon behavior in the upper right corner

1 Click the icon to display the latest N unread messages

  • Show drop-down box

  • Get the latest N unread messages in real time

  • N can be 5 to 10, The specific value depends on the height limit of the drop-down box

  • When the unread value is less than N, the drop-down box can adapt to the height

  • If there is no unread value Message, displaying "No new messages yet"

  • Stop getting the number of unread messages every 10 seconds interface

2 In the drop-down box, display the message source, time (relative to the current time: 10 minutes ago), title

  • Slide the drop-down box downward to display more unread messages (only get the id Less than the minimum id in the displayed message list, that is, new messages that come after clicking the icon will not be obtained)

3 Click a message in the drop-down box

  • The drop-down box does not disappear

  • Still stops the interface for obtaining the number of unread messages every 10 seconds

  • The number of unread messages is reduced by 1

  • Unread message list deletes the current message (slice)

  • Display pop-up box

4 The pop-up box displays the source, time (absolute time), title, and content of the message

5 Close the pop-up box or click on the periphery:

  • The pop-up box disappears

  • The drop-down box does not disappear

  • You can continue to click on an unread message

6 Click the drop-down box and the periphery of the icon again

  • The drop-down box disappears

  • Clear the existing ones The unread message list

  • Restore the interface for obtaining the number of unread messages every 10 seconds

7 Click the icon again , return to the #1 state

The icon behavior of Alibaba Cloud is:

  1. #The number of unread messages will be requested only once when the page is refreshed, and will not be refreshed regularly thereafter (of course it is also possible It's because the refresh time interval is relatively long, but I didn't find it; or maybe a socket method was used to establish a long link)

  2. hover icon, which is a drop-down box that displays unread messages

  3. Click the icon to enter the site message management page. The default is "Unread Messages"

4 Click on the unread message, open a new Tab, and display the details of the message (detail page). The content of the original Tab remains unchanged, that is, there is no unread message minus one, and the message just clicked is not deleted from the drop-down box

5 Displays up to 5 messages. As long as the page is not refreshed, it will always be these 5

6 There is no scrolling for more functions, only to view more, click to enter the site message management page. The default is "Unread Messages"

  • The difference between clicking the icon and clicking the icon is: clicking the icon directly jumps to the current page of the site message management page, clicking "View More" will create a new Tab

7 There is an additional "Message Acceptance Management" button. The current page jumps to the site message management page, but the default is "Basic Receive Management"

Hide the browser progress bar

The interface for obtaining the number of unread messages every 10 seconds will trigger the browser to display the progress bar, distracting the user's attention , to hide this progress bar.

Other behaviors of refreshing the page are not affected.

The above is the detailed content of How to implement the private message function in the site in php. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

See all articles