Pre Merge pull request !1209 from jinmh/master-jdk17
This commit is contained in:
commit
e5ac569ec6
|
@ -1259,14 +1259,16 @@ CREATE TABLE `system_mail_log` (
|
|||
`id` bigint NOT NULL AUTO_INCREMENT COMMENT '编号',
|
||||
`user_id` bigint NULL DEFAULT NULL COMMENT '用户编号',
|
||||
`user_type` tinyint NULL DEFAULT NULL COMMENT '用户类型',
|
||||
`to_mail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '接收邮箱地址',
|
||||
`to_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '接收邮箱地址',
|
||||
`cc_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '抄送邮箱地址',
|
||||
`bcc_mail` varchar(1024) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '密送邮箱地址',
|
||||
`account_id` bigint NOT NULL COMMENT '邮箱账号编号',
|
||||
`from_mail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '发送邮箱地址',
|
||||
`template_id` bigint NOT NULL COMMENT '模板编号',
|
||||
`template_code` varchar(63) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '模板编码',
|
||||
`template_nickname` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL DEFAULT NULL COMMENT '模版发送人名称',
|
||||
`template_title` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件标题',
|
||||
`template_content` varchar(10240) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件内容',
|
||||
`template_content` text CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件内容',
|
||||
`template_params` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL COMMENT '邮件参数',
|
||||
`send_status` tinyint NOT NULL DEFAULT 0 COMMENT '发送状态',
|
||||
`send_time` datetime NULL DEFAULT NULL COMMENT '发送时间',
|
||||
|
|
|
@ -82,7 +82,7 @@ public class MailTemplateController {
|
|||
@Operation(summary = "发送短信")
|
||||
@PreAuthorize("@ss.hasPermission('system:mail-template:send-mail')")
|
||||
public CommonResult<Long> sendMail(@Valid @RequestBody MailTemplateSendReqVO sendReqVO) {
|
||||
return success(mailSendService.sendSingleMailToAdmin(sendReqVO.getMail(), getLoginUserId(),
|
||||
return success(mailSendService.sendMultipleMailToAdmin(sendReqVO.getToMails(), sendReqVO.getCcMails(), sendReqVO.getBccMails(), getLoginUserId(),
|
||||
sendReqVO.getTemplateCode(), sendReqVO.getTemplateParams()));
|
||||
}
|
||||
|
||||
|
|
|
@ -2,13 +2,10 @@ package cn.iocoder.yudao.module.system.controller.admin.mail.vo.log;
|
|||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.FORMAT_YEAR_MONTH_DAY_HOUR_MINUTE_SECOND;
|
||||
|
||||
@Schema(description = "管理后台 - 邮件日志 Response VO")
|
||||
@Data
|
||||
public class MailLogRespVO {
|
||||
|
@ -22,9 +19,15 @@ public class MailLogRespVO {
|
|||
@Schema(description = "用户类型,参见 UserTypeEnum 枚举", example = "2")
|
||||
private Byte userType;
|
||||
|
||||
@Schema(description = "接收邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "76854@qq.com")
|
||||
@Schema(description = "接收邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user1@example.com, user2@example.com")
|
||||
private String toMail;
|
||||
|
||||
@Schema(description = "抄送邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user3@example.com, user4@example.com")
|
||||
private String ccMail;
|
||||
|
||||
@Schema(description = "密送邮箱地址", requiredMode = Schema.RequiredMode.REQUIRED, example = "user5@example.com, user6@example.com")
|
||||
private String bccMail;
|
||||
|
||||
@Schema(description = "邮箱账号编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "18107")
|
||||
private Long accountId;
|
||||
|
||||
|
|
|
@ -5,15 +5,22 @@ import lombok.Data;
|
|||
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@Schema(description = "管理后台 - 邮件发送 Req VO")
|
||||
@Data
|
||||
public class MailTemplateSendReqVO {
|
||||
|
||||
@Schema(description = "接收邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "7685413@qq.com")
|
||||
@Schema(description = "接收邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user1@example.com, user2@example.com]")
|
||||
@NotEmpty(message = "接收邮箱不能为空")
|
||||
private String mail;
|
||||
private List<String> toMails;
|
||||
|
||||
@Schema(description = "抄送邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user3@example.com, user4@example.com]")
|
||||
private List<String> ccMails;
|
||||
|
||||
@Schema(description = "密送邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "[user5@example.com, user6@example.com]")
|
||||
private List<String> bccMails;
|
||||
|
||||
@Schema(description = "模板编码", requiredMode = Schema.RequiredMode.REQUIRED, example = "test_01")
|
||||
@NotNull(message = "模板编码不能为空")
|
||||
|
|
|
@ -51,7 +51,14 @@ public class MailLogDO extends BaseDO implements Serializable {
|
|||
* 接收邮箱地址
|
||||
*/
|
||||
private String toMail;
|
||||
|
||||
/**
|
||||
* 接收邮箱地址
|
||||
*/
|
||||
private String ccMail;
|
||||
/**
|
||||
* 接收邮箱地址
|
||||
*/
|
||||
private String bccMail;
|
||||
/**
|
||||
* 邮箱账号编号
|
||||
*
|
||||
|
|
|
@ -5,6 +5,8 @@ import lombok.Data;
|
|||
import jakarta.validation.constraints.NotEmpty;
|
||||
import jakarta.validation.constraints.NotNull;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 邮箱发送消息
|
||||
*
|
||||
|
@ -21,8 +23,16 @@ public class MailSendMessage {
|
|||
/**
|
||||
* 接收邮件地址
|
||||
*/
|
||||
@NotNull(message = "接收邮件地址不能为空")
|
||||
private String mail;
|
||||
@NotEmpty(message = "接收邮件地址不能为空")
|
||||
private List<String> toMails;
|
||||
/**
|
||||
* 抄送邮件地址
|
||||
*/
|
||||
private List<String> ccMails;
|
||||
/**
|
||||
* 密送邮件地址
|
||||
*/
|
||||
private List<String> bccMails;
|
||||
/**
|
||||
* 邮件账号编号
|
||||
*/
|
||||
|
|
|
@ -7,6 +7,10 @@ import org.springframework.stereotype.Component;
|
|||
|
||||
import jakarta.annotation.Resource;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
/**
|
||||
* Mail 邮件相关消息的 Producer
|
||||
*
|
||||
|
@ -24,17 +28,36 @@ public class MailProducer {
|
|||
* 发送 {@link MailSendMessage} 消息
|
||||
*
|
||||
* @param sendLogId 发送日志编码
|
||||
* @param mail 接收邮件地址
|
||||
* @param mail 接收邮件地址
|
||||
* @param accountId 邮件账号编号
|
||||
* @param nickname 邮件发件人
|
||||
* @param title 邮件标题
|
||||
* @param content 邮件内容
|
||||
* @param nickname 邮件发件人
|
||||
* @param title 邮件标题
|
||||
* @param content 邮件内容
|
||||
*/
|
||||
public void sendMailSendMessage(Long sendLogId, String mail, Long accountId,
|
||||
String nickname, String title, String content) {
|
||||
sendMailSendMessage(sendLogId, singletonList(mail), null, null, accountId, nickname, title, content);
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送 {@link MailSendMessage} 消息
|
||||
*
|
||||
* @param sendLogId 发送日志编码
|
||||
* @param toMails 接收邮件地址
|
||||
* @param ccMails 抄送邮件地址
|
||||
* @param bccMails 密送邮件地址
|
||||
* @param accountId 邮件账号编号
|
||||
* @param nickname 邮件发件人
|
||||
* @param title 邮件标题
|
||||
* @param content 邮件内容
|
||||
*/
|
||||
public void sendMailSendMessage(Long sendLogId, List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
Long accountId, String nickname, String title, String content) {
|
||||
MailSendMessage message = new MailSendMessage()
|
||||
.setLogId(sendLogId).setMail(mail).setAccountId(accountId)
|
||||
.setNickname(nickname).setTitle(title).setContent(content);
|
||||
.setLogId(sendLogId)
|
||||
.setToMails(toMails).setCcMails(ccMails).setBccMails(bccMails)
|
||||
.setAccountId(accountId).setNickname(nickname)
|
||||
.setTitle(title).setContent(content);
|
||||
applicationContext.publishEvent(message);
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
|||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailLogDO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -49,6 +50,25 @@ public interface MailLogService {
|
|||
MailAccountDO account, MailTemplateDO template ,
|
||||
String templateContent, Map<String, Object> templateParams, Boolean isSend);
|
||||
|
||||
/**
|
||||
* 创建邮件日志
|
||||
*
|
||||
* @param userId 用户编码
|
||||
* @param userType 用户类型
|
||||
* @param toMails 收件人邮件
|
||||
* @param ccMails 收件人邮件
|
||||
* @param bccMails 收件人邮件
|
||||
* @param account 邮件账号信息
|
||||
* @param template 模版信息
|
||||
* @param templateContent 模版内容
|
||||
* @param templateParams 模版参数
|
||||
* @param isSend 是否发送成功
|
||||
* @return 日志编号
|
||||
*/
|
||||
Long createMailLog(Long userId, Integer userType, List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
MailAccountDO account, MailTemplateDO template,
|
||||
String templateContent, Map<String, Object> templateParams, Boolean isSend);
|
||||
|
||||
/**
|
||||
* 更新邮件发送结果
|
||||
*
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.yudao.module.system.service.mail;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.iocoder.yudao.framework.common.pojo.PageResult;
|
||||
import cn.iocoder.yudao.module.system.controller.admin.mail.vo.log.MailLogPageReqVO;
|
||||
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
||||
|
@ -12,6 +13,7 @@ import org.springframework.validation.annotation.Validated;
|
|||
|
||||
import jakarta.annotation.Resource;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
|
@ -61,6 +63,31 @@ public class MailLogServiceImpl implements MailLogService {
|
|||
return logDO.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long createMailLog(Long userId, Integer userType, List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
MailAccountDO account, MailTemplateDO template,
|
||||
String templateContent, Map<String, Object> templateParams, Boolean isSend) {
|
||||
String toMail = CollUtil.isEmpty(toMails) ? "" : String.join(",", toMails);
|
||||
String ccMail = CollUtil.isEmpty(ccMails) ? "" : String.join(",", ccMails);
|
||||
String bccMail = CollUtil.isEmpty(bccMails) ? "" : String.join(",", bccMails);
|
||||
|
||||
MailLogDO.MailLogDOBuilder logDOBuilder = MailLogDO.builder();
|
||||
// 根据是否要发送,设置状态
|
||||
logDOBuilder.sendStatus(Objects.equals(isSend, true) ? MailSendStatusEnum.INIT.getStatus()
|
||||
: MailSendStatusEnum.IGNORE.getStatus())
|
||||
// 用户信息
|
||||
.userId(userId).userType(userType).toMail(toMail).ccMail(ccMail).bccMail(bccMail)
|
||||
.accountId(account.getId()).fromMail(account.getMail())
|
||||
// 模板相关字段
|
||||
.templateId(template.getId()).templateCode(template.getCode()).templateNickname(template.getNickname())
|
||||
.templateTitle(template.getTitle()).templateContent(templateContent).templateParams(templateParams);
|
||||
|
||||
// 插入数据库
|
||||
MailLogDO logDO = logDOBuilder.build();
|
||||
mailLogMapper.insert(logDO);
|
||||
return logDO.getId();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateMailSendResult(Long logId, String messageId, Exception exception) {
|
||||
// 1. 成功
|
||||
|
|
|
@ -2,6 +2,7 @@ package cn.iocoder.yudao.module.system.service.mail;
|
|||
|
||||
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
|
@ -49,6 +50,49 @@ public interface MailSendService {
|
|||
Long sendSingleMail(String mail, Long userId, Integer userType,
|
||||
String templateCode, Map<String, Object> templateParams);
|
||||
|
||||
/**
|
||||
* 发送多条邮件给管理后台的用户
|
||||
*
|
||||
* @param toMails 收件邮箱
|
||||
* @param ccMails 抄送邮箱
|
||||
* @param bccMails 密送邮箱
|
||||
* @param userId 用户编码
|
||||
* @param templateCode 邮件模版编码
|
||||
* @param templateParams 邮件模版参数
|
||||
* @return 发送日志编号
|
||||
*/
|
||||
Long sendMultipleMailToAdmin(List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
Long userId, String templateCode, Map<String, Object> templateParams);
|
||||
|
||||
/**
|
||||
* 发送多条邮件给用户 APP 的用户
|
||||
*
|
||||
* @param toMails 收件邮箱
|
||||
* @param ccMails 抄送邮箱
|
||||
* @param bccMails 密送邮箱
|
||||
* @param userId 用户编码
|
||||
* @param templateCode 邮件模版编码
|
||||
* @param templateParams 邮件模版参数
|
||||
* @return 发送日志编号
|
||||
*/
|
||||
Long sendMultipleMailToMember(List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
Long userId, String templateCode, Map<String, Object> templateParams);
|
||||
|
||||
/**
|
||||
* 发送单条邮件给用户
|
||||
*
|
||||
* @param toMails 收件邮箱
|
||||
* @param ccMails 抄送邮箱
|
||||
* @param bccMails 密送邮箱
|
||||
* @param userId 用户编码
|
||||
* @param userType 用户类型
|
||||
* @param templateCode 邮件模版编码
|
||||
* @param templateParams 邮件模版参数
|
||||
* @return 发送日志编号
|
||||
*/
|
||||
Long sendMultipleMail(List<String> toMails, List<String> ccMails, List<String> bccMails,
|
||||
Long userId, Integer userType, String templateCode, Map<String, Object> templateParams);
|
||||
|
||||
/**
|
||||
* 执行真正的邮件发送
|
||||
* 注意,该方法仅仅提供给 MQ Consumer 使用
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package cn.iocoder.yudao.module.system.service.mail;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.enums.UserTypeEnum;
|
||||
|
@ -17,10 +18,12 @@ import org.dromara.hutool.extra.mail.*;
|
|||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
||||
import static java.util.Collections.singletonList;
|
||||
|
||||
/**
|
||||
* 邮箱发送 Service 实现类
|
||||
|
@ -99,6 +102,70 @@ public class MailSendServiceImpl implements MailSendService {
|
|||
return sendLogId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long sendMultipleMailToAdmin(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, String templateCode, Map<String, Object> templateParams) {
|
||||
// 如果 mail 为空,则加载用户编号对应的邮箱
|
||||
if (CollUtil.isEmpty(toMails)) {
|
||||
AdminUserDO user = adminUserService.getUser(userId);
|
||||
if (user != null) {
|
||||
toMails = singletonList(user.getEmail());
|
||||
}
|
||||
}
|
||||
// 执行发送
|
||||
return sendMultipleMail(toMails, ccMails, bccMails, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long sendMultipleMailToMember(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, String templateCode, Map<String, Object> templateParams) {
|
||||
// 如果 mail 为空,则加载用户编号对应的邮箱
|
||||
if (CollUtil.isEmpty(toMails)) {
|
||||
toMails = singletonList(memberService.getMemberUserEmail(userId));
|
||||
}
|
||||
// 执行发送
|
||||
return sendMultipleMail(toMails, ccMails, bccMails, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Long sendMultipleMail(List<String> toMails, List<String> ccMails, List<String> bccMails, Long userId, Integer userType, String templateCode, Map<String, Object> templateParams) {
|
||||
// 校验邮箱模版是否合法
|
||||
MailTemplateDO template = validateMailTemplate(templateCode);
|
||||
// 校验邮箱账号是否合法
|
||||
MailAccountDO account = validateMailAccount(template.getAccountId());
|
||||
// 校验邮件参数是否缺失
|
||||
validateTemplateParams(template, templateParams);
|
||||
|
||||
if (CollUtil.isEmpty(toMails)) {
|
||||
throw exception(MAIL_SEND_MAIL_NOT_EXISTS);
|
||||
}
|
||||
// 校验邮箱是否存在
|
||||
for (String mail : toMails) {
|
||||
validateMail(mail);
|
||||
}
|
||||
if (CollUtil.isNotEmpty(ccMails)) {
|
||||
for (String mail : ccMails) {
|
||||
validateMail(mail);
|
||||
}
|
||||
}
|
||||
if (CollUtil.isNotEmpty(bccMails)) {
|
||||
for (String mail : bccMails) {
|
||||
validateMail(mail);
|
||||
}
|
||||
}
|
||||
|
||||
// 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
|
||||
Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus());
|
||||
String title = mailTemplateService.formatMailTemplateContent(template.getTitle(), templateParams);
|
||||
String content = mailTemplateService.formatMailTemplateContent(template.getContent(), templateParams);
|
||||
Long sendLogId = mailLogService.createMailLog(userId, userType, toMails, ccMails, bccMails,
|
||||
account, template, content, templateParams, isSend);
|
||||
// 发送 MQ 消息,异步执行发送短信
|
||||
if (isSend) {
|
||||
mailProducer.sendMailSendMessage(sendLogId, toMails, ccMails, bccMails, account.getId(),
|
||||
template.getNickname(), title, content);
|
||||
}
|
||||
return sendLogId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doSendMail(MailSendMessage message) {
|
||||
// 1. 创建发送账号
|
||||
|
@ -106,7 +173,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|||
MailAccount mailAccount = buildMailAccount(account, message.getNickname());
|
||||
// 2. 发送邮件
|
||||
try {
|
||||
String messageId = MailUtil.send(mailAccount, message.getMail(),
|
||||
String messageId = MailUtil.send(mailAccount, message.getToMails(), message.getCcMails(), message.getBccMails(),
|
||||
message.getTitle(), message.getContent(), true);
|
||||
// 3. 更新结果(成功)
|
||||
mailLogService.updateMailSendResult(message.getLogId(), messageId, null);
|
||||
|
@ -155,7 +222,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|||
}
|
||||
|
||||
/**
|
||||
* 校验邮件参数是否确实
|
||||
* 校验邮件参数是否缺失
|
||||
*
|
||||
* @param template 邮箱模板
|
||||
* @param templateParams 参数列表
|
||||
|
|
Loading…
Reference in New Issue