本文最后更新于 2026年1月14日 上午
发表评论接口的实现
发表评论接口的需求
用户要登录之后才能在文章里评论和对别人的评论进行回复
用户登录后可以在友链页面进行评论
用户不需要登录就能能在留言板进行评论
在CommentServiceImpl类中添加方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
| @Override public ResponseResult addComment(Comment comment) { String type = comment.getType();
if (!StringUtils.hasText(comment.getContent())) { return ResponseResult.errorResult(AppHttpCodeEnum.CONTENT_NOT_NULL); }
comment.setType(type); comment.setArticleId(comment.getArticleId()); comment.setContent(comment.getContent()); comment.setRootId(comment.getRootId() != null ? comment.getRootId() : -1L); comment.setReplyToCommentId(comment.getReplyToCommentId()); comment.setReplyToUserId(comment.getReplyToUserId());
if (SystemConstants.COMMENT_TYPE_ARTICLE.equals(type) || SystemConstants.COMMENT_TYPE_LINK.equals(type)) { if (!SecurityUtils.isLogin()) { return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN); } LoginUser loginUser = SecurityUtils.getLoginUser(); User user = loginUser.getUser(); comment.setUserId(user.getId()); comment.setNickname(user.getNickname()); comment.setEmail(user.getEmail()); comment.setPersonalWebsite(comment.getPersonalWebsite()); } else if (SystemConstants.COMMENT_TYPE_MESSAGE.equals(type)) { if (SecurityUtils.isLogin()) { LoginUser loginUser = SecurityUtils.getLoginUser(); User user = loginUser.getUser(); comment.setUserId(user.getId()); comment.setNickname(user.getNickname()); comment.setEmail(user.getEmail()); comment.setPersonalWebsite(comment.getPersonalWebsite()); } else { if (!StringUtils.hasText(comment.getNickname())) { return ResponseResult.errorResult(AppHttpCodeEnum.NICKNAME_NOT_NULL); } if (!StringUtils.hasText(comment.getEmail())) { return ResponseResult.errorResult(AppHttpCodeEnum.EMAIL_NOT_NULL); } comment.setNickname(comment.getNickname()); comment.setEmail(comment.getEmail()); comment.setPersonalWebsite(comment.getPersonalWebsite()); } }
if (!StringUtils.hasText(comment.getAvatar())) { comment.setAvatar(GravatarUtils.getGravatarUrl(comment.getEmail())); }
comment.setLikeCount(0); comment.setStatus(SystemConstants.COMMENT_STATUS_NORMAL); comment.setDelFlag("0"); comment.setCreateTime(new Date());
save(comment);
CommentVo commentVo = BeanCopyUtils.copyBean(comment, CommentVo.class); if (StringUtils.hasText(comment.getEmail()) && comment.getEmail().equalsIgnoreCase(SystemConstants.BLOGGER_EMAIL)) { commentVo.setUserType("1"); } else if (comment.getUserId() != null) { User user = userMapper.selectById(comment.getUserId()); if (user != null) { commentVo.setUserType(user.getUserType()); } } else { commentVo.setUserType("0"); }
return ResponseResult.okResult(); }
|
添加通过邮箱获取用户头像的工具类GravatarUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
|
public class GravatarUtils {
private static final String GRAVATAR_URL = "https://cravatar.cn/avatar/"; private static final String QQ_AVATAR_URL = "https://q1.qlogo.cn/g?b=qq&nk="; private static final String DEFAULT_AVATAR = "identicon"; private static final int DEFAULT_SIZE = 80;
public static String getGravatarUrl(String email) { return getGravatarUrl(email, DEFAULT_SIZE); }
public static String getGravatarUrl(String email, int size) { if (email == null || email.trim().isEmpty()) { return GRAVATAR_URL + "00000000000000000000000000000000?d=" + DEFAULT_AVATAR + "&s=" + size; }
String trimmedEmail = email.trim().toLowerCase();
if (trimmedEmail.endsWith("@qq.com")) { String qqNumber = trimmedEmail.replace("@qq.com", ""); if (qqNumber.matches("\\d+")) { return QQ_AVATAR_URL + qqNumber + "&s=" + size; } }
String hash = md5Hex(trimmedEmail); return GRAVATAR_URL + hash + "?d=" + DEFAULT_AVATAR + "&s=" + size; }
private static String md5Hex(String input) { try { MessageDigest md = MessageDigest.getInstance("MD5"); byte[] digest = md.digest(input.getBytes()); StringBuilder sb = new StringBuilder(); for (byte b : digest) { sb.append(String.format("%02x", b)); } return sb.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("MD5 algorithm not found", e); } } }
|
添加获取用户登录token安全认证的工具类SecurityUtils
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
|
public class SecurityUtils {
public static LoginUser getLoginUser() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null || !authentication.isAuthenticated()) { return null; } Object principal = authentication.getPrincipal(); if (principal instanceof LoginUser) { return (LoginUser) principal; } return null; }
public static Long getUserId() { LoginUser loginUser = getLoginUser(); return loginUser != null ? loginUser.getUser().getId() : null; }
public static boolean isLogin() { return getLoginUser() != null; } }
|
再在SystemConstants中添加几个关于四个类型评论的常量
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
public static final String COMMENT_TYPE_LINK = "1";
public static final String COMMENT_TYPE_MESSAGE = "2";
public static final String COMMENT_STATUS_NORMAL = "0";
public static final String BLOGGER_EMAIL = "2962933152@qq.com";
|
PS:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2025 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/