通知列表
显示评论、点赞、关注三种类型的通知
通知详情
分类显示某一类主题所包含的通知
未读消息
在页面头部显示所有的未读消息数量
打开MessageMapper,添加
// 查询某个主题下最新的通知
Message selectLatestNotice(int userId, String topic);
// 查询某个主题所包含的通知数量
int selectNoticeCount(int userId, String topic);
// 查询未读的通知数量
int selectNoticeUnreadCount(int userId, String topic);
打开message-mapper.xml,添加
<select id="selectLatestNotice" resultType="Message">
select<include refid="selectFields"></include>
from message
where id in (
select max(id) from message
where status != 2
and from_id = 1
and to_id = #{userId}
and conversation_id = #{topic}
)
</select>
<select id="selectNoticeCount" resultType="int">
select count(id) from message
where status != 2 <!--2表示已删除的状态-->
and from_id = 1
and to_id = #{userId}
and conversation_id = #{topic}
</select>
<select id="selectNoticeUnreadCount" resultType="int">
select count(id) from message
where status = 0 <!--0表示未读的状态-->
and from_id = 1
and to_id = #{userId}
<if test="topic!=null">
and conversation_id = #{topic}
</if>
</select>
打开MessageService类,添加
public Message findLatestNotice(int userId, String topic){
return messageMapper.selectLatestNotice(userId,topic);
}
public int findNoticeCount(int userId,String topic){
return messageMapper.selectNoticeCount(userId,topic);
}
public int findNoticeUnreadCount(int userId,String topic){
return messageMapper.selectNoticeUnreadCount(userId,topic);
}
打开MessageController类,添加
@RequestMapping(path = "/notice/list",method = RequestMethod.GET)
public String getNoticeList(Model model){
User user = hostHolder.getUser();
//查询评论类通知
Message message = messageService.findLatestNotice(user.getId(),TOPIC_COMMENT);
if(message!=null){
Map<String, Object> messageVO = new HashMap<>();
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_COMMENT);
messageVO.put("count",count);
int unread = messageService.findNoticeUnreadCount(user.getId(),TOPIC_COMMENT);
messageVO.put("unread",unread);
model.addAttribute("commentNotice",messageVO);
}
// 查询点赞类通知
message = messageService.findLatestNotice(user.getId(),TOPIC_LIKE);
if(message!=null){
Map<String,Object> messageVO = new HashMap<>();
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_LIKE);
messageVO.put("count",count);
int unread = messageService.findNoticeUnreadCount(user.getId(),TOPIC_LIKE);
messageVO.put("unread",unread);
model.addAttribute("likeNotice",messageVO);
}
// 查询关注类通知
message = messageService.findLatestNotice(user.getId(),TOPIC_FOLLOW);
if(message!=null){
Map<String,Object> messageVO = new HashMap<>();
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"));
int count = messageService.findNoticeCount(user.getId(),TOPIC_FOLLOW);
messageVO.put("count",count);
int unread = messageService.findNoticeUnreadCount(user.getId(),TOPIC_FOLLOW);
messageVO.put("unread",unread);
model.addAttribute("followNotice",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";
}
并在getLetterList方法里,return "/site/letter"前,添加
int noticeUnreadCount = messageService.findNoticeUnreadCount(user.getId(),null);
model.addAttribute("noticeUnreadCount",noticeUnreadCount); //后来添加的
打开letter.html,修改对应的通知数量。
打开notice.html,修改相应位置。
在MessageMapper里添加
// 查询某个主题所包含的通知列表
List<Message> selectNotices(int userId, String topic ,int offset, int limit);
在message-mapper.xml里添加
<select id="selectNotices" resultType="Message">
select<include refid="selectFields"></include>
from message
where status != 2
and from_id = 1
and to_id = #{userId}
and conversation_id = #{topic}
order by create_time desc
limit #{offset},#{limit}
</select>
在MessageService里添加
public List<Message> findNotices(int userId, String topic, int offset, int limit){
return messageMapper.selectNotices(userId,topic,offset,limit);
}
在MessageController里添加
@RequestMapping(path = "/notice/detail/{topic}",method = RequestMethod.GET)
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")); //有两类通知要用到postId,没用到的就是null
//通知作者
map.put("fromUser",userService.findUserById(notice.getFromId()));
noticeVoList.add(map);
}
}
model.addAttribute("notices",noticeVoList);
//设置已读
List<Integer> ids = getLetterIds(noticeList);
if(!ids.isEmpty()){
messageService.readMessage(ids);
}
return "/site/notice-detail";
}
修改notice.html和notice-detail.html相应位置.
在controller/interceptor包下新建MessageIntercepter类
@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类addInterceptors方法里添加
registry.addInterceptor(messageInterceptor)
.excludePathPatterns("/**/*.css","/**/*.js","/**/*.png","/**/*.jpg","/**/*.jpeg");
在index.html里修改消息数量相应的位置。