diff --git a/src/main/java/com/rymcu/vertical/dto/ArticleSearchDTO.java b/src/main/java/com/rymcu/vertical/dto/ArticleSearchDTO.java new file mode 100644 index 0000000..1e3bb6f --- /dev/null +++ b/src/main/java/com/rymcu/vertical/dto/ArticleSearchDTO.java @@ -0,0 +1,17 @@ +package com.rymcu.vertical.dto; + +import lombok.Data; + +/** + * @author ronger + */ +@Data +public class ArticleSearchDTO { + + private String searchText; + + private String topicUri; + + private String tag; + +} diff --git a/src/main/java/com/rymcu/vertical/entity/Notification.java b/src/main/java/com/rymcu/vertical/entity/Notification.java index a104f2a..83ade86 100644 --- a/src/main/java/com/rymcu/vertical/entity/Notification.java +++ b/src/main/java/com/rymcu/vertical/entity/Notification.java @@ -43,7 +43,7 @@ public class Notification implements Serializable,Cloneable { * 数据摘要 */ @Column(name = "data_summary") - private String dataSummary ; + private String dataSummary; /** * 是否已读 */ diff --git a/src/main/java/com/rymcu/vertical/service/CommentService.java b/src/main/java/com/rymcu/vertical/service/CommentService.java index 5324a42..f859f25 100644 --- a/src/main/java/com/rymcu/vertical/service/CommentService.java +++ b/src/main/java/com/rymcu/vertical/service/CommentService.java @@ -8,6 +8,9 @@ import javax.servlet.http.HttpServletRequest; import java.util.List; import java.util.Map; +/** + * @author ronger + */ public interface CommentService extends Service { List getArticleComments(Integer idArticle); diff --git a/src/main/java/com/rymcu/vertical/service/NotificationService.java b/src/main/java/com/rymcu/vertical/service/NotificationService.java index df1c26f..14537b5 100644 --- a/src/main/java/com/rymcu/vertical/service/NotificationService.java +++ b/src/main/java/com/rymcu/vertical/service/NotificationService.java @@ -1,8 +1,8 @@ package com.rymcu.vertical.service; import com.rymcu.vertical.core.service.Service; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; -import org.apache.ibatis.annotations.Param; import java.util.List; @@ -23,7 +23,7 @@ public interface NotificationService extends Service { * @param idUser * @return */ - List findNotifications(Integer idUser); + List findNotifications(Integer idUser); /** * 获取消息数据 @@ -48,5 +48,5 @@ public interface NotificationService extends Service { * 标记消息已读 * @param id */ - void readNotification(Integer id); + Integer readNotification(Integer id); } 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 ef3808d..69d47d5 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/ArticleServiceImpl.java @@ -62,19 +62,19 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } else { list = articleMapper.selectArticles(searchDTO.getSearchText(), searchDTO.getTag(), searchDTO.getTopicUri()); } - list.forEach(article->{ - genArticle(article,0); + list.forEach(article -> { + genArticle(article, 0); }); return list; } @Override public ArticleDTO findArticleDTOById(Integer id, Integer type) { - ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id,type); + ArticleDTO articleDTO = articleMapper.selectArticleDTOById(id, type); if (articleDTO == null) { return null; } - articleDTO = genArticle(articleDTO,type); + articleDTO = genArticle(articleDTO, type); return articleDTO; } @@ -82,7 +82,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti public List findArticlesByTopicUri(String name) { List articleDTOS = articleMapper.selectArticlesByTopicUri(name); articleDTOS.forEach(articleDTO -> { - genArticle(articleDTO,0); + genArticle(articleDTO, 0); }); return articleDTOS; } @@ -96,22 +96,22 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public List findUserArticlesByIdUser(Integer idUser) { List list = articleMapper.selectUserArticles(idUser); - list.forEach(article->{ - genArticle(article,0); + list.forEach(article -> { + genArticle(article, 0); }); return list; } @Override - @Transactional(rollbackFor = { UnsupportedEncodingException.class, BaseApiException.class }) + @Transactional(rollbackFor = {UnsupportedEncodingException.class, BaseApiException.class}) public Map postArticle(ArticleDTO article, HttpServletRequest request) throws UnsupportedEncodingException, BaseApiException { Map map = new HashMap(1); - if(StringUtils.isBlank(article.getArticleTitle())){ - map.put("message","标题不能为空!"); + if (StringUtils.isBlank(article.getArticleTitle())) { + map.put("message", "标题不能为空!"); return map; } - if(StringUtils.isBlank(article.getArticleContent())){ - map.put("message","正文不能为空!"); + if (StringUtils.isBlank(article.getArticleContent())) { + map.put("message", "正文不能为空!"); return map; } String articleTitle = article.getArticleTitle(); @@ -131,7 +131,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti } } Article newArticle; - if(article.getIdArticle() == null || article.getIdArticle() == 0){ + if (article.getIdArticle() == null || article.getIdArticle() == 0) { newArticle = new Article(); newArticle.setArticleTitle(articleTitle); newArticle.setArticleAuthorId(user.getIdUser()); @@ -140,21 +140,21 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setUpdatedTime(newArticle.getCreatedTime()); newArticle.setArticleStatus(article.getArticleStatus()); articleMapper.insertSelective(newArticle); - articleMapper.insertArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); + articleMapper.insertArticleContent(newArticle.getIdArticle(), articleContent, articleContentHtml); if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) { BaiDuUtils.sendSEOData(newArticle.getArticlePermalink()); } } else { newArticle = articleMapper.selectByPrimaryKey(article.getIdArticle()); - if(!user.getIdUser().equals(newArticle.getArticleAuthorId())){ - map.put("message","非法访问!"); + if (!user.getIdUser().equals(newArticle.getArticleAuthorId())) { + map.put("message", "非法访问!"); return map; } newArticle.setArticleTitle(articleTitle); newArticle.setArticleTags(articleTags); newArticle.setArticleStatus(article.getArticleStatus()); newArticle.setUpdatedTime(new Date()); - articleMapper.updateArticleContent(newArticle.getIdArticle(),articleContent,articleContentHtml); + articleMapper.updateArticleContent(newArticle.getIdArticle(), articleContent, articleContentHtml); if (!ProjectConstant.ENV.equals(env) && defaultStatus.equals(newArticle.getArticleStatus())) { BaiDuUtils.sendUpdateSEOData(newArticle.getArticlePermalink()); } @@ -174,12 +174,12 @@ public class ArticleServiceImpl extends AbstractService
implements Arti newArticle.setArticleLink("/draft/" + newArticle.getIdArticle()); } - if(StringUtils.isNotBlank(articleContentHtml)){ + if (StringUtils.isNotBlank(articleContentHtml)) { Integer length = articleContentHtml.length(); - if(length > MAX_PREVIEW){ + if (length > MAX_PREVIEW) { length = MAX_PREVIEW; } - String articlePreviewContent = articleContentHtml.substring(0,length); + String articlePreviewContent = articleContentHtml.substring(0, length); newArticle.setArticlePreviewContent(Html2TextUtil.getContent(articlePreviewContent)); } articleMapper.updateByPrimaryKeySelective(newArticle); @@ -190,7 +190,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti private String checkTags(String articleTags) { // 判断文章是否有标签 - if(StringUtils.isBlank(articleTags)){ + if (StringUtils.isBlank(articleTags)) { return ""; } // 判断是否存在系统配置的保留标签词 @@ -206,7 +206,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti continue; } - for (String articleTag: articleTagArr) { + for (String articleTag : articleTagArr) { if (StringUtils.isBlank(articleTag)) { continue; } @@ -223,13 +223,13 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override @Transactional(rollbackFor = Exception.class) public Map delete(Integer id) { - Map map = new HashMap(1); + Map map = new HashMap(1); Integer result; // 删除引用标签记录 result = articleMapper.deleteTagArticle(id); - if (result > 0){ + if (result > 0) { result = articleMapper.deleteByPrimaryKey(id); - if (result < 1){ + if (result < 1) { map.put("message", "删除失败!"); } } else { @@ -261,8 +261,8 @@ public class ArticleServiceImpl extends AbstractService
implements Arti public List findDrafts() throws BaseApiException { User user = UserUtils.getWxCurrentUser(); List list = articleMapper.selectDrafts(user.getIdUser()); - list.forEach(article->{ - genArticle(article,0); + list.forEach(article -> { + genArticle(article, 0); }); return list; } @@ -270,17 +270,17 @@ public class ArticleServiceImpl extends AbstractService
implements Arti @Override public List findArticlesByIdPortfolio(Integer idPortfolio) { List list = articleMapper.selectArticlesByIdPortfolio(idPortfolio); - list.forEach(article->{ - genArticle(article,0); + list.forEach(article -> { + genArticle(article, 0); }); return list; } @Override public List selectUnbindArticles(Integer idPortfolio, String searchText, Integer idUser) { - List list = articleMapper.selectUnbindArticlesByIdPortfolio(idPortfolio,searchText,idUser); - list.forEach(article->{ - genArticle(article,0); + List list = articleMapper.selectUnbindArticlesByIdPortfolio(idPortfolio, searchText, idUser); + list.forEach(article -> { + genArticle(article, 0); }); return list; } @@ -296,7 +296,7 @@ public class ArticleServiceImpl extends AbstractService
implements Arti article.setTags(tags); if (!type.equals(ARTICLE_LIST)) { ArticleContent articleContent = articleMapper.selectArticleContent(article.getIdArticle()); - if (type.equals(ARTICLE_VIEW)){ + if (type.equals(ARTICLE_VIEW)) { article.setArticleContent(articleContent.getArticleContentHtml()); // 获取所属作品集列表数据 List portfolioArticleDTOList = articleMapper.selectPortfolioArticles(article.getIdArticle()); diff --git a/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java b/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java index f7268e1..b6aab70 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/CommentServiceImpl.java @@ -83,7 +83,7 @@ public class CommentServiceImpl extends AbstractService implements Comm comment.setCreatedTime(new Date()); commentMapper.insertSelective(comment); StringBuilder commentSharpUrl = new StringBuilder(article.getArticlePermalink()); - commentSharpUrl.append("/comment/").append(comment.getIdComment()); + commentSharpUrl.append("#comment-").append(comment.getIdComment()); commentMapper.updateCommentSharpUrl(comment.getIdComment(), commentSharpUrl.toString()); String commentContent = comment.getCommentContent(); 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 564b17b..4bc3f54 100644 --- a/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/rymcu/vertical/service/impl/NotificationServiceImpl.java @@ -1,13 +1,25 @@ package com.rymcu.vertical.service.impl; import com.rymcu.vertical.core.service.AbstractService; +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.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.util.BeanCopierUtil; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import javax.annotation.Resource; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * @author ronger @@ -17,31 +29,83 @@ public class NotificationServiceImpl extends AbstractService imple @Resource private NotificationMapper notificationMapper; + @Resource + private ArticleService articleService; + @Resource + private CommentService commentService; + @Resource + private UserService userService; @Override - public List findUnreadNotifications(Integer idUser){ + public List findUnreadNotifications(Integer idUser) { List list = notificationMapper.selectUnreadNotifications(idUser); return list; } @Override - public List findNotifications(Integer idUser) { + public List findNotifications(Integer idUser) { List list = notificationMapper.selectNotifications(idUser); - return list; + List notifications = new ArrayList<>(); + list.forEach(notification -> { + NotificationDTO notificationDTO = genNotification(notification); + notifications.add(notificationDTO); + }); + return notifications; + } + + private NotificationDTO genNotification(Notification notification) { + NotificationDTO notificationDTO = new NotificationDTO(); + BeanCopierUtil.copy(notification, notificationDTO); + ArticleDTO article; + Comment comment; + User user; + switch (notification.getDataType()) { + case "0": + // 系统公告/帖子 + article = articleService.findArticleDTOById(notification.getDataId(), 0); + notificationDTO.setDataTitle("系统公告"); + notificationDTO.setDataUrl(article.getArticlePermalink()); + user = userService.findById(article.getArticleAuthorId().toString()); + notificationDTO.setAuthor(genAuthor(user)); + break; + case "1": + // 关注 + break; + case "2": + // 回帖 + comment = commentService.findById(notification.getDataId().toString()); + article = articleService.findArticleDTOById(comment.getCommentArticleId(), 0); + notificationDTO.setDataTitle(article.getArticleTitle()); + notificationDTO.setDataUrl(comment.getCommentSharpUrl()); + user = userService.findById(comment.getCommentAuthorId().toString()); + notificationDTO.setAuthor(genAuthor(user)); + break; + } + return notificationDTO; + } + + private Author genAuthor(User user) { + Author author = new Author(); + author.setUserNickname(user.getNickname()); + author.setUserAvatarURL(user.getAvatarUrl()); + author.setIdUser(user.getIdUser()); + return author; } @Override public Notification findNotification(Integer idUser, Integer dataId, String dataType) { - return notificationMapper.selectNotification(idUser,dataId,dataType); + return notificationMapper.selectNotification(idUser, dataId, dataType); } @Override + @Transactional(rollbackFor = Exception.class) public Integer save(Integer idUser, Integer dataId, String dataType, String dataSummary) { - return notificationMapper.insertNotification(idUser,dataId,dataType,dataSummary); + return notificationMapper.insertNotification(idUser, dataId, dataType, dataSummary); } @Override - public void readNotification(Integer id) { - notificationMapper.readNotification(id); + @Transactional(rollbackFor = Exception.class) + public Integer readNotification(Integer id) { + return notificationMapper.readNotification(id); } } diff --git a/src/main/java/com/rymcu/vertical/util/Utils.java b/src/main/java/com/rymcu/vertical/util/Utils.java index e3554c0..7b55a2f 100644 --- a/src/main/java/com/rymcu/vertical/util/Utils.java +++ b/src/main/java/com/rymcu/vertical/util/Utils.java @@ -2,6 +2,7 @@ package com.rymcu.vertical.util; import com.github.pagehelper.PageInfo; import com.rymcu.vertical.dto.ArticleDTO; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; import com.rymcu.vertical.entity.User; import org.apache.shiro.SecurityUtils; @@ -34,20 +35,19 @@ public class Utils { String plain = Encodes.unescapeHtml(plainPassword); byte[] salt = Digests.generateSalt(SALT_SIZE); byte[] hashPassword = Digests.sha1(plain.getBytes(), salt, HASH_INTERATIONS); - return Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword); + return Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword); } /** - * 一般检查工具密码比对 add by xlf 2018-11-8 - * + *一般检查工具密码比对 add by xlf 2018-11-8 * @param pwd - * @param enpwd 加密的密码 + * @param enpwd 加密的密码 * @return */ - public static boolean comparePwd(String pwd, String enpwd) { - byte[] salt = Encodes.decodeHex(enpwd.substring(0, 16)); + public static boolean comparePwd(String pwd,String enpwd){ + byte[] salt = Encodes.decodeHex(enpwd.substring(0,16)); byte[] hashPassword = Digests.sha1(pwd.getBytes(), salt, HASH_INTERATIONS); - return enpwd.equals(Encodes.encodeHex(salt) + Encodes.encodeHex(hashPassword)); + return enpwd.equals(Encodes.encodeHex(salt)+Encodes.encodeHex(hashPassword)); } public static User getCurrentUser() { @@ -55,34 +55,33 @@ public class Utils { } public static Session getSession() { - try { + try{ Subject subject = SecurityUtils.getSubject(); Session session = subject.getSession(false); - if (session == null) { + if (session == null){ session = subject.getSession(); } - if (session != null) { + if (session != null){ return session; } subject.logout(); - } catch (InvalidSessionException e) { + }catch (InvalidSessionException e){ } return null; } public static Integer genCode() { - Integer code = (int) ((Math.random() * 9 + 1) * 100000); + Integer code = (int)((Math.random()*9+1)*100000); return code; } /** * 获取配置文件内属性 - * * @param key 键值 * @return 属性值 - */ - public static String getProperty(String key) { + * */ + public static String getProperty(String key){ return env.getProperty(key); } @@ -98,24 +97,24 @@ public class Utils { LocalDate today = LocalDate.now(); Period p = Period.between(oldLocalDate, today); - if (p.getYears() > 0) { - timeAgo = p.getYears() + " 年前 "; - } else if (p.getMonths() > 0) { - timeAgo = p.getMonths() + " 月前 "; - } else if (p.getDays() > 0) { - timeAgo = p.getDays() + " 天前 "; - } else { + if(p.getYears() > 0){ + timeAgo = p.getYears()+" 年前 "; + }else if(p.getMonths() > 0){ + timeAgo = p.getMonths()+" 月前 "; + }else if(p.getDays() > 0){ + timeAgo = p.getDays()+" 天前 "; + }else { long to = System.currentTimeMillis(); long from = date.getTime(); - int hours = (int) ((to - from) / (1000 * 60 * 60)); - if (hours > 0) { - timeAgo = hours + " 小时前 "; - } else { - int minutes = (int) ((to - from) / (1000 * 60)); - if (minutes < 5) { + int hours = (int) ((to - from)/(1000 * 60 * 60)); + if(hours > 0){ + timeAgo = hours+" 小时前 "; + }else { + int minutes = (int) ((to - from)/(1000 * 60)); + if(minutes == 0){ timeAgo = " 刚刚 "; - } else { - timeAgo = minutes + " 分钟前 "; + }else { + timeAgo = minutes+" 分钟前 "; } } } @@ -124,13 +123,13 @@ public class Utils { public static Map getPagination(PageInfo pageInfo) { Map pagination = new HashMap(3); - pagination.put("pageSize", pageInfo.getPageSize()); - pagination.put("total", pageInfo.getTotal()); - pagination.put("currentPage", pageInfo.getPageNum()); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); return pagination; } - public static void main(String[] args) { + public static void main(String[] args){ LocalDate localDate = LocalDate.parse("2019-11-15"); ZoneId zone = ZoneId.systemDefault(); Instant instant = localDate.atStartOfDay().atZone(zone).toInstant(); @@ -142,9 +141,9 @@ public class Utils { Map map = new HashMap(2); map.put("articles", pageInfo.getList()); Map pagination = new HashMap(4); - pagination.put("pageSize", pageInfo.getPageSize()); - pagination.put("total", pageInfo.getTotal()); - pagination.put("currentPage", pageInfo.getPageNum()); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); map.put("pagination", pagination); return map; } @@ -153,9 +152,9 @@ public class Utils { Map map = new HashMap(2); map.put("notifications", pageInfo.getList()); Map pagination = new HashMap(4); - pagination.put("pageSize", pageInfo.getPageSize()); - pagination.put("total", pageInfo.getTotal()); - pagination.put("currentPage", pageInfo.getPageNum()); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); map.put("pagination", pagination); return map; } @@ -184,4 +183,15 @@ public class Utils { return ip; } + + public static Map getNotificationDTOsGlobalResult(PageInfo pageInfo) { + Map map = new HashMap(2); + map.put("notifications", pageInfo.getList()); + Map pagination = new HashMap(4); + pagination.put("pageSize",pageInfo.getPageSize()); + pagination.put("total",pageInfo.getTotal()); + pagination.put("currentPage",pageInfo.getPageNum()); + map.put("pagination", pagination); + return map; + } } 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 3186585..2b184e1 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 @@ -4,6 +4,7 @@ import com.github.pagehelper.PageHelper; import com.github.pagehelper.PageInfo; import com.rymcu.vertical.core.result.GlobalResult; import com.rymcu.vertical.core.result.GlobalResultGenerator; +import com.rymcu.vertical.dto.NotificationDTO; import com.rymcu.vertical.entity.Notification; import com.rymcu.vertical.entity.User; import com.rymcu.vertical.service.NotificationService; @@ -31,9 +32,9 @@ public class NotificationController { public GlobalResult notifications(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "10") Integer rows) throws BaseApiException { User user = UserUtils.getWxCurrentUser(); PageHelper.startPage(page, rows); - List list = notificationService.findNotifications(user.getIdUser()); - PageInfo pageInfo = new PageInfo(list); - Map map = Utils.getNotificationsGlobalResult(pageInfo); + List list = notificationService.findNotifications(user.getIdUser()); + PageInfo pageInfo = new PageInfo(list); + Map map = Utils.getNotificationDTOsGlobalResult(pageInfo); return GlobalResultGenerator.genSuccessResult(map); } @@ -48,8 +49,12 @@ public class NotificationController { } @PutMapping("/read/{id}") - public void read(@PathVariable Integer id) { - notificationService.readNotification(id); + public GlobalResult read(@PathVariable Integer id) { + Integer result = notificationService.readNotification(id); + if (result == 0) { + return GlobalResultGenerator.genErrorResult("标记已读失败"); + } + return GlobalResultGenerator.genSuccessResult("标记已读成功"); } }