package top.naccl.controller;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.mail.MailProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.naccl.po.Comment;
import top.naccl.po.User;
import top.naccl.service.BlogService;
import top.naccl.service.CommentService;
import top.naccl.util.IpAddressUtils;
import top.naccl.util.MD5Utils;
import top.naccl.util.MailUtils;
import top.naccl.util.verifycode.VerifyCode;
import top.naccl.util.verifycode.VerifyCodeFactory;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/**
* @Description: 评论处理
* @Author: Naccl
* @Date: 2020-04-30
*/
@Controller
public class CommentController {
@Autowired
private CommentService commentService;
@Autowired
private BlogService blogService;
@Autowired
private MailUtils mailUtils;
@Autowired
private MailProperties mailProperties;
// @Value("${comment.avatar}")
private String avatar;
/**
* 获取评论列表
*/
@GetMapping("/comments/{blogId}")
public String blogComments(@PathVariable Long blogId, Model model) {
model.addAttribute("comments", commentService.listCommentByBlogId(blogId));
return "blog :: commentList";
}
/**
* 获取评论列表
*/
@GetMapping("/comments/about")
public String aboutComments(Model model) {
model.addAttribute("comments", commentService.listCommentByAboutMe(true));
return "about :: commentList";
}
/**
* 获取评论验证码
*/
@GetMapping("/verifyCode")
public void getVerifyCode(HttpSession session, HttpServletResponse response) {
VerifyCodeFactory verifyCodeFactory = new VerifyCodeFactory();
try {
//设置长宽
VerifyCode verifyCode = verifyCodeFactory.getMathVerifyCode(100, 30, '-');
String answer = verifyCode.getCode();
//将verifyCode绑定到session
session.setAttribute("verifyCode", answer);
//设置响应头信息,告诉浏览器不要缓存此内容
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
//设置HttpOnly属性,防止Xss攻击,加了这个,第一次获取的sessionID和之后的sessionID不一致
//response.setHeader("Set-Cookie", "name=value; HttpOnly");
//在代理服务器端防止缓冲
response.setDateHeader("Expires", 0);
//设置响应内容类型
response.setContentType("image/jpeg");
response.getOutputStream().write(verifyCode.getImgBytes());
response.getOutputStream().flush();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* POST评论
*/
@PostMapping("/comments")
@ResponseBody
public JSONObject judgeVerifyCode(HttpSession session, HttpServletRequest request, Comment comment) {
String answer = (String) session.getAttribute("verifyCode");
String inputVerifyCode = request.getParameter("verifyCode");
JSONObject result = new JSONObject();
if (answer == null) {
result.put("success", false);
result.put("errorInfo", 2);
return result;
}
if (inputVerifyCode != null && !"".equals(inputVerifyCode) && answer.equalsIgnoreCase(inputVerifyCode)) {
saveComment(comment, session, request);//保存评论
result.put("success", true);
session.setAttribute("verifyCode", null);
sendMailToParentComment(comment);//发送邮件提醒回复对象
sendMailToMe(comment);//发送邮件提醒我自己
} else {
result.put("success", false);
result.put("errorInfo", 1);
}
return result;
}
/**
* 保存评论
*/
private void saveComment(Comment comment, HttpSession session, HttpServletRequest request) {
if (comment.isAboutMe()) {
comment.setAboutMe(true);
comment.setBlog(null);
} else {
comment.setAboutMe(false);
Long blogId = comment.getBlog().getId();
comment.setBlog(blogService.getBlog(blogId));
}
User user = (User) session.getAttribute("user");
if (user != null) {
comment.setAdminComment(true);
comment.setAvatar(user.getAvatar());
comment.setNickname(user.getNickname());
comment.setEmail(user.getEmail());
} else {
String nicknameMD5 = MD5Utils.getMD5(comment.getNickname());//根据评论昵称取MD5,保证每一个昵称对应一个头像
char m = nicknameMD5.charAt(nicknameMD5.length() - 1);//取MD5最后一位
int num = m % 6 + 1;//计算对应的头像
avatar = "/images/comment-avatar/" + num + ".jpg";
comment.setAvatar(avatar);
}
comment.setPublished(true);//默认发表时公开
String ip = IpAddressUtils.getIpAddress(request);
comment.setIp(ip);
commentService.saveComment(comment);
}
/**
* 发送邮件提醒回复对象
*/
private void sendMailToParentComment(Comment comment) {
if (comment.getParentComment() != null && comment.getParentComment().isReceiveEmail()) {
String url;
if (comment.isAboutMe()) {
url = "https://naccl.top/about";
} else {
url = "https://naccl.top/blog/" + comment.getBlog().getId();
}
String toAccount = comment.getParentComment().getEmail();
String subject = "Naccl's Blog评论回复";
String content = "<body><h2>您的评论有新回复</h2><p><a href='" + url + "'>详情请看" + url + "</a></p><p>此邮件为自动发送,如不想再收到此类消息,请回复TD</p></body>";
mailUtils.sendHTMLMail(toAccount, subject, content);
}
}
/**
* 发送邮件提醒我自己
*/
private void sendMailToMe(Comment comment) {
//如果有父评论 且父评论是我自己 或是我自己的回复 就不用再发一封了
if (comment.getParentComment() == null || !mailProperties.getUsername().equals(comment.getParentComment().getEmail()) || !mailProperties.getUsername().equals(comment.getEmail())) {
String url;
if (comment.isAboutMe()) {
url = "https://naccl.top/about";
} else {
url = "https://naccl.top/blog/" + comment.getBlog().getId();
}
String toAccount = mailProperties.getUsername();
String subject = "Naccl's Blog新评论";
String content = "<body><p><a href='" + url + "'>新评论" + url + "</a></p></body>";
mailUtils.sendHTMLMail(toAccount, subject, content);
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Spring Boot + Thymeleaf + JPA + Semantic UI 博客系统.zip
共525个文件
js:200个
html:122个
java:74个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 105 浏览量
2024-04-29
23:39:38
上传
评论
收藏 2.89MB ZIP 举报
温馨提示
【博客个人资源】 包含前端、后端、移动开发、操作系统、人工智能、物联网、信息化管理、数据库、硬件开发、大数据、课程资源、音视频、网站开发等各种技术项目的源码。包括STM32、ESP8266、PHP、QT、Linux、iOS、C++、Java、python、web、C#、EDA、proteus、RTOS等项目的源码。 【技术】 Java、Python、Node.js、Spring Boot、Django、Express、MySQL、PostgreSQL、MongoDB、React、Angular、Vue、Bootstrap、Material-UI、Redis、Docker、Kubernetes
资源推荐
资源详情
资源评论
收起资源包目录
Spring Boot + Thymeleaf + JPA + Semantic UI 博客系统.zip (525个子文件)
AUTHORS 6KB
editormd.css 76KB
editormd.min.css 60KB
editormd.preview.css 55KB
editormd.preview.min.css 44KB
ambiance.css 26KB
animate.css 23KB
viewer.css 9KB
me.css 9KB
codemirror.css 8KB
typo.css 7KB
codemirror.min.css 5KB
prism.css 5KB
mdn-like.css 5KB
solarized.css 5KB
merge.css 3KB
lint.css 3KB
xq-dark.css 3KB
prism.css 3KB
lesser-dark.css 2KB
pastel-on-dark.css 2KB
xq-light.css 2KB
tomorrow-night-eighties.css 2KB
editormd.logo.css 2KB
erlang-dark.css 2KB
zenburn.css 2KB
twilight.css 2KB
midnight.css 2KB
vibrant-ink.css 2KB
mbo.css 2KB
base16-dark.css 2KB
base16-light.css 2KB
tern.css 2KB
3024-night.css 2KB
paraiso-dark.css 2KB
paraiso-light.css 2KB
tomorrow-night-bright.css 2KB
3024-day.css 2KB
blackboard.css 2KB
colorforth.css 2KB
editormd.logo.min.css 2KB
the-matrix.css 2KB
night.css 2KB
rubyblue.css 2KB
monokai.css 1KB
cobalt.css 1KB
github-badge.css 1KB
simplescrollbars.css 1KB
eclipse.css 1KB
skill-style.css 1007B
neo.css 932B
tocbot.css 924B
elegant.css 768B
neat.css 693B
show-hint.css 662B
dialog.css 502B
tiki.css 440B
foldgutter.css 435B
tiddlywiki.css 220B
matchesonscrollbar.css 188B
fullscreen.css 116B
ambiance-mobile.css 103B
fontawesome-webfont.eot 59KB
editormd-logo.eot 1KB
[email protected] 21KB
[email protected] 16KB
loading.gif 8KB
.gitattributes 86B
.gitignore 427B
blog.html 45KB
_fragments.html 33KB
scala.html 28KB
index.html 22KB
about.html 21KB
index.html 17KB
index.html 13KB
index.html 13KB
index.html 11KB
index.html 11KB
blogs-input.html 10KB
index.html 10KB
tags.html 10KB
types.html 9KB
search.html 9KB
index.html 9KB
index.html 8KB
index.html 8KB
blogs.html 7KB
index.html 7KB
index.html 7KB
index.html 6KB
index.html 6KB
index.html 6KB
tags-input.html 6KB
comments-trash.html 6KB
index.html 6KB
index.html 6KB
index.html 6KB
comments.html 5KB
index.html 5KB
共 525 条
- 1
- 2
- 3
- 4
- 5
- 6
资源评论
枫蜜柚子茶
- 粉丝: 6282
- 资源: 4641
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功