From e42e9e940eb92dfc32f509f734f838623330eb0c Mon Sep 17 00:00:00 2001 From: x ronger Date: Wed, 9 Sep 2020 22:34:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?:sparkles:=20=E5=85=B3=E6=B3=A8=E7=94=A8?= =?UTF-8?q?=E6=88=B7=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/vertical/mapper/FollowMapper.java | 19 ++++++++ .../rymcu/vertical/service/FollowService.java | 35 +++++++++++++++ .../service/impl/ArticleServiceImpl.java | 6 +-- .../service/impl/FollowServiceImpl.java | 45 +++++++++++++++++++ .../service/impl/PortfolioServiceImpl.java | 4 +- .../vertical/service/impl/TagServiceImpl.java | 2 +- .../com/rymcu/vertical/util/UserUtils.java | 2 +- .../web/api/follow/FollowController.java | 45 +++++++++++++++++++ .../notification/NotificationController.java | 4 +- src/main/java/mapper/FollowMapper.xml | 8 ++++ 10 files changed, 161 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/rymcu/vertical/mapper/FollowMapper.java create mode 100644 src/main/java/com/rymcu/vertical/service/FollowService.java create mode 100644 src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java create mode 100644 src/main/java/com/rymcu/vertical/web/api/follow/FollowController.java create mode 100644 src/main/java/mapper/FollowMapper.xml diff --git a/src/main/java/com/rymcu/vertical/mapper/FollowMapper.java b/src/main/java/com/rymcu/vertical/mapper/FollowMapper.java new file mode 100644 index 0000000..09e1105 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/mapper/FollowMapper.java @@ -0,0 +1,19 @@ +package com.rymcu.vertical.mapper; + +import com.rymcu.vertical.core.mapper.Mapper; +import com.rymcu.vertical.entity.Follow; +import org.apache.ibatis.annotations.Param; + +/** + * @author ronger + */ +public interface FollowMapper extends Mapper { + /** + * 判断是否关注 + * @param followingId + * @param followerId + * @param followingType + * @return + */ + Boolean isFollow(@Param("followingId") Integer followingId, @Param("followerId") Integer followerId, @Param("followingType") String followingType); +} diff --git a/src/main/java/com/rymcu/vertical/service/FollowService.java b/src/main/java/com/rymcu/vertical/service/FollowService.java new file mode 100644 index 0000000..921b391 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/FollowService.java @@ -0,0 +1,35 @@ +package com.rymcu.vertical.service; + +import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.entity.Follow; +import com.rymcu.vertical.web.api.exception.BaseApiException; + +/** + * @author ronger + */ +public interface FollowService extends Service { + /** + * 判断是否关注 + * @param followingId + * @param followingType + * @return + * @throws BaseApiException + */ + Boolean isFollow(Integer followingId, String followingType) throws BaseApiException; + + /** + * 关注操作 + * @param follow + * @return + * @throws BaseApiException + */ + Boolean follow(Follow follow) throws BaseApiException; + + /** + * 取消关注操作 + * @param follow + * @return + * @throws BaseApiException + */ + Boolean cancelFollow(Follow follow) throws BaseApiException; +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java index 6ee4fe4..ccc9b6a 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -118,7 +118,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti String articleTags = article.getArticleTags(); String articleContent = article.getArticleContent(); String articleContentHtml = article.getArticleContentHtml(); - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); String reservedTag = checkTags(articleTags); boolean notification = false; if (StringUtils.isNotBlank(reservedTag)) { @@ -249,7 +249,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public Map share(Integer id) throws BaseApiException { Article article = articleMapper.selectByPrimaryKey(id); - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); StringBuilder shareUrl = new StringBuilder(article.getArticlePermalink()); shareUrl.append("?s=").append(user.getNickname()); Map map = new HashMap(1); @@ -259,7 +259,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public List findDrafts() throws BaseApiException { - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); List list = articleMapper.selectDrafts(user.getIdUser()); list.forEach(article -> { genArticle(article, 0); diff --git a/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java new file mode 100644 index 0000000..8066718 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java @@ -0,0 +1,45 @@ +package com.rymcu.vertical.service.impl; + +import com.rymcu.vertical.core.service.AbstractService; +import com.rymcu.vertical.entity.Follow; +import com.rymcu.vertical.entity.User; +import com.rymcu.vertical.mapper.FollowMapper; +import com.rymcu.vertical.service.FollowService; +import com.rymcu.vertical.util.UserUtils; +import com.rymcu.vertical.web.api.exception.BaseApiException; +import org.springframework.stereotype.Service; + +import javax.annotation.Resource; + +/** + * @author ronger + */ +@Service +public class FollowServiceImpl extends AbstractService implements FollowService { + + @Resource + private FollowMapper followMapper; + + @Override + public Boolean isFollow(Integer followingId, String followingType) throws BaseApiException { + User tokenUser = UserUtils.getCurrentUserByToken(); + Boolean b = followMapper.isFollow(followingId, tokenUser.getIdUser(), followingType); + return b; + } + + @Override + public Boolean follow(Follow follow) throws BaseApiException { + User tokenUser = UserUtils.getCurrentUserByToken(); + follow.setFollowerId(tokenUser.getIdUser()); + int result = followMapper.insertSelective(follow); + return result > 0; + } + + @Override + public Boolean cancelFollow(Follow follow) throws BaseApiException { + User tokenUser = UserUtils.getCurrentUserByToken(); + follow.setFollowerId(tokenUser.getIdUser()); + int result = followMapper.delete(follow); + return result == 0; + } +} diff --git a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java index d5814bc..fdf75db 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/PortfolioServiceImpl.java @@ -62,7 +62,7 @@ public class PortfolioServiceImpl extends AbstractService implements @Override public Portfolio postPortfolio(Portfolio portfolio) throws BaseApiException { - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); if (portfolio.getIdPortfolio() == null || portfolio.getIdPortfolio() == 0) { portfolio.setPortfolioAuthorId(user.getIdUser()); portfolio.setCreatedTime(new Date()); @@ -78,7 +78,7 @@ public class PortfolioServiceImpl extends AbstractService implements @Override public Map findUnbindArticles(Integer page, Integer rows, String searchText, Integer idPortfolio) throws BaseApiException { Map map = new HashMap(1); - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); Portfolio portfolio = portfolioMapper.selectByPrimaryKey(idPortfolio); if (portfolio == null) { map.put("message", "该作品集不存在或已被删除!"); diff --git a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java index 72be192..f0ef482 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/TagServiceImpl.java @@ -39,7 +39,7 @@ public class TagServiceImpl extends AbstractService implements TagService { @Override @Transactional(rollbackFor = { UnsupportedEncodingException.class,BaseApiException.class }) public Integer saveTagArticle(Article article) throws UnsupportedEncodingException, BaseApiException { - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); String articleTags = article.getArticleTags(); if(StringUtils.isNotBlank(articleTags)){ String[] tags = articleTags.split(","); diff --git a/src/main/java/com/rymcu/vertical/util/UserUtils.java b/src/main/java/com/rymcu/vertical/util/UserUtils.java index 0edbd09..502b6b0 100644 --- a/src/main/java/com/rymcu/vertical/util/UserUtils.java +++ b/src/main/java/com/rymcu/vertical/util/UserUtils.java @@ -25,7 +25,7 @@ public class UserUtils { * 通过token获取当前用户的信息 * @return */ - public static User getWxCurrentUser() throws BaseApiException { + public static User getCurrentUserByToken() throws BaseApiException { String authHeader = ContextHolderUtils.getRequest().getHeader(JwtConstants.AUTHORIZATION); if (authHeader == null) { return null; diff --git a/src/main/java/com/rymcu/vertical/web/api/follow/FollowController.java b/src/main/java/com/rymcu/vertical/web/api/follow/FollowController.java new file mode 100644 index 0000000..5e0fa28 --- /dev/null +++ b/src/main/java/com/rymcu/vertical/web/api/follow/FollowController.java @@ -0,0 +1,45 @@ +package com.rymcu.vertical.web.api.follow; + +import com.rymcu.vertical.core.result.GlobalResult; +import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.entity.Follow; +import com.rymcu.vertical.jwt.def.JwtConstants; +import com.rymcu.vertical.service.FollowService; +import com.rymcu.vertical.web.api.exception.BaseApiException; +import com.rymcu.vertical.web.api.exception.ErrorCode; +import org.apache.commons.lang.StringUtils; +import org.springframework.web.bind.annotation.*; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; + +/** + * @author ronger + */ +@RestController +@RequestMapping("/api/v1/follow") +public class FollowController { + + @Resource + private FollowService followService; + + @GetMapping("/is-follow") + public GlobalResult isFollow(@RequestParam Integer followingId, @RequestParam String followingType) throws BaseApiException { + Boolean b = followService.isFollow(followingId, followingType); + return GlobalResultGenerator.genSuccessResult(b); + } + + @PostMapping + public GlobalResult follow(@RequestBody Follow follow) throws BaseApiException { + Boolean b = followService.follow(follow); + return GlobalResultGenerator.genSuccessResult(b); + } + + @PostMapping("cancel-follow") + public GlobalResult cancelFollow(@RequestBody Follow follow) throws BaseApiException { + Boolean b = followService.cancelFollow(follow); + return GlobalResultGenerator.genSuccessResult(b); + } + + +} diff --git a/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java b/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java index 2b184e1..7f8085e 100644 --- a/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java +++ b/src/main/java/com/rymcu/vertical/web/api/notification/NotificationController.java @@ -30,7 +30,7 @@ public class NotificationController { @GetMapping("/all") public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); PageHelper.startPage(page, rows); List list = notificationService.findNotifications(user.getIdUser()); PageInfo pageInfo = new PageInfo(list); @@ -40,7 +40,7 @@ public class NotificationController { @GetMapping("/unread") public GlobalResult unreadNotification(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { - User user = UserUtils.getWxCurrentUser(); + User user = UserUtils.getCurrentUserByToken(); PageHelper.startPage(page, rows); List list = notificationService.findUnreadNotifications(user.getIdUser()); PageInfo pageInfo = new PageInfo(list); diff --git a/src/main/java/mapper/FollowMapper.xml b/src/main/java/mapper/FollowMapper.xml new file mode 100644 index 0000000..d65b9e5 --- /dev/null +++ b/src/main/java/mapper/FollowMapper.xml @@ -0,0 +1,8 @@ + + + + + \ No newline at end of file From e16e1a8db4b4ff52567d3bc66fb2edbdc177ad7f Mon Sep 17 00:00:00 2001 From: x ronger Date: Wed, 9 Sep 2020 22:59:35 +0800 Subject: [PATCH 2/2] =?UTF-8?q?:sparkles:=20=E5=85=B3=E6=B3=A8=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E9=80=9A=E7=9F=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/FollowServiceImpl.java | 5 +++ .../service/impl/NotificationServiceImpl.java | 32 ++++++++++++++++--- 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java index 8066718..ab7621f 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/FollowServiceImpl.java @@ -1,10 +1,12 @@ package com.rymcu.vertical.service.impl; +import com.rymcu.vertical.core.constant.NotificationConstant; import com.rymcu.vertical.core.service.AbstractService; import com.rymcu.vertical.entity.Follow; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.FollowMapper; import com.rymcu.vertical.service.FollowService; +import com.rymcu.vertical.util.NotificationUtils; import com.rymcu.vertical.util.UserUtils; import com.rymcu.vertical.web.api.exception.BaseApiException; import org.springframework.stereotype.Service; @@ -32,6 +34,9 @@ public class FollowServiceImpl extends AbstractService implements Follow User tokenUser = UserUtils.getCurrentUserByToken(); follow.setFollowerId(tokenUser.getIdUser()); int result = followMapper.insertSelective(follow); + if (result > 0) { + NotificationUtils.saveNotification(follow.getFollowingId(), follow.getIdFollow(), NotificationConstant.Follow, tokenUser.getNickname() + " 关注了你!"); + } return result > 0; } diff --git a/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java index 4bc3f54..5fc016e 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java @@ -5,14 +5,13 @@ import com.rymcu.vertical.dto.ArticleDTO; import com.rymcu.vertical.dto.Author; import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Comment; +import com.rymcu.vertical.entity.Follow; import com.rymcu.vertical.entity.Notification; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.mapper.NotificationMapper; -import com.rymcu.vertical.service.ArticleService; -import com.rymcu.vertical.service.CommentService; -import com.rymcu.vertical.service.NotificationService; -import com.rymcu.vertical.service.UserService; +import com.rymcu.vertical.service.*; import com.rymcu.vertical.util.BeanCopierUtil; +import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -35,6 +34,10 @@ public class NotificationServiceImpl extends AbstractService imple private CommentService commentService; @Resource private UserService userService; + @Resource + private FollowService followService; + @Value("${resource.domain}") + private String domain; @Override public List findUnreadNotifications(Integer idUser) { @@ -59,6 +62,7 @@ public class NotificationServiceImpl extends AbstractService imple ArticleDTO article; Comment comment; User user; + Follow follow; switch (notification.getDataType()) { case "0": // 系统公告/帖子 @@ -70,6 +74,11 @@ public class NotificationServiceImpl extends AbstractService imple break; case "1": // 关注 + follow = followService.findById(notification.getDataId().toString()); + notificationDTO.setDataTitle("关注提醒"); + user = userService.findById(follow.getFollowerId().toString()); + notificationDTO.setDataUrl(getFollowLink(follow.getFollowingType(), user.getNickname())); + notificationDTO.setAuthor(genAuthor(user)); break; case "2": // 回帖 @@ -80,10 +89,25 @@ public class NotificationServiceImpl extends AbstractService imple user = userService.findById(comment.getCommentAuthorId().toString()); notificationDTO.setAuthor(genAuthor(user)); break; + default: + break; } return notificationDTO; } + private String getFollowLink(String followingType, String id) { + StringBuilder url = new StringBuilder(); + url.append(domain); + switch (followingType) { + case "0": + url = url.append("/user/").append(id); + break; + default: + url.append("/notification"); + } + return url.toString(); + } + private Author genAuthor(User user) { Author author = new Author(); author.setUserNickname(user.getNickname());