系统消息功能
框架已实现系统中的消息功能。消息可通过模板生成,也可直接调用controller中的新增方法生成。
消息保存在消息表UTILITY_MESSAGE和消息详情表UTILITY_MESSAGE_DETAIL中。
消息状态保存在消息状态表UTILITY_MESSAGE_STATUS中。(特别注意:消息状态表中只保存已读状态记录,不保存未读状态记录。)
消息模板保存在消息模板表UTILITY_MESSAGE_TEMPLATE中。
1.表设计
消息表UTILITY_MESSAGE设计如下:
消息详情表UTILITY_MESSAGE_DETAIL设计如下:
消息状态表UTILITY_MESSAGE_STATUS设计如下:
消息模板表UTILITY_MESSAGE_TEMPLATE设计如下:
2.模板生成消息
2.1.示例代码位置如下:
2.2.配置模板
在消息模板表UTILITY_MESSAGE_TEMPLATE中新增模板数据,新增数据中CODE字段自定义,CONTENT字段需按规定的格式填写内容,示例如下所示:
1
2
insert into "UTILITY_MESSAGE_TEMPLATE" ("ID","CODE","CONTENT","CREATE_USER","CREATE_TIME","UPDATE_USER","UPDATE_TIME")
values ('testTemplate', 'codeTest', '您申请订阅的 “#{name}”审核#{status}。您现在可跳转至在我的工作台查看具体内容。', null, null, null, null);
注意,content中占位符使用#{},前台传入数据后,会根据{}中的字段进行替换。如使用以上模板,传入参数Map中包含name参数时,会使用name参数的值替换模板中的#{name}。注意大小写需保持一致,否则模板内容会无法正确替换。
2.3.发送消息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Autowired private MessageService messageService;
@Operation(description = "发送消息")
@RequestMapping(value = "/sendMessage", method = RequestMethod.POST)
public boolean sendMessage() {
String subjectType = "user";
String subjectId = "testUserId";
String type = "testType";
String templateCode = "codeTest";
Map<String, String> map = new HashMap<>();
map.put("name", "测试应用");
map.put("status", "通过");
return messageService.sendMessage(subjectType, subjectId, type, templateCode, map);
}
调用MessageService的sendMessage方法进行消息发送。
其中
- subjectType:消息接收主体类别,如user,org等。表示消息需要发送给用户或者是组织。
- subjectId:消息接收主体id。根据subjectType判断。若subjectType为user,subjectId表示为用户Id;subjectType为org,则subjectId表示为组织Id。
- type:消息类别。可根据系统需求,自定义类别。
- templateCode:模板编号。表示消息生成时,需要使用的模板code。对应模板表中的code字段。
- map:占位符与待替换内容的对应关系。key值使用模板中大括号里的英文单词。
3.其他方法
框架还提供其他操作消息的方法。如下所示:
新增消息的方法:/utility/message/create
根据id删除消息的方法:/utility/message/delete
根据id数组删除消息的方法:/utility/message/deleteByIds
根据id查询消息的方法:/utility/message/getOne
分页查询消息的方法:/utility/message/pageList
标记消息为已读的方法:/utility/message/read
更新消息的方法:/utility/message/update