diff --git a/forum-api/src/main/java/com/github/liueyueyi/forum/api/model/enums/NotifyTypeEnum.java b/forum-api/src/main/java/com/github/liueyueyi/forum/api/model/enums/NotifyTypeEnum.java index 1c76d341f..4b21e36f1 100644 --- a/forum-api/src/main/java/com/github/liueyueyi/forum/api/model/enums/NotifyTypeEnum.java +++ b/forum-api/src/main/java/com/github/liueyueyi/forum/api/model/enums/NotifyTypeEnum.java @@ -17,11 +17,15 @@ public enum NotifyTypeEnum { COLLECT(4, "收藏"), FOLLOW(5, "关注消息"), SYSTEM(6, "系统消息"), - DELETE_COMMENT(11, "删除评论"), - DELETE_REPLY(12, "删除回复"), - CANCEL_PRAISE(13, "取消点赞"), - CANCEL_COLLECT(14, "取消收藏"), - CANCEL_FOLLOW(15, "取消关注"), + DELETE_COMMENT(1, "删除评论"), + DELETE_REPLY(2, "删除回复"), + CANCEL_PRAISE(3, "取消点赞"), + CANCEL_COLLECT(4, "取消收藏"), + CANCEL_FOLLOW(5, "取消关注"), + + // 注册、登录添加系统相关提示消息 + REGISTER(6, "用户注册"), + LOGIN(6, "用户登录"), ; diff --git a/forum-service/src/main/java/com/github/liuyueyi/forum/service/notify/service/impl/NotifyMsgListener.java b/forum-service/src/main/java/com/github/liuyueyi/forum/service/notify/service/impl/NotifyMsgListener.java index a9dc1297f..fc63334c8 100644 --- a/forum-service/src/main/java/com/github/liuyueyi/forum/service/notify/service/impl/NotifyMsgListener.java +++ b/forum-service/src/main/java/com/github/liuyueyi/forum/service/notify/service/impl/NotifyMsgListener.java @@ -1,6 +1,7 @@ package com.github.liuyueyi.forum.service.notify.service.impl; import com.github.liueyueyi.forum.api.model.enums.NotifyStatEnum; +import com.github.liueyueyi.forum.api.model.enums.NotifyTypeEnum; import com.github.liueyueyi.forum.api.model.vo.notify.NotifyMsgEvent; import com.github.liuyueyi.forum.service.article.repository.entity.ArticleDO; import com.github.liuyueyi.forum.service.article.service.ArticleReadService; @@ -23,6 +24,7 @@ @Async @Service public class NotifyMsgListener implements ApplicationListener> { + private static final Long ADMIN_ID = 1L; private final ArticleReadService articleReadService; private final CommentReadService commentReadService; @@ -59,6 +61,13 @@ public void onApplicationEvent(NotifyMsgEvent msgEvent) { case CANCEL_FOLLOW: // todo 取消操作,若之前的消息是未读状态,则移除对应的记录 log.info("取消操作: {}", msgEvent); + case LOGIN: + // todo 用户登录,判断是否需要插入新的通知消息,暂时先不做 + break; + case REGISTER: + // 首次注册,插入一个欢迎的消息 + saveRegisterSystemNotify((Long) msgEvent.getContent()); + break; default: // todo 系统消息 } @@ -140,4 +149,18 @@ private void saveFollowNotify(NotifyMsgEvent event) { } } + private void saveRegisterSystemNotify(Long userId) { + NotifyMsgDO msg = new NotifyMsgDO().setRelatedId(0L) + .setNotifyUserId(userId) + .setOperateUserId(ADMIN_ID) + .setType(NotifyTypeEnum.REGISTER.getType()) + .setState(NotifyStatEnum.UNREAD.getStat()) + .setMsg("欢迎注册使用论坛,更多的使用姿势请参考:使用教程"); + NotifyMsgDO record = notifyMsgDao.getByUserIdRelatedIdAndType(msg); + if (record == null) { + // 若之前已经有对应的通知,则不重复记录;因为用户的关注是一对一的,可以重复的关注、取消,但是最终我们只通知一次 + notifyMsgDao.save(msg); + } + } + } diff --git a/forum-service/src/main/java/com/github/liuyueyi/forum/service/user/service/user/UserServiceImpl.java b/forum-service/src/main/java/com/github/liuyueyi/forum/service/user/service/user/UserServiceImpl.java index 0dc1255ea..46f390728 100644 --- a/forum-service/src/main/java/com/github/liuyueyi/forum/service/user/service/user/UserServiceImpl.java +++ b/forum-service/src/main/java/com/github/liuyueyi/forum/service/user/service/user/UserServiceImpl.java @@ -1,14 +1,17 @@ package com.github.liuyueyi.forum.service.user.service.user; import com.github.liueyueyi.forum.api.model.context.ReqInfoContext; +import com.github.liueyueyi.forum.api.model.enums.NotifyTypeEnum; import com.github.liueyueyi.forum.api.model.exception.ExceptionUtil; import com.github.liueyueyi.forum.api.model.vo.article.dto.YearArticleDTO; import com.github.liueyueyi.forum.api.model.vo.constants.StatusEnum; +import com.github.liueyueyi.forum.api.model.vo.notify.NotifyMsgEvent; import com.github.liueyueyi.forum.api.model.vo.user.UserInfoSaveReq; import com.github.liueyueyi.forum.api.model.vo.user.UserSaveReq; import com.github.liueyueyi.forum.api.model.vo.user.dto.ArticleFootCountDTO; import com.github.liueyueyi.forum.api.model.vo.user.dto.BaseUserInfoDTO; import com.github.liueyueyi.forum.api.model.vo.user.dto.UserStatisticInfoDTO; +import com.github.liuyueyi.forum.core.util.SpringUtil; import com.github.liuyueyi.forum.service.article.repository.dao.ArticleDao; import com.github.liuyueyi.forum.service.article.service.ArticleReadService; import com.github.liuyueyi.forum.service.user.converter.UserConverter; @@ -65,6 +68,9 @@ public void registerOrGetUserInfo(UserSaveReq req) { if (record != null) { // 用户存在,不需要注册 req.setUserId(record.getId()); + + // 用户登录事件 + SpringUtil.publishEvent(new NotifyMsgEvent<>(this, NotifyTypeEnum.LOGIN, record.getId())); return; } @@ -79,6 +85,9 @@ record = UserConverter.toDO(req); userInfo.setUserName(UserRandomGenHelper.genNickName()); userInfo.setPhoto(UserRandomGenHelper.genAvatar()); userDao.save(userInfo); + + // 用户注册事件 + SpringUtil.publishEvent(new NotifyMsgEvent<>(this, NotifyTypeEnum.REGISTER, userInfo.getUserId())); } @Override diff --git a/forum-ui/src/main/resources/templates/biz/notice/msg.html b/forum-ui/src/main/resources/templates/biz/notice/msg.html index acebafae6..5326cbbdd 100644 --- a/forum-ui/src/main/resources/templates/biz/notice/msg.html +++ b/forum-ui/src/main/resources/templates/biz/notice/msg.html @@ -63,12 +63,18 @@
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+