本文最后更新于 2026年4月17日 凌晨
发表评论接口的实现
发表评论接口的需求
用户要登录之后才能在文章里评论和对别人的评论进行回复
用户登录后可以在友链页面进行评论
用户不需要登录就能能在留言板进行评论
在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
| @Override public ResponseResult addComment(AddCommentDto addCommentDto) { if (!StringUtils.hasText(addCommentDto.getContent())) { return ResponseResult.errorResult(AppHttpCodeEnum.CONTENT_NOT_NULL); }
Comment comment = BeanCopyUtils.copyBean(addCommentDto, Comment.class);
if (comment.getRootId() == null) { comment.setRootId(-1L); }
String type = addCommentDto.getType();
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(); SysUser sysUser = loginUser.getSysUser(); comment.setUserId(sysUser.getId()); comment.setNickname(sysUser.getNickname()); comment.setEmail(sysUser.getEmail()); } else if (SystemConstants.COMMENT_TYPE_MESSAGE.equals(type)) { if (SecurityUtils.isLogin()) { LoginUser loginUser = SecurityUtils.getLoginUser(); SysUser sysUser = loginUser.getSysUser(); comment.setUserId(sysUser.getId()); comment.setNickname(sysUser.getNickname()); comment.setEmail(sysUser.getEmail()); } else { if (!StringUtils.hasText(addCommentDto.getNickname())) { return ResponseResult.errorResult(AppHttpCodeEnum.NICKNAME_NOT_NULL); } if (!StringUtils.hasText(addCommentDto.getEmail())) { return ResponseResult.errorResult(AppHttpCodeEnum.EMAIL_NOT_NULL); } } }
if (comment.getUserId() != null) { SysUser sysUser = sysUserMapper.selectById(comment.getUserId()); if (sysUser != null && StringUtils.hasText(sysUser.getAvatar())) { comment.setAvatar(sysUser.getAvatar()); } else { comment.setAvatar(GravatarUtils.getCommentPreviewAvatarUrl(comment.getEmail())); } } else { comment.setAvatar(GravatarUtils.getCommentPreviewAvatarUrl(comment.getEmail())); }
comment.setLikeCount(0); comment.setStatus(SystemConstants.COMMENT_STATUS_NORMAL); comment.setDelFlag("0"); comment.setCreateTime(new Date());
save(comment);
emailService.sendCommentNotificationByEmail(comment);
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:该系列只做为作者学习开发项目做的笔记用
不一定符合读者来学习,仅供参考
预告
后续会记录博客的开发过程
每次学习会做一份笔记来进行发表
“一花一世界,一叶一菩提”
版权所有 © 2026 云梦泽
欢迎访问我的个人网站:https://hgt12.github.io/