PHP实现站内信( 二 )


PHP实现站内信

文章插图
 
站内信本身除了消息来源(module_name),还有一个纬度的描述,叫消息类型(message_type),例如安全消息、活动消息、服务消息等,每一大类里,又可以划分子类,例如活动消息-优惠活动
 
消息来源和消息类型可以是正交关系,即工单平台也可以有活动消息;消息来源也可以是消息类型的一种,称为“产品消息”
 
站内信发送表
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;
PHP实现站内信

文章插图
 
消息来源表
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;
PHP实现站内信

文章插图
 
六、API设计发送站内信:POST /v1/message
request body Content-Type: Application/json
{ "title": "工单审批", "content": "XXX提交了变更申请,请审批", "to": "sunzhongyuan,shenli,wangya", "module_name": "工单平台", "link": "xxx"}response
{ "code": 200, "data": 32, "msg": "OK"}获取站内信列表:GET /v1/message User-Id: xxx
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"}注:
返回数据的个数是由 limit 限制,而 total 是符合query条件的总数(用于分页)
目前没有发送用户组、全站的行为,如果有的话,在获取列表接口里,增加一步“插入所有发送给我所在用户组,或发给全站的,且我自己的站内信列表里没有记录到的站内信”
获取未读站内信数量:GET /v1/message/unread_count
response
{ "code": 200, "data": 29, "msg": "OK"} 获取单个站内信内容:GET /v1/message/:id
{ "code": 200, "data": { "id": 2, "message_text": { "id": 2, "title": "title 2", "content": "content 3", "create_time": "2018-01-12 11:37:54", "update_time": "2018-01-12 11:37:54", "send_type": 1, "creator_name": "sysadmin", "deleted": 0, "link": "", "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": 1, "deleted": 0, "create_time": "2018-01-12 11:37:54", "update_time": "2018-01-22 17:33:20" }, "msg": "OK"}已阅、批量已阅站内信:PUT /v1/read_messages/:messageIds
response
{ "code": 200, "data": "OK", "msg": "OK"}


推荐阅读