Home > Article > Backend Development > How to implement the private message function in the site in 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.
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.
#In other words, on-site messages are not the first entry point, and the real-time nature of on-site messages is not significant.
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
Then you can ask for By link/number, go to the specified page
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
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
Read the request body of the POST request
Verification length
Insert into database
Return
Get the list of site messages
Call the sub-module and insert the site messages sent to the entire site or the user group I belong to
Return database data according to query conditions
Get the number of unread messages on the site
Call the sub-module and insert the site message sent to the whole site or the user group I belong to
Return quantity
Batch read
Check whether the messageId belongs to the current user
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
Check whether the messageId belongs to the current user
Set deleted to 1 in the inbox_message table, modify update_time
update inbox_message set “deleted”=1, “update_time”=now where “receiver_name”=currentUser() and “deleted” = 0
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;
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"
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;
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;
request body Content-Type: application/json
{ "title": "工单审批", "content": "XXX提交了变更申请,请审批", "to": "sunzhongyuan,shenli,wangya", "module_name": "工单平台", "link": "xxx" }
response
{ "code": 200, "data": 32, "msg": "OK" }
http://127.0.0.1:10085/v1/message?query=message_text_id.module_id.name:xxx&limit=1
{ "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" }
response
{ "code": 200, "data": "OK", "msg": "OK" }
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" }
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:
#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)
hover icon, which is a drop-down box that displays unread messages
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!