refactor: 邮件支持直接发送内容

This commit is contained in:
caiyuchao
2025-09-20 17:15:51 +08:00
parent 075fb6a92a
commit eb882e08e9
4 changed files with 18 additions and 15 deletions

View File

@@ -27,4 +27,7 @@ public class MailSendSingleToUserReqDTO {
@Schema(description = "邮件模板参数列表") @Schema(description = "邮件模板参数列表")
private List<Map<String, Object>> templateParamsList; private List<Map<String, Object>> templateParamsList;
@Schema(description = "邮件内容")
private String content;
} }

View File

@@ -26,7 +26,7 @@ public class MailSendApiImpl implements MailSendApi {
@Override @Override
public CommonResult<Long> sendSingleMailToAdmins(MailSendSingleToUserReqDTO reqDTO) { public CommonResult<Long> sendSingleMailToAdmins(MailSendSingleToUserReqDTO reqDTO) {
return success(mailSendService.sendSingleMailToAdmins(reqDTO.getMail(), reqDTO.getUserId(), return success(mailSendService.sendSingleMailToAdmins(reqDTO.getMail(), reqDTO.getUserId(),
reqDTO.getTemplateCode(), reqDTO.getTemplateParamsList())); reqDTO.getTemplateCode(), reqDTO.getContent()));
} }
@Override @Override

View File

@@ -2,7 +2,6 @@ package org.agt.module.system.service.mail;
import org.agt.module.system.mq.message.mail.MailSendMessage; import org.agt.module.system.mq.message.mail.MailSendMessage;
import java.util.List;
import java.util.Map; import java.util.Map;
/** /**
@@ -26,7 +25,7 @@ public interface MailSendService {
String templateCode, Map<String, Object> templateParams); String templateCode, Map<String, Object> templateParams);
Long sendSingleMailToAdmins(String mail, Long userId, Long sendSingleMailToAdmins(String mail, Long userId,
String templateCode, List<Map<String, Object>> templateParamsList); String templateCode, String content);
/** /**
* 发送单条邮件给用户 APP 的用户 * 发送单条邮件给用户 APP 的用户
@@ -54,7 +53,7 @@ public interface MailSendService {
String templateCode, Map<String, Object> templateParams); String templateCode, Map<String, Object> templateParams);
Long sendSingleMails(String mail, Long userId, Integer userType, Long sendSingleMails(String mail, Long userId, Integer userType,
String templateCode, List<Map<String, Object>> templateParamsList); String templateCode, String content);
/** /**
* 执行真正的邮件发送 * 执行真正的邮件发送

View File

@@ -20,7 +20,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import java.util.List; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception; import static org.agt.framework.common.exception.util.ServiceExceptionUtil.exception;
@@ -78,7 +78,7 @@ public class MailSendServiceImpl implements MailSendService {
@Override @Override
public Long sendSingleMailToAdmins(String mail, Long userId, public Long sendSingleMailToAdmins(String mail, Long userId,
String templateCode, List<Map<String, Object>> templateParamsList) { String templateCode, String content) {
// 如果 mail 为空,则加载用户编号对应的邮箱 // 如果 mail 为空,则加载用户编号对应的邮箱
if (StrUtil.isEmpty(mail)) { if (StrUtil.isEmpty(mail)) {
AdminUserDO user = adminUserService.getUser(userId); AdminUserDO user = adminUserService.getUser(userId);
@@ -87,7 +87,7 @@ public class MailSendServiceImpl implements MailSendService {
} }
} }
// 执行发送 // 执行发送
return sendSingleMails(mail, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParamsList); return sendSingleMails(mail, userId, UserTypeEnum.ADMIN.getValue(), templateCode, content);
} }
@Override @Override
@@ -129,7 +129,7 @@ public class MailSendServiceImpl implements MailSendService {
@Override @Override
public Long sendSingleMails(String mail, Long userId, Integer userType, public Long sendSingleMails(String mail, Long userId, Integer userType,
String templateCode, List<Map<String, Object>> templateParamsList) { String templateCode, String content) {
// 校验邮箱模版是否合法 // 校验邮箱模版是否合法
MailTemplateDO template = validateMailTemplate(templateCode); MailTemplateDO template = validateMailTemplate(templateCode);
// 校验邮箱账号是否合法 // 校验邮箱账号是否合法
@@ -137,17 +137,18 @@ public class MailSendServiceImpl implements MailSendService {
// 校验邮箱是否存在 // 校验邮箱是否存在
mail = validateMail(mail); mail = validateMail(mail);
String content = ""; // String content = "";
for (Map<String, Object> templateParams : templateParamsList) { // for (Map<String, Object> templateParams : templateParamsList) {
validateTemplateParams(template, templateParams); // validateTemplateParams(template, templateParams);
content += mailTemplateService.formatMailTemplateContent(template.getContent(), templateParams); // content += mailTemplateService.formatMailTemplateContent(template.getContent(), templateParams);
} // }
Map<String, Object> templateParams = new HashMap<>();
// 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志
Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()); Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus());
String title = mailTemplateService.formatMailTemplateContent(template.getTitle(), templateParamsList.get(0)); String title = mailTemplateService.formatMailTemplateContent(template.getTitle(), templateParams);
Long sendLogId = mailLogService.createMailLog(userId, userType, mail, Long sendLogId = mailLogService.createMailLog(userId, userType, mail,
account, template, content, templateParamsList.get(0), isSend); account, template, content, templateParams, isSend);
// 发送 MQ 消息,异步执行发送短信 // 发送 MQ 消息,异步执行发送短信
if (isSend && mailEnable) { if (isSend && mailEnable) {
mailProducer.sendMailSendMessage(sendLogId, mail, account.getId(), mailProducer.sendMailSendMessage(sendLogId, mail, account.getId(),