Browse Source

邮件模块:修改发送规则

wangPH 2 years ago
parent
commit
a875d058d9

+ 12 - 0
pdf-office-common/pom.xml

@@ -51,6 +51,12 @@
         <dependency>
             <groupId>com.github.pagehelper</groupId>
             <artifactId>pagehelper</artifactId>
+            <exclusions>
+                <exclusion>
+                    <groupId>com.github.jsqlparser</groupId>
+                    <artifactId>jsqlparser</artifactId>
+                </exclusion>
+            </exclusions>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
@@ -151,6 +157,12 @@
             <artifactId>mybatis-plus-boot-starter</artifactId>
             <version>${mybatis.plus.version}</version>
         </dependency>
+        <dependency>
+            <groupId>com.baomidou</groupId>
+            <artifactId>mybatis-plus-annotation</artifactId>
+            <version>${mybatis.plus.version}</version>
+            <scope>compile</scope>
+        </dependency>
         <!--lombok 表达式-->
         <dependency>
             <groupId>org.projectlombok</groupId>

+ 20 - 0
pdf-office-common/src/main/java/cn/kdan/cloud/pdf/office/common/annotation/EnableMybatisPlusConfig.java

@@ -0,0 +1,20 @@
+package cn.kdan.cloud.pdf.office.common.annotation;
+
+import cn.kdan.cloud.pdf.office.common.config.mybatis.CustomLocalDateTimeTypeHandler;
+import cn.kdan.cloud.pdf.office.common.config.mybatis.MybatisPlusConfig;
+import cn.kdan.cloud.pdf.office.common.config.mybatis.MybatisPlusMetaObjectHandler;
+import org.springframework.context.annotation.Import;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * @author ComPDFKit-WPH 2023/1/12
+ */
+@Target(value = ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+@Import({CustomLocalDateTimeTypeHandler.class, MybatisPlusConfig.class, MybatisPlusMetaObjectHandler.class})
+public @interface EnableMybatisPlusConfig {
+}

+ 0 - 1
pdf-office-common/src/main/java/cn/kdan/cloud/pdf/office/common/config/mybatis/MybatisPlusConfig.java

@@ -19,5 +19,4 @@ public class MybatisPlusConfig {
         interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
         return interceptor;
     }
-
 }

+ 6 - 0
pdf-office-common/src/main/java/cn/kdan/cloud/pdf/office/common/pojo/ResultMap.java

@@ -30,6 +30,8 @@ public class ResultMap<T> {
         this.result = result;
     }
 
+
+
     public int getCode() {
         return code;
     }
@@ -86,6 +88,10 @@ public class ResultMap<T> {
         return this.code == CommonConstant.SUCCESS;
     }
 
+    public static <T> ResultMap<T> success() {
+        return success(null);
+    }
+
     public static <T> ResultMap<T> success(T result) {
         return success(CommonConstant.RESULT_SUCCESS, result);
     }

+ 4 - 4
pdf-office-email/pom.xml

@@ -76,10 +76,10 @@
             <scope>compile</scope>
         </dependency>
 
-<!--        <dependency>-->
-<!--            <groupId>com.alibaba</groupId>-->
-<!--            <artifactId>druid-spring-boot-starter</artifactId>-->
-<!--        </dependency>-->
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+        </dependency>
 
         <dependency>
             <groupId>com.alibaba</groupId>

+ 2 - 0
pdf-office-email/src/main/java/cn/kdan/cloud/pdf/office/email/PDFOfficeEmailApplication.java

@@ -1,6 +1,7 @@
 package cn.kdan.cloud.pdf.office.email;
 
 import cn.kdan.cloud.pdf.office.common.annotation.EnableMyRabbitMqHandler;
+import cn.kdan.cloud.pdf.office.common.annotation.EnableMybatisPlusConfig;
 import org.mybatis.spring.annotation.MapperScan;
 import org.springframework.amqp.rabbit.annotation.EnableRabbit;
 import org.springframework.boot.SpringApplication;
@@ -18,6 +19,7 @@ import org.springframework.cloud.netflix.hystrix.EnableHystrix;
 @EnableHystrix
 @EnableConfigurationProperties
 @EnableMyRabbitMqHandler
+@EnableMybatisPlusConfig
 @MapperScan("cn.kdan.cloud.pdf.office.email.mapper")
 public class PDFOfficeEmailApplication {
 

+ 29 - 0
pdf-office-email/src/main/java/cn/kdan/cloud/pdf/office/email/controller/sendEmailController.java

@@ -0,0 +1,29 @@
+package cn.kdan.cloud.pdf.office.email.controller;
+
+import cn.kdan.cloud.pdf.office.api.email.bo.EmailSendBO;
+import cn.kdan.cloud.pdf.office.common.pojo.ResultMap;
+import cn.kdan.cloud.pdf.office.email.service.SendEmailHandlerService;
+import lombok.RequiredArgsConstructor;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+/**
+ * @author ComPDFKit-WPH 2023/1/12
+ */
+@RestController
+@RequiredArgsConstructor
+@RequestMapping("/mail")
+public class sendEmailController {
+
+    private final SendEmailHandlerService emailHandlerService;
+
+    @PostMapping("send")
+    public ResultMap<Void> sendEmail(@RequestBody EmailSendBO emailSendBO){
+        emailHandlerService.sendEmail(emailSendBO);
+        return ResultMap.success();
+    }
+
+
+}

+ 45 - 0
pdf-office-email/src/main/java/cn/kdan/cloud/pdf/office/email/rabbit/listener/SendEmailListener.java

@@ -1,15 +1,29 @@
 package cn.kdan.cloud.pdf.office.email.rabbit.listener;
 
 import cn.kdan.cloud.pdf.office.api.email.bo.EmailSendBO;
+import cn.kdan.cloud.pdf.office.api.email.bo.TemplateHtmlBO;
 import cn.kdan.cloud.pdf.office.common.constant.RabbitMqConstant;
+import cn.kdan.cloud.pdf.office.common.utils.JsonUtils;
+import cn.kdan.cloud.pdf.office.email.config.MailSenderConfig;
+import cn.kdan.cloud.pdf.office.email.entity.EmailLog;
+import cn.kdan.cloud.pdf.office.email.service.EmailLogService;
+import cn.kdan.cloud.pdf.office.email.service.EmailTemplateService;
 import cn.kdan.cloud.pdf.office.email.service.SendEmailHandlerService;
+import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import com.rabbitmq.client.Channel;
 import lombok.RequiredArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.amqp.core.Message;
 import org.springframework.amqp.rabbit.annotation.RabbitListener;
+import org.springframework.mail.javamail.JavaMailSenderImpl;
+import org.springframework.mail.javamail.MimeMessageHelper;
 import org.springframework.stereotype.Component;
 
+import javax.mail.internet.MimeMessage;
+import java.util.Date;
+import java.util.Map;
+import java.util.Objects;
+
 /**
  * @author ComPDFKit-WPH 2023/1/11
  */
@@ -20,6 +34,10 @@ public class SendEmailListener {
 
     private final SendEmailHandlerService sendEmailService;
 
+    private final MailSenderConfig mailSenderConfig;
+    private final EmailLogService emailLogService;
+    private final EmailTemplateService emailTemplateService;
+
     /**
      * 发送邮件 监听
      *
@@ -31,7 +49,34 @@ public class SendEmailListener {
     public void backgroundApiAddQueue(Message message, Channel channel, EmailSendBO emailSendBO) {
         try {
             log.info("邮件发送监听内容:{}", emailSendBO);
+            TemplateHtmlBO template = emailTemplateService.getTemplate(emailSendBO.getTemplateId());
+
+            JavaMailSenderImpl mailSender = mailSenderConfig.getDefaultSender();
+            Map<String, String> sendContent = emailSendBO.getSendContent();
+            String templateHtml = template.getTemplateHtml();
+            for (String key : sendContent.keySet()) {
+                templateHtml = templateHtml.replaceAll(key,sendContent.get(key));
+            }
+
+            MimeMessage mimeMessage = mailSender.createMimeMessage();
+            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
+            helper.setSubject(emailSendBO.getEmailTitle());
+            helper.setText(templateHtml, true);
+            helper.setTo(emailSendBO.getToEmail());
+            helper.setFrom(ObjectUtils.isEmpty(emailSendBO.getFromEmail())
+                    ? Objects.requireNonNull(mailSender.getUsername())
+                    : emailSendBO.getFromEmail());
+            mailSender.send(mimeMessage);
+
+            EmailLog emailLog = new EmailLog();
+            emailLog.setSendTo(emailSendBO.getToEmail());
+            emailLog.setSendFrom(emailSendBO.getFromEmail());
+            emailLog.setSendTime(new Date());
+            emailLog.setSendTemplate(emailSendBO.getTemplateId());
+            emailLog.setSendContent(JsonUtils.getJsonString(emailSendBO.getSendContent()));
+            emailLogService.getBaseMapper().insert(emailLog);
             sendEmailService.sendEmail(emailSendBO);
+
             channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
         } catch (Exception e) {
             // TODO 补救策略

+ 6 - 30
pdf-office-email/src/main/java/cn/kdan/cloud/pdf/office/email/service/impl/SendEmailHandlerServiceImpl.java

@@ -2,6 +2,7 @@ package cn.kdan.cloud.pdf.office.email.service.impl;
 
 import cn.kdan.cloud.pdf.office.api.email.bo.EmailSendBO;
 import cn.kdan.cloud.pdf.office.api.email.bo.TemplateHtmlBO;
+import cn.kdan.cloud.pdf.office.common.constant.RabbitMqConstant;
 import cn.kdan.cloud.pdf.office.common.utils.JsonUtils;
 import cn.kdan.cloud.pdf.office.email.config.MailSenderConfig;
 import cn.kdan.cloud.pdf.office.email.entity.EmailLog;
@@ -12,6 +13,7 @@ import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
 import lombok.RequiredArgsConstructor;
 import lombok.SneakyThrows;
 import lombok.extern.slf4j.Slf4j;
+import org.springframework.amqp.rabbit.core.RabbitTemplate;
 import org.springframework.mail.SimpleMailMessage;
 import org.springframework.mail.javamail.JavaMailSenderImpl;
 import org.springframework.mail.javamail.MimeMessageHelper;
@@ -32,39 +34,13 @@ import java.util.Objects;
 @RequiredArgsConstructor
 public class SendEmailHandlerServiceImpl implements SendEmailHandlerService {
 
-    private final MailSenderConfig mailSenderConfig;
-    private final EmailLogService emailLogService;
-    private final EmailTemplateService emailTemplateService;
+    private final RabbitTemplate rabbitTemplate;
 
-    @SneakyThrows
     @Override
     public void sendEmail(EmailSendBO emailSendBO){
-        TemplateHtmlBO template = emailTemplateService.getTemplate(emailSendBO.getTemplateId());
-
-        JavaMailSenderImpl mailSender = mailSenderConfig.getDefaultSender();
-        Map<String, String> sendContent = emailSendBO.getSendContent();
-        String templateHtml = template.getTemplateHtml();
-        for (String key : sendContent.keySet()) {
-            templateHtml = templateHtml.replaceAll(key,sendContent.get(key));
-        }
-
-        MimeMessage message = mailSender.createMimeMessage();
-        MimeMessageHelper helper = new MimeMessageHelper(message, true);
-        helper.setSubject(emailSendBO.getEmailTitle());
-        helper.setText(templateHtml, true);
-        helper.setTo(emailSendBO.getToEmail());
-        helper.setFrom(ObjectUtils.isEmpty(emailSendBO.getFromEmail())
-                ? Objects.requireNonNull(mailSender.getUsername())
-                : emailSendBO.getFromEmail());
-        mailSender.send(message);
-
-        EmailLog emailLog = new EmailLog();
-        emailLog.setSendTo(emailSendBO.getToEmail());
-        emailLog.setSendFrom(emailSendBO.getFromEmail());
-        emailLog.setSendTime(new Date());
-        emailLog.setSendTemplate(emailSendBO.getTemplateId());
-        emailLog.setSendContent(JsonUtils.getJsonString(emailSendBO.getSendContent()));
-        emailLogService.getBaseMapper().insert(emailLog);
+        rabbitTemplate.convertAndSend(RabbitMqConstant.EMAIL_EXCHANGE,
+                RabbitMqConstant.EMAIL_SEND_ROUTING_KEY,
+                emailSendBO);
     }
 
 

+ 1 - 1
pdf-office-email/src/main/resources/bootstrap.properties

@@ -1,5 +1,5 @@
 spring.profiles.active=@spring.profiles.active@
-server.servlet.context-path=/pdf-office-email
+server.servlet.context-path=/
 spring.application.name=pdf-office-email
 spring.cloud.nacos.config.server-addr=http://106.55.99.175:8848
 spring.cloud.nacos.config.username=nacos

+ 108 - 0
pdf-office-payment/pom.xml

@@ -17,4 +17,112 @@
         <maven.compiler.target>8</maven.compiler.target>
     </properties>
 
+    <dependencies>
+        <dependency>
+            <groupId>cn.kdan.pdf.office</groupId>
+            <artifactId>pdf-office-common</artifactId>
+            <version>0.0.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-test</artifactId>
+            <scope>test</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-actuator</artifactId>
+            <scope>compile</scope>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba.cloud</groupId>
+            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-openfeign</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.cloud</groupId>
+            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid-spring-boot-starter</artifactId>
+        </dependency>
+
+        <dependency>
+            <groupId>com.alibaba</groupId>
+            <artifactId>druid</artifactId>
+        </dependency>
+
+    </dependencies>
+
+
+    <build>
+        <finalName>pdf-office-payment</finalName>
+
+        <resources>
+            <resource>
+                <directory>src/main/resources</directory>
+                <includes>
+                    <include>**/*</include>
+                </includes>
+                <filtering>true</filtering>
+            </resource>
+        </resources>
+
+
+        <plugins>
+            <plugin>
+                <groupId>org.springframework.boot</groupId>
+                <artifactId>spring-boot-maven-plugin</artifactId>
+            </plugin>
+            <plugin>
+                <groupId>org.apache.maven.plugins</groupId>
+                <artifactId>maven-surefire-plugin</artifactId>
+                <version>2.22.2</version>
+                <configuration>
+                    <skipTests>true</skipTests>
+                </configuration>
+            </plugin>
+
+            <plugin>
+                <groupId>com.spotify</groupId>
+                <artifactId>docker-maven-plugin</artifactId>
+                <version>${docker.version}</version>
+                <configuration>
+                    <!-- Docker 远程管理地址-->
+                    <dockerHost>http://${docker.host}</dockerHost>
+                    <!--镜像名称-->
+                    <imageName>${project.artifactId}</imageName>
+                    <!--Dockerfile-->
+                    <dockerDirectory>${project.basedir}/docker</dockerDirectory>
+                    <!--插件会将需要的资源拷贝到docker目录下,供Dockerfile里构建镜像使用-->
+                    <resources>
+                        <resource>
+                            <targetPath>/</targetPath>
+                            <directory>${project.build.directory}</directory>
+                            <include>${project.build.finalName}.jar</include>
+                        </resource>
+                    </resources>
+                </configuration>
+            </plugin>
+        </plugins>
+    </build>
+
 </project>

+ 28 - 0
pdf-office-payment/src/main/java/cn/kdan/cloud/pdf/office/payment/PDFOfficePaymentApplication.java

@@ -0,0 +1,28 @@
+package cn.kdan.cloud.pdf.office.payment;
+
+import cn.kdan.cloud.pdf.office.common.annotation.EnableMyRabbitMqHandler;
+import cn.kdan.cloud.pdf.office.common.annotation.EnableMybatisPlusConfig;
+import org.mybatis.spring.annotation.MapperScan;
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
+import org.springframework.cloud.netflix.hystrix.EnableHystrix;
+
+/**
+ * @author ComPDFKit-WPH 2023/1/12
+ */
+
+@SpringBootApplication
+@EnableDiscoveryClient
+@EnableHystrix
+@EnableConfigurationProperties
+@EnableMyRabbitMqHandler
+@EnableMybatisPlusConfig
+@MapperScan("cn.kdan.cloud.pdf.office.payment.mapper")
+public class PDFOfficePaymentApplication {
+
+    public static void main(String[] args) {
+        SpringApplication.run(PDFOfficePaymentApplication.class, args);
+    }
+}

+ 12 - 0
pdf-office-payment/src/main/resources/bootstrap.properties

@@ -0,0 +1,12 @@
+spring.profiles.active=@spring.profiles.active@
+server.servlet.context-path=/pdf-office-email
+spring.application.name=pdf-office-email
+spring.cloud.nacos.config.server-addr=http://106.55.99.175:8848
+spring.cloud.nacos.config.username=nacos
+spring.cloud.nacos.config.password=Nacos@123!
+spring.cloud.nacos.config.file-extension=yml
+spring.cloud.nacos.config.refresh-enabled=true
+
+spring.cloud.nacos.discovery.server-addr=http://localhost:8848
+spring.cloud.nacos.discovery.username=nacos
+spring.cloud.nacos.discovery.password=nacos

+ 73 - 0
pdf-office-payment/src/main/resources/logback-spring.xml

@@ -0,0 +1,73 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<configuration debug="false">
+    <!-- 引入spirng boot默认的logback配置文件 -->
+    <include resource="org/springframework/boot/logging/logback/defaults.xml"/>
+    <springProperty scope="context" name="logPath" source="log.path" />
+    <springProperty scope="context" name="logLevel" source="log.level" />
+    <!-- 控制台输出 -->
+    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+        <encoder>
+            <pattern>${CONSOLE_LOG_PATTERN}</pattern>
+        </encoder>
+    </appender>
+
+    <!-- 按照每天生成日志文件 info输出 -->
+    <appender name="FileInfoLog" class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <!--只输出INFO-->
+        <filter class="ch.qos.logback.classic.filter.LevelFilter">
+            <!--过滤 INFO-->
+            <level>INFO</level>
+            <!--匹配到就禁止-->
+            <onMatch>ACCEPT</onMatch>
+            <!--没有匹配到就允许-->
+            <onMismatch>DENY</onMismatch>
+        </filter>
+        <!--滚动策略-->
+        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
+            <!--日志文件输出的文件名-->
+            <FileNamePattern>${logPath}/account-provider/account-provider_%d{yyyy-MM-dd}.%i.log</FileNamePattern>
+            <!--日志文件保留天数-->
+            <MaxHistory>30</MaxHistory>
+            <!--日志文件最大的大小-->
+            <MaxFileSize>300MB</MaxFileSize>
+        </rollingPolicy>
+        <!--格式化输出-->
+        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset>
+        </encoder>
+    </appender>
+
+    <!--warn文件输出-->
+    <!-- 按照每天和固定大小(10MB)生成日志文件【最新的日志,是日期最大数字最大的】 -->
+    <appender name="FileWarnLog"  class="ch.qos.logback.core.rolling.RollingFileAppender">
+        <filter class="ch.qos.logback.classic.filter.ThresholdFilter">
+            <!--设置日志级别,过滤掉info日志,只输入warn日志-->
+            <level>WARN</level>
+        </filter>
+        <!--滚动策略-->
+        <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
+            <!--日志文件输出的文件名-->
+            <FileNamePattern>${logPath}/account-provider/account-provider_warn_%d{yyyy-MM-dd}.%i.log</FileNamePattern>
+            <!--日志文件保留天数-->
+            <MaxHistory>30</MaxHistory>
+            <!--日志文件最大的大小-->
+            <MaxFileSize>300MB</MaxFileSize>
+        </rollingPolicy>
+        <!--格式化输出-->
+        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
+            <!--格式化输出:%d表示日期,%thread表示线程名,%-5level:级别从左显示5个字符宽度%msg:日志消息,%n是换行符-->
+            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
+            <charset>UTF-8</charset>
+        </encoder>
+    </appender>
+
+
+    <!-- 日志输出级别 -->
+    <root level="${logLevel}">
+        <appender-ref ref="STDOUT" />
+        <appender-ref ref="FileWarnLog" />
+        <appender-ref ref="FileInfoLog" />
+    </root>
+</configuration>

+ 7 - 0
pom.xml

@@ -303,6 +303,13 @@
                 <version>1.46</version>
             </dependency>
 
+            <dependency>
+                <groupId>com.baomidou</groupId>
+                <artifactId>mybatis-plus-annotation</artifactId>
+                <version>${mybatis.plus.version}</version>
+                <scope>compile</scope>
+            </dependency>
+
             <!--SpringCloud-->
             <dependency>
                 <groupId>org.springframework.cloud</groupId>