From f639b5bf8a20b72b782558ca01f086a4ed54c520 Mon Sep 17 00:00:00 2001
From: inleft <inleft@qq.com>
Date: Thu, 14 Jul 2022 15:27:35 +0800
Subject: [PATCH] 添加邮件回复功能

---
 snowy-base/snowy-core/src/main/java/vip/xiaonuo/core/consts/MyConstant.java                                         |    4 +
 snowy-main/src/main/resources/application-local.yml                                                                 |    2 
 snowy-main/src/main/resources/templates/emailReplyModel.ftl                                                         |   41 +++++++++++++
 snowy-base/snowy-core/pom.xml                                                                                       |    9 +++
 snowy-main/src/main/java/vip/xiaonuo/modular/blogarticlecomment/controller/BlogArticleCommentOutsideController.java |   96 +++++++++++++++++++++++++++++++
 snowy-main/src/main/resources/application-prod.yml                                                                  |    2 
 snowy-main/pom.xml                                                                                                  |    8 ++
 7 files changed, 157 insertions(+), 5 deletions(-)

diff --git a/snowy-base/snowy-core/pom.xml b/snowy-base/snowy-core/pom.xml
index e6be763..a825562 100644
--- a/snowy-base/snowy-core/pom.xml
+++ b/snowy-base/snowy-core/pom.xml
@@ -180,6 +180,15 @@
             <groupId>com.tencentcloudapi</groupId>
             <artifactId>tencentcloud-sdk-java</artifactId>
         </dependency>
+
+
+        <!-- https://mvnrepository.com/artifact/com.sun.mail/javax.mail -->
+        <dependency>
+            <groupId>com.sun.mail</groupId>
+            <artifactId>javax.mail</artifactId>
+            <version>1.6.2</version>
+        </dependency>
+
     </dependencies>
 
     <build>
diff --git a/snowy-base/snowy-core/src/main/java/vip/xiaonuo/core/consts/MyConstant.java b/snowy-base/snowy-core/src/main/java/vip/xiaonuo/core/consts/MyConstant.java
index 4cf0d4f..7d50a43 100644
--- a/snowy-base/snowy-core/src/main/java/vip/xiaonuo/core/consts/MyConstant.java
+++ b/snowy-base/snowy-core/src/main/java/vip/xiaonuo/core/consts/MyConstant.java
@@ -7,6 +7,10 @@
 
     String privateComment = "(悄悄话已隐藏)******";
     String checkComment = "(未审核消息)******";
+    String inleft= "inleft";
+    String email = "inleft@qq.com";
+    String url_1 = "http://blog.inleft.com/comment#%d";
+    String url_2 = "http://blog.inleft.com/mdDetail?id=%d#%d";
 
     interface AuthStatus {
         int publicCode = 1;
diff --git a/snowy-main/pom.xml b/snowy-main/pom.xml
index f4e67df..5f9a1ce 100644
--- a/snowy-main/pom.xml
+++ b/snowy-main/pom.xml
@@ -37,6 +37,7 @@
             <scope>test</scope>
         </dependency>
 
+        <!-- pdf demo放在这里,以后需要可以利用-->
         <!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
         <dependency>
             <groupId>com.itextpdf</groupId>
@@ -44,7 +45,12 @@
             <version>5.5.13.2</version>
         </dependency>
 
-
+        <!-- 模板引擎,用于邮件回复-->
+        <dependency>
+            <groupId>org.freemarker</groupId>
+            <artifactId>freemarker</artifactId>
+            <version>2.3.31</version>
+        </dependency>
 
     </dependencies>
 
diff --git a/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticlecomment/controller/BlogArticleCommentOutsideController.java b/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticlecomment/controller/BlogArticleCommentOutsideController.java
index fa50c0e..194209a 100644
--- a/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticlecomment/controller/BlogArticleCommentOutsideController.java
+++ b/snowy-main/src/main/java/vip/xiaonuo/modular/blogarticlecomment/controller/BlogArticleCommentOutsideController.java
@@ -26,11 +26,20 @@
 
 import cn.hutool.core.bean.BeanUtil;
 import cn.hutool.core.collection.CollUtil;
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.lang.Dict;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.template.Template;
+import cn.hutool.extra.template.TemplateConfig;
+import cn.hutool.extra.template.TemplateEngine;
+import cn.hutool.extra.template.TemplateUtil;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import org.springframework.validation.annotation.Validated;
 import org.springframework.web.bind.annotation.*;
 import vip.xiaonuo.core.annotion.BusinessLog;
 import vip.xiaonuo.core.consts.MyConstant;
+import vip.xiaonuo.core.email.MailSender;
+import vip.xiaonuo.core.email.modular.model.SendMailParam;
 import vip.xiaonuo.core.enums.LogAnnotionOpTypeEnum;
 import vip.xiaonuo.core.exception.BlogException;
 import vip.xiaonuo.core.pojo.response.ResponseData;
@@ -60,19 +69,27 @@
     @Resource
     private BlogArticleService blogArticleService;
 
-
     @Resource
     private BlogArticleCommentService blogArticleCommentService;
 
 
+    @Resource
+    private MailSender mailSender;
+
     @PostMapping("/blogComment/add")
     @BusinessLog(title = "外部blog系统_blog留言/评论_增加", opType = LogAnnotionOpTypeEnum.ADD)
     public ResponseData add(@RequestBody @Validated(BlogArticleCommentAddDto.add.class) BlogArticleCommentAddDto addDto) {
+        BlogArticle checkArticle = null;
+
+        if (MyConstant.Yes.equals(addDto.getIsReceiveMail()) && StrUtil.isEmpty(addDto.getVisitorEmail())) {
+            throw new BlogException("如果是想接收通知的话,你可能需要填上一个邮箱..");
+        }
+
         if (addDto.getCommentType().equals(MyConstant.CommentType.type_2)) {
             if (addDto.getArticleId() == null) {
                 throw new BlogException("评论类型为日志评论,日志id不能为空");
             }
-            BlogArticle checkArticle = blogArticleService.getById(addDto.getArticleId());
+            checkArticle = blogArticleService.getById(addDto.getArticleId());
             if (checkArticle == null) {
                 throw new BlogException("查询不到相关日志");
             }
@@ -88,6 +105,57 @@
         insert.setIsReceiveCallback(addDto.getIsReceiveMail());
 
         blogArticleCommentService.save(insert);
+
+        if (addDto.getReplyId() == null || addDto.getReplyId() == 0L) {
+            //这里为根节点,预留通知我
+            return new SuccessResponseData();
+        }
+
+        BlogArticleComment lastComment = blogArticleCommentService.getById(addDto.getReplyId());
+
+        if (lastComment == null) {
+            return new SuccessResponseData();
+        }
+
+        //前一个留言接收邮件回复(发送给我的本体可以跳过.如果是给评论者自己的追加可以跳过)
+        if (MyConstant.Yes.equals(lastComment.getIsReceiveCallback())
+                && StrUtil.isNotEmpty(lastComment.getVisitorEmail())
+                && !lastComment.getVisitorNickName().equals(MyConstant.inleft)
+                && !lastComment.getVisitorEmail().equals(MyConstant.email)
+                && !lastComment.getVisitorNickName().equals(addDto.getVisitorNickName())
+                && !lastComment.getVisitorEmail().equals(addDto.getVisitorEmail())) {
+
+            TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
+            Template template = engine.getTemplate("emailReplyModel.ftl");
+
+            String title = "碎碎念";
+            String address;
+            //跳转地址
+            if (addDto.getCommentType().equals(MyConstant.CommentType.type_1)) {
+                //留言板
+                address = String.format(MyConstant.url_1, insert.getId());
+            } else {
+                //文章详情
+                address = String.format(MyConstant.url_2, addDto.getArticleId(), insert.getId());
+                title = checkArticle.getTitle();
+            }
+
+            Dict dict = Dict.create()
+                    .set("lastGuest", lastComment.getVisitorNickName())
+                    .set("commentContent", lastComment.getCommentContent())
+                    .set("address", address)
+                    .set("title", title)
+                    .set("sender", addDto.getVisitorNickName())
+                    .set("sendContent", addDto.getCommentContent());
+            String result = template.render(dict);
+
+            SendMailParam param = new SendMailParam();
+            param.setContent(result);
+            param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
+            param.setTo(lastComment.getVisitorEmail());
+            mailSender.sendMailHtml(param);
+        }
+
 
         return new SuccessResponseData();
     }
@@ -158,4 +226,28 @@
 
         return new SuccessResponseData(replyList);
     }
+
+    //    @GetMapping("/blogComment/test")
+    public ResponseData test() {
+
+
+        TemplateEngine engine = TemplateUtil.createEngine(new TemplateConfig("templates", TemplateConfig.ResourceMode.CLASSPATH));
+        Template template = engine.getTemplate("emailReplyModel.ftl");
+        Dict dict = Dict.create()
+                .set("lastGuest", "回复人")
+                .set("commentContent", "评论内容")
+                .set("address", "http://blog.inleft.com")
+                .set("title", "回复标题")
+                .set("sendTime", DateUtil.now())
+                .set("sender", "发送人")
+                .set("sendContent", "发送内容");
+        String result = template.render(dict);
+
+        SendMailParam param = new SendMailParam();
+        param.setContent(result);
+        param.setTitle("先前您在 [inleft的小木屋] 的留言有了新的回复!\n");
+        param.setTo("1479853828@qq.com");
+        mailSender.sendMailHtml(param);
+        return new SuccessResponseData();
+    }
 }
diff --git a/snowy-main/src/main/resources/application-local.yml b/snowy-main/src/main/resources/application-local.yml
index d9ba22a..28f8c8b 100644
--- a/snowy-main/src/main/resources/application-local.yml
+++ b/snowy-main/src/main/resources/application-local.yml
@@ -9,7 +9,7 @@
 #    username: root
 #    password: root
   redis:
-    host: mpw:74taZBJwkaUlVuljmdqsRg==
+    host: mpw:Ck8aKOzJ5ocPfWENU0oJnA==
     port: 6379
     password: mpw:xMwjz92TmxCDCJYvcfwIZA==
 
diff --git a/snowy-main/src/main/resources/application-prod.yml b/snowy-main/src/main/resources/application-prod.yml
index 58313ea..1a83b11 100644
--- a/snowy-main/src/main/resources/application-prod.yml
+++ b/snowy-main/src/main/resources/application-prod.yml
@@ -8,7 +8,7 @@
 #    username: root
 #    password: root
   redis:
-    host: mpw:74taZBJwkaUlVuljmdqsRg==
+    host: mpw:Ck8aKOzJ5ocPfWENU0oJnA==
     port: 6379
     password: mpw:xMwjz92TmxCDCJYvcfwIZA==
 
diff --git a/snowy-main/src/main/resources/templates/emailReplyModel.ftl b/snowy-main/src/main/resources/templates/emailReplyModel.ftl
new file mode 100644
index 0000000..d770428
--- /dev/null
+++ b/snowy-main/src/main/resources/templates/emailReplyModel.ftl
@@ -0,0 +1,41 @@
+<div
+        style="width: 550px;height: auto;border-radius: 5px;margin:0 auto;border:1px;box-shadow: 0px 0px 20px #888888;position: relative;padding-bottom: 5px;">
+    <div
+            style="background-image: url(http://t.inleft.com/share/media_photo/background.jpg);width:550px;height: 250px;background-size: cover;background-repeat: no-repeat;border-radius: 5px 5px 0px 0px;">
+    </div>
+    <div
+            style="width: 200px;height: 40px;background-color:#c7e0f5;margin-top: -20px;margin-left: 20px;box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.3);color: rgb(255, 255, 255);text-align: center;line-height: 40px;">
+        亲爱的: ${lastGuest}
+    </div>
+    <div
+            style="background-color:white;line-height:180%;padding:0 15px 12px;width:520px;margin:30px auto;color:#555555;font-family:'Century Gothic','Trebuchet MS','Hiragino Sans GB',微软雅黑,'Microsoft Yahei',Tahoma,Helvetica,Arial,'SimSun',sans-serif;font-size:12px;margin-bottom: 0px;">
+        <h2 style="border-bottom:1px solid #DDD;font-size:14px;font-weight:normal;padding:13px 0 10px 8px;">
+            <span style="color: #12ADDB;font-weight: bold;">&gt; </span>叮! 您在<a
+                style="text-decoration:none;color: #12ADDB;" href="${address}"
+                target="_blank" rel="noopener">${title}</a>的留言有了新的回复呐~
+        </h2>
+        <div style="padding:0 12px 0 12px;margin-top:18px">
+            <p>您的留言:</p>
+            <p
+                    style="background:#fafafa repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow:0 2px 5px rgba(0,0,0,.15);margin:20px 0;padding:15px;border-radius:5px;font-size:14px;color:#555">
+            ${commentContent}
+            </p>
+            <p><strong>${sender}</strong>&nbsp;回复:</p>
+            <p
+                    style="background:#fafafa repeating-linear-gradient(-45deg,#fff,#fff 1.125rem,transparent 1.125rem,transparent 2.25rem);box-shadow:0 2px 5px rgba(0,0,0,.15);margin:20px 0;padding:15px;border-radius:5px;font-size:14px;color:#555">
+            ${sendContent}
+            </p>
+        </div>
+    </div>
+    <div
+            style="color:#8c8c8c;;font-family: 'Century Gothic','Trebuchet MS','Hiragino Sans GB',微软雅黑,'Microsoft Yahei',Tahoma,Helvetica,Arial,'SimSun',sans-serif;font-size: 10px;width: 100%;text-align: center;word-wrap:break-word;margin-top: -30px;">
+        <p style="padding:20px;">邮件内容来源 <a href="http://blog.inleft.com/" rel="noopener" target="_blank">inleft</a></p>
+    </div>
+    <a style="text-decoration:none; color:#FFF;width: 40%;text-align: center;background-color:#c7e0f5;height: 40px;line-height: 35px;box-shadow: 3px 3px 3px rgba(0, 0, 0, 0.30);margin: -10px auto;display: block;"
+       href="${address}" target="_blank" rel="noopener">查看原文</a>
+    <div
+            style="color:#8c8c8c;;font-family: 'Century Gothic','Trebuchet MS','Hiragino Sans GB',微软雅黑,'Microsoft Yahei',Tahoma,Helvetica,Arial,'SimSun',sans-serif;font-size: 10px;width: 100%;text-align: center;margin-top: 30px;">
+        <p>本邮件为系统自动发送提醒,感谢您的聆听~</p>
+        <span style="font-size: 10px; color: rgb(153, 153, 153);">在你左边,听风及雨</span>
+    </div>
+</div>

--
Gitblit v1.9.1