212 lines
6.2 KiB
Markdown
212 lines
6.2 KiB
Markdown
# 显示系统通知
|
|
|
|
---
|
|
|
|
![](https://gitee.com/veal98/images/raw/master/img/20210128170230.png)
|
|
|
|
## DAO
|
|
|
|
```xml
|
|
int selectNoticUnReadCount(int userId, String topic);
|
|
|
|
<!--查询未读的系统通知数量-->
|
|
<select id="selectNoticUnReadCount" resultType="int">
|
|
select count(id)
|
|
from message
|
|
where status = 0
|
|
and from_id = 1
|
|
and to_id = #{userId}
|
|
<if test = "topic != null">
|
|
and conversation_id = #{topic}
|
|
</if>
|
|
</select>
|
|
```
|
|
|
|
动态查询,如果 `selectNoticUnReadCount` 不传入 topic 参数,则查询所有系统通知的未读数量。
|
|
|
|
## Service
|
|
|
|
```java
|
|
/**
|
|
* 查询某个主题下最新的系统通知
|
|
* @param userId
|
|
* @param topic
|
|
* @return
|
|
*/
|
|
public Message findLatestNotice(int userId, String topic) {
|
|
return messageMapper.selectLatestNotice(userId, topic);
|
|
}
|
|
|
|
/**
|
|
* 查询某个主题下包含的系统通知数量
|
|
* @param userId
|
|
* @param topic
|
|
* @return
|
|
*/
|
|
public int findNoticeCount(int userId, String topic) {
|
|
return messageMapper.selectNoticeCount(userId, topic);
|
|
}
|
|
|
|
/**
|
|
* 查询未读的系统通知数量
|
|
* @param userId
|
|
* @param topic
|
|
* @return
|
|
*/
|
|
public int findNoticeUnReadCount(int userId, String topic) {
|
|
return messageMapper.selectNoticeUnReadCount(userId, topic);
|
|
}
|
|
|
|
/**
|
|
* 查询某个主题所包含的通知列表
|
|
* @param userId
|
|
* @param topic
|
|
* @param offset
|
|
* @param limit
|
|
* @return
|
|
*/
|
|
public List<Message> findNotices(int userId, String topic, int offset, int limit) {
|
|
return messageMapper.selectNotices(userId, topic, offset, limit);
|
|
}
|
|
```
|
|
|
|
## Controller
|
|
|
|
```java
|
|
/**
|
|
* 通知列表(只显示最新一条消息)
|
|
* @param model
|
|
* @return
|
|
*/
|
|
@GetMapping("/notice/list")
|
|
public String getNoticeList(Model model) {
|
|
User user = hostHolder.getUser();
|
|
|
|
// 查询评论类通知
|
|
Message message = messageService.findLatestNotice(user.getId(), TOPIC_COMMNET);
|
|
// 封装通知需要用到的数据
|
|
Map<String, Object> messageVO = new HashMap<>();
|
|
if (message != null) {
|
|
messageVO.put("message", message);
|
|
|
|
String content = HtmlUtils.htmlUnescape(message.getContent());
|
|
Map<String, Object> data = JSONObject.parseObject(content, HashMap.class);
|
|
|
|
messageVO.put("user", userService.findUserById((Integer) data.get("userId")));
|
|
messageVO.put("entityType", data.get("entityType"));
|
|
messageVO.put("entityId", data.get("entityId"));
|
|
messageVO.put("postId", data.get("postId"));
|
|
|
|
int count = messageService.findNoticeCount(user.getId(), TOPIC_COMMNET);
|
|
messageVO.put("count", count);
|
|
|
|
int unread = messageService.findNoticeUnReadCount(user.getId(), TOPIC_COMMNET);
|
|
messageVO.put("unread", unread);
|
|
}
|
|
model.addAttribute("commentNotice", messageVO);
|
|
|
|
// 查询点赞类通知
|
|
...........
|
|
// 查询关注类通知
|
|
...........
|
|
|
|
// 查询未读消息数量
|
|
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
|
|
model.addAttribute("letterUnreadCount", letterUnreadCount);
|
|
int noticeUnreadCount = messageService.findNoticeUnReadCount(user.getId(), null);
|
|
model.addAttribute("noticeUnreadCount", noticeUnreadCount);
|
|
|
|
return "/site/notice";
|
|
}
|
|
|
|
/**
|
|
* 查询某个主题所包含的通知列表
|
|
* @param topic
|
|
* @param page
|
|
* @param model
|
|
* @return
|
|
*/
|
|
@GetMapping("/notice/detail/{topic}")
|
|
public String getNoticeDetail(@PathVariable("topic") String topic, Page page, Model model) {
|
|
User user = hostHolder.getUser();
|
|
|
|
page.setLimit(5);
|
|
page.setPath("/notice/detail/" + topic);
|
|
page.setRows(messageService.findNoticeCount(user.getId(), topic));
|
|
|
|
List<Message> noticeList = messageService.findNotices(user.getId(), topic,page.getOffset(), page.getLimit());
|
|
List<Map<String, Object>> noticeVoList = new ArrayList<>();
|
|
if (noticeList != null) {
|
|
for (Message notice : noticeList) {
|
|
Map<String, Object> map = new HashMap<>();
|
|
// 通知
|
|
map.put("notice", notice);
|
|
// 内容
|
|
String content = HtmlUtils.htmlUnescape(notice.getContent());
|
|
Map<String, Object> data = JSONObject.parseObject(content, HashMap.class);
|
|
map.put("user", userService.findUserById((Integer) data.get("userId")));
|
|
map.put("entityType", data.get("entityType"));
|
|
map.put("entityId", data.get("entityId"));
|
|
map.put("postId", data.get("postId"));
|
|
// 发送系统通知的作者
|
|
map.put("fromUser", userService.findUserById(notice.getFromId()));
|
|
|
|
noticeVoList.add(map);
|
|
}
|
|
}
|
|
model.addAttribute("notices", noticeVoList);
|
|
|
|
// 设置已读
|
|
List<Integer> ids = getUnreadLetterIds(noticeList);
|
|
if (!ids.isEmpty()) {
|
|
messageService.readMessage(ids);
|
|
}
|
|
|
|
return "/site/notice-detail";
|
|
}
|
|
```
|
|
|
|
存储在 message 表中的系统通知的 content 字段是 JSON 格式的
|
|
|
|
![](https://gitee.com/veal98/images/raw/master/img/20210128172348.png)
|
|
|
|
我们需要获取这个 JSON 字符串,将其转化成对象然后转换成一条通知
|
|
|
|
前端的修改此处就不写了
|
|
|
|
## 拦截器
|
|
|
|
这里需要注意一下导航栏上面的未读消息数量(未读私信 + 未读系统通知)的实时更新。使用拦截器实现,在 Controller 之后模板之前调用:
|
|
|
|
```java
|
|
@Component
|
|
public class MessageInterceptor implements HandlerInterceptor {
|
|
|
|
@Autowired
|
|
private HostHolder hostHolder;
|
|
|
|
@Autowired
|
|
private MessageService messageService;
|
|
|
|
/**
|
|
* Controller之后模板之前被调用
|
|
* 获取未读私信/系统通知的数量
|
|
* @param request
|
|
* @param response
|
|
* @param handler
|
|
* @param modelAndView
|
|
* @throws Exception
|
|
*/
|
|
@Override
|
|
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
|
|
User user = hostHolder.getUser();
|
|
if (user != null && modelAndView != null) {
|
|
int letterUnreadCount = messageService.findLetterUnreadCount(user.getId(), null);
|
|
int noticeUnreadCount = messageService.findNoticeUnReadCount(user.getId(), null);
|
|
modelAndView.addObject("allUnreadCount", letterUnreadCount + noticeUnreadCount);
|
|
}
|
|
}
|
|
}
|
|
```
|
|
|
|
别忘记在 `WebMvcConfig` 中配置该拦截器 |