Commit afced9d420626919b9cf8e165a073abad9713b65
1 parent
ea39dcc5
邮箱模块:完善发送 send 逻辑
Showing
12 changed files
with
188 additions
and
79 deletions
yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ErrorCodeConstants.java
| @@ -145,9 +145,12 @@ public interface ErrorCodeConstants { | @@ -145,9 +145,12 @@ public interface ErrorCodeConstants { | ||
| 145 | ErrorCode MAIL_ACCOUNT_NOT_EXISTS = new ErrorCode(1002023000, "邮箱账号不存在"); | 145 | ErrorCode MAIL_ACCOUNT_NOT_EXISTS = new ErrorCode(1002023000, "邮箱账号不存在"); |
| 146 | ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002023001, "无法删除,该邮箱账号还有邮件模板"); | 146 | ErrorCode MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS = new ErrorCode(1002023001, "无法删除,该邮箱账号还有邮件模板"); |
| 147 | 147 | ||
| 148 | - // ========== 邮箱模版 1002024000 ========== | 148 | + // ========== 邮件模版 1002024000 ========== |
| 149 | ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002024000, "邮件模版不存在"); | 149 | ErrorCode MAIL_TEMPLATE_NOT_EXISTS = new ErrorCode(1002024000, "邮件模版不存在"); |
| 150 | ErrorCode MAIL_TEMPLATE_CODE_EXISTS = new ErrorCode(1002024001, "邮件模版 code({}) 已存在"); | 150 | ErrorCode MAIL_TEMPLATE_CODE_EXISTS = new ErrorCode(1002024001, "邮件模版 code({}) 已存在"); |
| 151 | - ErrorCode MAIL_SEND_TEMPLATE_PARAM_MISS = new ErrorCode(1002021003, "模板参数({})缺失"); // TODO 优化 | 151 | + |
| 152 | + // ========== 邮件发送 1002025000 ========== | ||
| 153 | + ErrorCode MAIL_SEND_TEMPLATE_PARAM_MISS = new ErrorCode(1002025000, "模板参数({})缺失"); | ||
| 154 | + ErrorCode MAIL_SEND_MAIL_NOT_EXISTS = new ErrorCode(1002025000, "邮箱不存在"); | ||
| 152 | 155 | ||
| 153 | } | 156 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java
| @@ -17,9 +17,4 @@ public interface MailAccountMapper extends BaseMapperX<MailAccountDO> { | @@ -17,9 +17,4 @@ public interface MailAccountMapper extends BaseMapperX<MailAccountDO> { | ||
| 17 | .likeIfPresent(MailAccountDO::getUsername , pageReqVO.getUsername())); | 17 | .likeIfPresent(MailAccountDO::getUsername , pageReqVO.getUsername())); |
| 18 | } | 18 | } |
| 19 | 19 | ||
| 20 | - default MailAccountDO selectOneByFrom(String from){ | ||
| 21 | - return selectOne(new QueryWrapperX<MailAccountDO>() | ||
| 22 | - .eqIfPresent("from_address" , from)); | ||
| 23 | - } | ||
| 24 | - | ||
| 25 | } | 20 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountService.java
| @@ -23,6 +23,14 @@ public interface MailAccountService { | @@ -23,6 +23,14 @@ public interface MailAccountService { | ||
| 23 | void initLocalCache(); | 23 | void initLocalCache(); |
| 24 | 24 | ||
| 25 | /** | 25 | /** |
| 26 | + * 从缓存中获取邮箱账号 | ||
| 27 | + * | ||
| 28 | + * @param id 编号 | ||
| 29 | + * @return 邮箱账号 | ||
| 30 | + */ | ||
| 31 | + MailAccountDO getMailAccountFromCache(Long id); | ||
| 32 | + | ||
| 33 | + /** | ||
| 26 | * 创建邮箱账号 | 34 | * 创建邮箱账号 |
| 27 | * | 35 | * |
| 28 | * @param createReqVO 邮箱账号信息 | 36 | * @param createReqVO 邮箱账号信息 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailAccountServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.system.service.mail.impl; | 1 | +package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; | 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; |
| @@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; | @@ -8,8 +8,6 @@ import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; | ||
| 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 9 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; | 9 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; |
| 10 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 10 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 11 | -import cn.iocoder.yudao.module.system.service.mail.MailAccountService; | ||
| 12 | -import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; | ||
| 13 | import lombok.extern.slf4j.Slf4j; | 11 | import lombok.extern.slf4j.Slf4j; |
| 14 | import org.springframework.stereotype.Service; | 12 | import org.springframework.stereotype.Service; |
| 15 | import org.springframework.validation.annotation.Validated; | 13 | import org.springframework.validation.annotation.Validated; |
| @@ -21,8 +19,7 @@ import java.util.Map; | @@ -21,8 +19,7 @@ import java.util.Map; | ||
| 21 | 19 | ||
| 22 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 20 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 23 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; | 21 | import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| 24 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_NOT_EXISTS; | ||
| 25 | -import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_RELATE_TEMPLATE_EXISTS; | 22 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 26 | 23 | ||
| 27 | /** | 24 | /** |
| 28 | * 邮箱账号 Service 实现类 | 25 | * 邮箱账号 Service 实现类 |
| @@ -64,6 +61,11 @@ public class MailAccountServiceImpl implements MailAccountService { | @@ -64,6 +61,11 @@ public class MailAccountServiceImpl implements MailAccountService { | ||
| 64 | } | 61 | } |
| 65 | 62 | ||
| 66 | @Override | 63 | @Override |
| 64 | + public MailAccountDO getMailAccountFromCache(Long id) { | ||
| 65 | + return mailAccountCache.get(id); | ||
| 66 | + } | ||
| 67 | + | ||
| 68 | + @Override | ||
| 67 | public Long createMailAccount(MailAccountCreateReqVO createReqVO) { | 69 | public Long createMailAccount(MailAccountCreateReqVO createReqVO) { |
| 68 | // 插入 | 70 | // 插入 |
| 69 | MailAccountDO account = MailAccountConvert.INSTANCE.convert(createReqVO); | 71 | MailAccountDO account = MailAccountConvert.INSTANCE.convert(createReqVO); |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailLogServiceImpl.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailLogServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.system.service.mail.impl; | 1 | +package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 3 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.log.MailLogExportReqVO; | 4 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.log.MailLogExportReqVO; |
| @@ -28,7 +28,6 @@ import java.util.Objects; | @@ -28,7 +28,6 @@ import java.util.Objects; | ||
| 28 | @Validated | 28 | @Validated |
| 29 | public class MailLogServiceImpl implements MailLogService { | 29 | public class MailLogServiceImpl implements MailLogService { |
| 30 | 30 | ||
| 31 | - // TODO @wangjingyi:private,然后使用 @Resource DONE | ||
| 32 | @Resource | 31 | @Resource |
| 33 | private MailLogMapper mailLogMapper; | 32 | private MailLogMapper mailLogMapper; |
| 34 | 33 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendService.java
| 1 | package cn.iocoder.yudao.module.system.service.mail; | 1 | package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | ||
| 4 | -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO; | ||
| 5 | -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; | ||
| 6 | -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; | ||
| 7 | -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; | ||
| 8 | -import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | ||
| 9 | import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; | 3 | import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; |
| 10 | 4 | ||
| 11 | -import javax.validation.Valid; | ||
| 12 | -import java.util.List; | ||
| 13 | import java.util.Map; | 5 | import java.util.Map; |
| 14 | 6 | ||
| 15 | /** | 7 | /** |
| 16 | - * 邮箱模版服务类 | 8 | + * 邮件发送 Service 接口 |
| 17 | * | 9 | * |
| 18 | * @author wangjingyi | 10 | * @author wangjingyi |
| 19 | * @since 2022-03-21 | 11 | * @since 2022-03-21 |
| @@ -21,17 +13,41 @@ import java.util.Map; | @@ -21,17 +13,41 @@ import java.util.Map; | ||
| 21 | public interface MailSendService { | 13 | public interface MailSendService { |
| 22 | 14 | ||
| 23 | /** | 15 | /** |
| 24 | - * 发送邮件 | 16 | + * 发送单条邮件给管理后台的用户 |
| 25 | * | 17 | * |
| 18 | + * @param mail 邮箱 | ||
| 19 | + * @param userId 用户编码 | ||
| 26 | * @param templateCode 邮件模版编码 | 20 | * @param templateCode 邮件模版编码 |
| 27 | - * @param from 邮箱 | ||
| 28 | - * @param content 内容 | ||
| 29 | - * @param templateParams 模版参数 | ||
| 30 | - * @param to 收件人 | 21 | + * @param templateParams 邮件模版参数 |
| 22 | + * @return 发送日志编号 | ||
| 23 | + */ | ||
| 24 | + Long sendSingleMailToAdmin(String mail, Long userId, | ||
| 25 | + String templateCode, Map<String, Object> templateParams); | ||
| 26 | + | ||
| 27 | + /** | ||
| 28 | + * 发送单条邮件给用户 APP 的用户 | ||
| 29 | + * | ||
| 30 | + * @param mail 邮箱 | ||
| 31 | + * @param userId 用户编码 | ||
| 32 | + * @param templateCode 邮件模版编码 | ||
| 33 | + * @param templateParams 邮件模版参数 | ||
| 34 | + * @return 发送日志编号 | ||
| 35 | + */ | ||
| 36 | + Long sendSingleMailToMember(String mail, Long userId, | ||
| 37 | + String templateCode, Map<String, Object> templateParams); | ||
| 38 | + | ||
| 39 | + /** | ||
| 40 | + * 发送单条邮件给用户 | ||
| 41 | + * | ||
| 42 | + * @param mail 邮箱 | ||
| 31 | * @param userId 用户编码 | 43 | * @param userId 用户编码 |
| 32 | * @param userType 用户类型 | 44 | * @param userType 用户类型 |
| 45 | + * @param templateCode 邮件模版编码 | ||
| 46 | + * @param templateParams 邮件模版参数 | ||
| 47 | + * @return 发送日志编号 | ||
| 33 | */ | 48 | */ |
| 34 | - void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams); | 49 | + Long sendSingleMail(String mail, Long userId, Integer userType, |
| 50 | + String templateCode, Map<String, Object> templateParams); | ||
| 35 | 51 | ||
| 36 | /** | 52 | /** |
| 37 | * 执行真正的邮件发送 | 53 | * 执行真正的邮件发送 |
| @@ -40,4 +56,5 @@ public interface MailSendService { | @@ -40,4 +56,5 @@ public interface MailSendService { | ||
| 40 | * @param message 邮件 | 56 | * @param message 邮件 |
| 41 | */ | 57 | */ |
| 42 | void doSendMail(MailSendMessage message); | 58 | void doSendMail(MailSendMessage message); |
| 59 | + | ||
| 43 | } | 60 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailSendServiceImpl.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailSendServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.system.service.mail.impl; | 1 | +package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | +import cn.hutool.core.util.StrUtil; | ||
| 3 | import cn.hutool.extra.mail.MailAccount; | 4 | import cn.hutool.extra.mail.MailAccount; |
| 4 | import cn.hutool.extra.mail.MailUtil; | 5 | import cn.hutool.extra.mail.MailUtil; |
| 5 | import cn.iocoder.yudao.framework.common.core.KeyValue; | 6 | import cn.iocoder.yudao.framework.common.core.KeyValue; |
| 6 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; | 7 | import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum; |
| 8 | +import cn.iocoder.yudao.framework.common.enums.UserTypeEnum; | ||
| 7 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; | 9 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; |
| 8 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; | 10 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 9 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 12 | +import cn.iocoder.yudao.module.system.dal.dataobject.sms.SmsChannelDO; | ||
| 13 | +import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO; | ||
| 10 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; | 14 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; |
| 11 | import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; | 15 | import cn.iocoder.yudao.module.system.mq.message.mail.MailSendMessage; |
| 12 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 16 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 13 | import cn.iocoder.yudao.module.system.service.mail.MailLogService; | 17 | import cn.iocoder.yudao.module.system.service.mail.MailLogService; |
| 14 | import cn.iocoder.yudao.module.system.service.mail.MailSendService; | 18 | import cn.iocoder.yudao.module.system.service.mail.MailSendService; |
| 15 | import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; | 19 | import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; |
| 20 | +import cn.iocoder.yudao.module.system.service.member.MemberService; | ||
| 21 | +import cn.iocoder.yudao.module.system.service.user.AdminUserService; | ||
| 16 | import com.google.common.annotations.VisibleForTesting; | 22 | import com.google.common.annotations.VisibleForTesting; |
| 17 | import lombok.extern.slf4j.Slf4j; | 23 | import lombok.extern.slf4j.Slf4j; |
| 18 | import org.springframework.stereotype.Service; | 24 | import org.springframework.stereotype.Service; |
| @@ -36,42 +42,70 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | @@ -36,42 +42,70 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||
| 36 | @Validated | 42 | @Validated |
| 37 | @Slf4j | 43 | @Slf4j |
| 38 | public class MailSendServiceImpl implements MailSendService { | 44 | public class MailSendServiceImpl implements MailSendService { |
| 39 | - | 45 | + |
| 46 | + @Resource | ||
| 47 | + private AdminUserService adminUserService; | ||
| 48 | + @Resource | ||
| 49 | + private MemberService memberService; | ||
| 50 | + | ||
| 40 | @Resource | 51 | @Resource |
| 41 | - private MailAccountMapper mailAccountMapper; | 52 | + private MailAccountService mailAccountService; |
| 42 | @Resource | 53 | @Resource |
| 43 | private MailTemplateService mailTemplateService; | 54 | private MailTemplateService mailTemplateService; |
| 55 | + | ||
| 44 | @Resource | 56 | @Resource |
| 45 | private MailLogService mailLogService; | 57 | private MailLogService mailLogService; |
| 46 | @Resource | 58 | @Resource |
| 47 | private MailProducer mailProducer; | 59 | private MailProducer mailProducer; |
| 48 | 60 | ||
| 61 | + @Override | ||
| 62 | + public Long sendSingleMailToAdmin(String mail, Long userId, | ||
| 63 | + String templateCode, Map<String, Object> templateParams) { | ||
| 64 | + // 如果 mail 为空,则加载用户编号对应的邮箱 | ||
| 65 | + if (StrUtil.isEmpty(mail)) { | ||
| 66 | + AdminUserDO user = adminUserService.getUser(userId); | ||
| 67 | + if (user != null) { | ||
| 68 | + mail = user.getEmail(); | ||
| 69 | + } | ||
| 70 | + } | ||
| 71 | + // 执行发送 | ||
| 72 | + return sendSingleMail(mail, userId, UserTypeEnum.ADMIN.getValue(), templateCode, templateParams); | ||
| 73 | + } | ||
| 49 | 74 | ||
| 50 | @Override | 75 | @Override |
| 51 | - public void sendMail(Long userId, Integer userType, String templateCode, String from,String to, String content, Map<String, Object> templateParams) { | ||
| 52 | - // TODO @@wangjingyi:发送的时候,参考下短信;DONE | ||
| 53 | - //校验邮箱模版是否合法 | ||
| 54 | - MailTemplateDO mailTemplateDO = this.checkMailTemplateValid(templateCode); | ||
| 55 | - // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 | ||
| 56 | - Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(mailTemplateDO.getStatus()); | ||
| 57 | - //校验账号信息是否合法 | ||
| 58 | - MailAccountDO mailAccountDO = this.checkMailAccountValid(from); | ||
| 59 | - Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); | ||
| 60 | - content = mailTemplateService.formatMailTemplateContent(mailTemplateDO.getContent(), params); | ||
| 61 | - Long sendLogId = mailLogService.createMailLog(userId,userType,to,mailAccountDO , mailTemplateDO , content, templateParams, isSend); | ||
| 62 | - List<KeyValue<String,Object>> newTemplateParams = buildTemplateParams(mailTemplateDO,templateParams); | ||
| 63 | - // 发送 MQ 消息,异步执行发送短信 | ||
| 64 | - if (isSend) { | ||
| 65 | - mailProducer.sendMailSendMessage(sendLogId,mailAccountDO , mailTemplateDO ,content, newTemplateParams,to); | 76 | + public Long sendSingleMailToMember(String mail, Long userId, |
| 77 | + String templateCode, Map<String, Object> templateParams) { | ||
| 78 | + // 如果 mail 为空,则加载用户编号对应的邮箱 | ||
| 79 | + if (StrUtil.isEmpty(mail)) { | ||
| 80 | + mail = memberService.getMemberUserEmail(userId); | ||
| 66 | } | 81 | } |
| 82 | + // 执行发送 | ||
| 83 | + return sendSingleMail(mail, userId, UserTypeEnum.MEMBER.getValue(), templateCode, templateParams); | ||
| 67 | } | 84 | } |
| 68 | 85 | ||
| 69 | - private MailAccountDO checkMailAccountValid(String from) { | ||
| 70 | - MailAccountDO mailAccountDO = mailAccountMapper.selectOneByFrom(from); | ||
| 71 | - if(null == mailAccountDO){ | ||
| 72 | - throw exception(MAIL_ACCOUNT_NOT_EXISTS); | 86 | + @Override |
| 87 | + public Long sendSingleMail(String mail, Long userId, Integer userType, | ||
| 88 | + String templateCode, Map<String, Object> templateParams) { | ||
| 89 | + // 校验邮箱模版是否合法 | ||
| 90 | + MailTemplateDO template = checkMailTemplateValid(templateCode); | ||
| 91 | + // 校验邮箱账号是否合法 | ||
| 92 | + MailAccountDO account = checkMailAccountValid(template.getAccountId()); | ||
| 93 | + | ||
| 94 | + // 校验邮箱是否存在 | ||
| 95 | + mail = checkMail(mail); | ||
| 96 | + // 构建有序的模板参数。为什么放在这个位置,是提前保证模板参数的正确性,而不是到了插入发送日志 | ||
| 97 | + List<KeyValue<String, Object>> newTemplateParams = buildTemplateParams(template, templateParams); | ||
| 98 | + | ||
| 99 | + // 创建发送日志。如果模板被禁用,则不发送短信,只记录日志 | ||
| 100 | + Boolean isSend = CommonStatusEnum.ENABLE.getStatus().equals(template.getStatus()); | ||
| 101 | + String content = mailTemplateService.formatMailTemplateContent(template.getContent(), templateParams); | ||
| 102 | + Long sendLogId = mailLogService.createMailLog(userId, userType, mail, | ||
| 103 | + account, template, content, templateParams, isSend); // TODO 芋艿:待测试 | ||
| 104 | + // 发送 MQ 消息,异步执行发送短信 | ||
| 105 | + if (isSend) { | ||
| 106 | + mailProducer.sendMailSendMessage(sendLogId, account, template, content, newTemplateParams, mail); // TODO 芋艿:待测试 | ||
| 73 | } | 107 | } |
| 74 | - return mailAccountDO; | 108 | + return sendLogId; |
| 75 | } | 109 | } |
| 76 | 110 | ||
| 77 | @Override | 111 | @Override |
| @@ -80,22 +114,44 @@ public class MailSendServiceImpl implements MailSendService { | @@ -80,22 +114,44 @@ public class MailSendServiceImpl implements MailSendService { | ||
| 80 | //装载账号信息 | 114 | //装载账号信息 |
| 81 | MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message); | 115 | MailAccount account = MailAccountConvert.INSTANCE.convertAccount(message); |
| 82 | //发送邮件 | 116 | //发送邮件 |
| 83 | - try{ | 117 | + try { |
| 84 | String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null); | 118 | String messageId = MailUtil.send(account,message.getTo(),message.getTitle(),message.getContent(),false,null); |
| 85 | mailLogService.updateMailSendResult(message.getLogId() , messageId); | 119 | mailLogService.updateMailSendResult(message.getLogId() , messageId); |
| 86 | - }catch (Exception e){ | 120 | + } catch (Exception e){ |
| 87 | mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage()); | 121 | mailLogService.updateFailMailSendResult(message.getLogId() , e.getMessage()); |
| 88 | } | 122 | } |
| 89 | - | ||
| 90 | } | 123 | } |
| 91 | 124 | ||
| 92 | - private MailTemplateDO checkMailTemplateValid(String templateCode) { | ||
| 93 | - MailTemplateDO mailTemplateDO = mailTemplateService.getMailTemplateByCodeFromCache(templateCode); | ||
| 94 | - if (mailTemplateDO == null){ | 125 | + @VisibleForTesting |
| 126 | + public MailTemplateDO checkMailTemplateValid(String templateCode) { | ||
| 127 | + // 获得邮件模板。考虑到效率,从缓存中获取 | ||
| 128 | + MailTemplateDO template = mailTemplateService.getMailTemplateByCodeFromCache(templateCode); | ||
| 129 | + // 邮件模板不存在 | ||
| 130 | + if (template == null) { | ||
| 95 | throw exception(MAIL_TEMPLATE_NOT_EXISTS); | 131 | throw exception(MAIL_TEMPLATE_NOT_EXISTS); |
| 96 | } | 132 | } |
| 97 | - return mailTemplateDO; | 133 | + return template; |
| 98 | } | 134 | } |
| 135 | + | ||
| 136 | + @VisibleForTesting | ||
| 137 | + public MailAccountDO checkMailAccountValid(Long accountId) { | ||
| 138 | + // 获得邮箱账号。考虑到效率,从缓存中获取 | ||
| 139 | + MailAccountDO account = mailAccountService.getMailAccountFromCache(accountId); | ||
| 140 | + // 邮箱账号不存在 | ||
| 141 | + if (account == null) { | ||
| 142 | + throw exception(MAIL_ACCOUNT_NOT_EXISTS); | ||
| 143 | + } | ||
| 144 | + return account; | ||
| 145 | + } | ||
| 146 | + | ||
| 147 | + @VisibleForTesting | ||
| 148 | + public String checkMail(String mail) { | ||
| 149 | + if (StrUtil.isEmpty(mail)) { | ||
| 150 | + throw exception(MAIL_SEND_MAIL_NOT_EXISTS); | ||
| 151 | + } | ||
| 152 | + return mail; | ||
| 153 | + } | ||
| 154 | + | ||
| 99 | /** | 155 | /** |
| 100 | * 将参数模板,处理成有序的 KeyValue 数组 | 156 | * 将参数模板,处理成有序的 KeyValue 数组 |
| 101 | * | 157 | * |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateService.java
| @@ -83,7 +83,7 @@ public interface MailTemplateService { | @@ -83,7 +83,7 @@ public interface MailTemplateService { | ||
| 83 | * @param params 合成参数 | 83 | * @param params 合成参数 |
| 84 | * @return 格式化后的内容 | 84 | * @return 格式化后的内容 |
| 85 | */ | 85 | */ |
| 86 | - String formatMailTemplateContent(String content, Map<String, String> params); | 86 | + String formatMailTemplateContent(String content, Map<String, Object> params); |
| 87 | 87 | ||
| 88 | /** | 88 | /** |
| 89 | * 获得指定邮件账号下的邮件模板数量 | 89 | * 获得指定邮件账号下的邮件模板数量 |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailTemplateServiceImpl.java renamed to yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/mail/MailTemplateServiceImpl.java
| 1 | -package cn.iocoder.yudao.module.system.service.mail.impl; | 1 | +package cn.iocoder.yudao.module.system.service.mail; |
| 2 | 2 | ||
| 3 | - | ||
| 4 | -import cn.hutool.core.collection.CollUtil; | ||
| 5 | import cn.hutool.core.util.ObjUtil; | 3 | import cn.hutool.core.util.ObjUtil; |
| 4 | +import cn.hutool.core.util.ReUtil; | ||
| 6 | import cn.hutool.core.util.StrUtil; | 5 | import cn.hutool.core.util.StrUtil; |
| 7 | import cn.iocoder.yudao.framework.common.pojo.PageResult; | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 8 | -import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; | ||
| 9 | -import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; | ||
| 10 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateCreateReqVO; |
| 11 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; | 8 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplatePageReqVO; |
| 12 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; | 9 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.template.MailTemplateUpdateReqVO; |
| @@ -14,7 +11,6 @@ import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert; | @@ -14,7 +11,6 @@ import cn.iocoder.yudao.module.system.convert.mail.MailTemplateConvert; | ||
| 14 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; | 11 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 15 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; | 12 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; |
| 16 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; | 13 | import cn.iocoder.yudao.module.system.mq.producer.mail.MailProducer; |
| 17 | -import cn.iocoder.yudao.module.system.service.mail.MailTemplateService; | ||
| 18 | import com.google.common.annotations.VisibleForTesting; | 14 | import com.google.common.annotations.VisibleForTesting; |
| 19 | import lombok.extern.slf4j.Slf4j; | 15 | import lombok.extern.slf4j.Slf4j; |
| 20 | import org.springframework.stereotype.Service; | 16 | import org.springframework.stereotype.Service; |
| @@ -23,12 +19,12 @@ import org.springframework.validation.annotation.Validated; | @@ -23,12 +19,12 @@ import org.springframework.validation.annotation.Validated; | ||
| 23 | import javax.annotation.PostConstruct; | 19 | import javax.annotation.PostConstruct; |
| 24 | import javax.annotation.Resource; | 20 | import javax.annotation.Resource; |
| 25 | import javax.validation.Valid; | 21 | import javax.validation.Valid; |
| 26 | -import java.util.Date; | ||
| 27 | import java.util.List; | 22 | import java.util.List; |
| 28 | import java.util.Map; | 23 | import java.util.Map; |
| 24 | +import java.util.regex.Pattern; | ||
| 29 | 25 | ||
| 30 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 26 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 31 | -import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.*; | 27 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertMap; |
| 32 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | 28 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; |
| 33 | 29 | ||
| 34 | /** | 30 | /** |
| @@ -42,6 +38,11 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | @@ -42,6 +38,11 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||
| 42 | @Slf4j | 38 | @Slf4j |
| 43 | public class MailTemplateServiceImpl implements MailTemplateService { | 39 | public class MailTemplateServiceImpl implements MailTemplateService { |
| 44 | 40 | ||
| 41 | + /** | ||
| 42 | + * 正则表达式,匹配 {} 中的变量 | ||
| 43 | + */ | ||
| 44 | + private static final Pattern PATTERN_PARAMS = Pattern.compile("\\{(.*?)}"); | ||
| 45 | + | ||
| 45 | @Resource | 46 | @Resource |
| 46 | private MailTemplateMapper mailTemplateMapper; | 47 | private MailTemplateMapper mailTemplateMapper; |
| 47 | 48 | ||
| @@ -73,7 +74,8 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -73,7 +74,8 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 73 | validateCodeUnique(null, createReqVO.getCode()); | 74 | validateCodeUnique(null, createReqVO.getCode()); |
| 74 | 75 | ||
| 75 | // 插入 | 76 | // 插入 |
| 76 | - MailTemplateDO template = MailTemplateConvert.INSTANCE.convert(createReqVO); | 77 | + MailTemplateDO template = MailTemplateConvert.INSTANCE.convert(createReqVO) |
| 78 | + .setParams(parseTemplateContentParams(createReqVO.getContent())); | ||
| 77 | mailTemplateMapper.insert(template); | 79 | mailTemplateMapper.insert(template); |
| 78 | // 发送刷新消息 | 80 | // 发送刷新消息 |
| 79 | mailProducer.sendMailTemplateRefreshMessage(); | 81 | mailProducer.sendMailTemplateRefreshMessage(); |
| @@ -88,7 +90,8 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -88,7 +90,8 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 88 | validateCodeUnique(updateReqVO.getId(),updateReqVO.getCode()); | 90 | validateCodeUnique(updateReqVO.getId(),updateReqVO.getCode()); |
| 89 | 91 | ||
| 90 | // 更新 | 92 | // 更新 |
| 91 | - MailTemplateDO updateObj = MailTemplateConvert.INSTANCE.convert(updateReqVO); | 93 | + MailTemplateDO updateObj = MailTemplateConvert.INSTANCE.convert(updateReqVO) |
| 94 | + .setParams(parseTemplateContentParams(updateReqVO.getContent())); | ||
| 92 | mailTemplateMapper.updateById(updateObj); | 95 | mailTemplateMapper.updateById(updateObj); |
| 93 | // 发送刷新消息 | 96 | // 发送刷新消息 |
| 94 | mailProducer.sendMailTemplateRefreshMessage(); | 97 | mailProducer.sendMailTemplateRefreshMessage(); |
| @@ -118,6 +121,12 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -118,6 +121,12 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 118 | mailProducer.sendMailTemplateRefreshMessage(); | 121 | mailProducer.sendMailTemplateRefreshMessage(); |
| 119 | } | 122 | } |
| 120 | 123 | ||
| 124 | + private void validateMailTemplateExists(Long id) { | ||
| 125 | + if (mailTemplateMapper.selectById(id) == null) { | ||
| 126 | + throw exception(MAIL_TEMPLATE_NOT_EXISTS); | ||
| 127 | + } | ||
| 128 | + } | ||
| 129 | + | ||
| 121 | @Override | 130 | @Override |
| 122 | public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);} | 131 | public MailTemplateDO getMailTemplate(Long id) {return mailTemplateMapper.selectById(id);} |
| 123 | 132 | ||
| @@ -135,14 +144,13 @@ public class MailTemplateServiceImpl implements MailTemplateService { | @@ -135,14 +144,13 @@ public class MailTemplateServiceImpl implements MailTemplateService { | ||
| 135 | } | 144 | } |
| 136 | 145 | ||
| 137 | @Override | 146 | @Override |
| 138 | - public String formatMailTemplateContent(String content, Map<String, String> params) { | 147 | + public String formatMailTemplateContent(String content, Map<String, Object> params) { |
| 139 | return StrUtil.format(content, params); | 148 | return StrUtil.format(content, params); |
| 140 | } | 149 | } |
| 141 | 150 | ||
| 142 | - private void validateMailTemplateExists(Long id) { | ||
| 143 | - if (mailTemplateMapper.selectById(id) == null) { | ||
| 144 | - throw exception(MAIL_TEMPLATE_NOT_EXISTS); | ||
| 145 | - } | 151 | + @VisibleForTesting |
| 152 | + public List<String> parseTemplateContentParams(String content) { | ||
| 153 | + return ReUtil.findAllGroup1(PATTERN_PARAMS, content); | ||
| 146 | } | 154 | } |
| 147 | 155 | ||
| 148 | @Override | 156 | @Override |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/member/MemberService.java
| @@ -15,4 +15,12 @@ public interface MemberService { | @@ -15,4 +15,12 @@ public interface MemberService { | ||
| 15 | */ | 15 | */ |
| 16 | String getMemberUserMobile(Long id); | 16 | String getMemberUserMobile(Long id); |
| 17 | 17 | ||
| 18 | + /** | ||
| 19 | + * 获得会员用户的邮箱 | ||
| 20 | + * | ||
| 21 | + * @param id 会员用户编号 | ||
| 22 | + * @return 邮箱 | ||
| 23 | + */ | ||
| 24 | + String getMemberUserEmail(Long id); | ||
| 25 | + | ||
| 18 | } | 26 | } |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/member/MemberServiceImpl.java
| @@ -21,14 +21,27 @@ public class MemberServiceImpl implements MemberService { | @@ -21,14 +21,27 @@ public class MemberServiceImpl implements MemberService { | ||
| 21 | 21 | ||
| 22 | @Override | 22 | @Override |
| 23 | public String getMemberUserMobile(Long id) { | 23 | public String getMemberUserMobile(Long id) { |
| 24 | - if (id == null) { | 24 | + Object user = getMemberUser(id); |
| 25 | + if (user == null) { | ||
| 25 | return null; | 26 | return null; |
| 26 | } | 27 | } |
| 27 | - Object user = ReflectUtil.invoke(getMemberUserApi(), "getUser", id); | 28 | + return ReflectUtil.invoke(user, "getMobile"); |
| 29 | + } | ||
| 30 | + | ||
| 31 | + @Override | ||
| 32 | + public String getMemberUserEmail(Long id) { | ||
| 33 | + Object user = getMemberUser(id); | ||
| 28 | if (user == null) { | 34 | if (user == null) { |
| 29 | return null; | 35 | return null; |
| 30 | } | 36 | } |
| 31 | - return ReflectUtil.invoke(user, "getMobile"); | 37 | + return ReflectUtil.invoke(user, "getEmail"); |
| 38 | + } | ||
| 39 | + | ||
| 40 | + private Object getMemberUser(Long id) { | ||
| 41 | + if (id == null) { | ||
| 42 | + return null; | ||
| 43 | + } | ||
| 44 | + return ReflectUtil.invoke(getMemberUserApi(), "getUser", id); | ||
| 32 | } | 45 | } |
| 33 | 46 | ||
| 34 | private Object getMemberUserApi() { | 47 | private Object getMemberUserApi() { |
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sms/SmsTemplateServiceImpl.java
| @@ -39,7 +39,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | @@ -39,7 +39,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*; | ||
| 39 | * 短信模板 Service 实现类 | 39 | * 短信模板 Service 实现类 |
| 40 | * | 40 | * |
| 41 | * @author zzf | 41 | * @author zzf |
| 42 | - * @date 2021/1/25 9:25 | 42 | + * @since 2021/1/25 9:25 |
| 43 | */ | 43 | */ |
| 44 | @Service | 44 | @Service |
| 45 | @Slf4j | 45 | @Slf4j |