|
@@ -2,17 +2,18 @@ package cn.iocoder.yudao.module.system.service.mail.impl;
|
|
|
|
|
|
import cn.hutool.extra.mail.MailAccount;
|
|
import cn.hutool.extra.mail.MailAccount;
|
|
import cn.hutool.extra.mail.MailUtil;
|
|
import cn.hutool.extra.mail.MailUtil;
|
|
|
|
+import cn.iocoder.yudao.framework.common.core.KeyValue;
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
|
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
|
|
import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
|
import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO;
|
|
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
|
|
import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper;
|
|
-import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper;
|
|
|
|
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
|
|
import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage;
|
|
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
|
import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailLogService;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailLogService;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailSendService;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailSendService;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
|
|
import cn.iocoder.yudao.module.system.service.mail.MailTemplateService;
|
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.validation.annotation.Validated;
|
|
import org.springframework.validation.annotation.Validated;
|
|
@@ -20,6 +21,7 @@ import org.springframework.validation.annotation.Validated;
|
|
import javax.annotation.Resource;
|
|
import javax.annotation.Resource;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
@@ -34,9 +36,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
@Validated
|
|
@Validated
|
|
@Slf4j
|
|
@Slf4j
|
|
public class MailSendServiceImpl implements MailSendService {
|
|
public class MailSendServiceImpl implements MailSendService {
|
|
-
|
|
|
|
- @Resource
|
|
|
|
- private MailTemplateMapper mailTemplateMapper;
|
|
|
|
|
|
+
|
|
@Resource
|
|
@Resource
|
|
private MailAccountMapper mailAccountMapper;
|
|
private MailAccountMapper mailAccountMapper;
|
|
@Resource
|
|
@Resource
|
|
@@ -48,7 +48,7 @@ public class MailSendServiceImpl implements MailSendService {
|
|
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
- public void sendMail(String templateCode, String from , String content , List<String> tos , String title) {
|
|
|
|
|
|
+ public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams) {
|
|
// TODO @@wangjingyi:发送的时候,参考下短信;DONE
|
|
// TODO @@wangjingyi:发送的时候,参考下短信;DONE
|
|
//校验邮箱模版是否合法
|
|
//校验邮箱模版是否合法
|
|
MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode);
|
|
MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode);
|
|
@@ -58,14 +58,11 @@ public class MailSendServiceImpl implements MailSendService {
|
|
MailAccountDO mailAccountDO = this.checkMailAccountValid(from);
|
|
MailAccountDO mailAccountDO = this.checkMailAccountValid(from);
|
|
Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
|
|
Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content);
|
|
content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params);
|
|
content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params);
|
|
- Long sendLogId = mailLogService.createMailLog(mailAccountDO , mailTemplateDO , from , content , tos , title , isSend);
|
|
|
|
-
|
|
|
|
- // 后续功能 TODO :附件查询
|
|
|
|
- //List<String> fileIds = mailSendVO.getFileIds();
|
|
|
|
-
|
|
|
|
|
|
+ Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content, templateParams, isSend);
|
|
|
|
+ List<KeyValue<String,Object>> newTemplateParams = buildTemplateParams(mailTemplateDO,templateParams);
|
|
// 发送 MQ 消息,异步执行发送短信
|
|
// 发送 MQ 消息,异步执行发送短信
|
|
if (isSend) {
|
|
if (isSend) {
|
|
- mailProducer.sendMailSendMessage(mailAccountDO , mailTemplateDO ,content , tos , title , sendLogId);
|
|
|
|
|
|
+ mailProducer.sendMailSendMessage(sendLogId,mailAccountDO , mailTemplateDO ,content, newTemplateParams,to);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -84,10 +81,10 @@ public class MailSendServiceImpl implements MailSendService {
|
|
MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message);
|
|
MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message);
|
|
//发送邮件
|
|
//发送邮件
|
|
try{
|
|
try{
|
|
- String messageId = MailUtil.send(account,message.getTos(),message.getTitle(),message.getContent(),false,null);
|
|
|
|
|
|
+ String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null);
|
|
mailLogService.updateMailSendResult(message.getLogId() , messageId);
|
|
mailLogService.updateMailSendResult(message.getLogId() , messageId);
|
|
}catch (Exception e){
|
|
}catch (Exception e){
|
|
- mailLogService.updateMailSendResult(message.getLogId() , e.getMessage());
|
|
|
|
|
|
+ mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
@@ -99,16 +96,21 @@ public class MailSendServiceImpl implements MailSendService {
|
|
}
|
|
}
|
|
return mailTemplateDO;
|
|
return mailTemplateDO;
|
|
}
|
|
}
|
|
-
|
|
|
|
- private void validateMailTemplateExists(Long id) {
|
|
|
|
- if (mailTemplateMapper.selectById(id) == null) {
|
|
|
|
- throw exception(MAIL_TEMPLATE_NOT_EXISTS);
|
|
|
|
- }
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- private void validateMailTemplateOnlyByCode(String code){
|
|
|
|
- if (mailTemplateMapper.selectOneByCode(code) != null) {
|
|
|
|
- throw exception(MAIL_TEMPLATE_EXISTS);
|
|
|
|
- }
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 将参数模板,处理成有序的 KeyValue 数组
|
|
|
|
+ *
|
|
|
|
+ * @param template 邮箱模板
|
|
|
|
+ * @param templateParams 原始参数
|
|
|
|
+ * @return 处理后的参数
|
|
|
|
+ */
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ public List<KeyValue<String, Object>> buildTemplateParams(MailTemplateDO template, Map<String, Object> templateParams) {
|
|
|
|
+ return template.getParams().stream().map(key -> {
|
|
|
|
+ Object value = templateParams.get(key);
|
|
|
|
+ if (value == null) {
|
|
|
|
+ throw exception(MAIL_SEND_TEMPLATE_PARAM_MISS, key);
|
|
|
|
+ }
|
|
|
|
+ return new KeyValue<>(key, value);
|
|
|
|
+ }).collect(Collectors.toList());
|
|
}
|
|
}
|
|
}
|
|
}
|