Commit 1bf1d3ee6a4125d7efd23be8ce1e6513fcae9ef5
1 parent
f349fbf8
邮件发送功能修改
Showing
2 changed files
with
25 additions
and
23 deletions
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/dal/mysql/mail/MailAccountMapper.java
| ... | ... | @@ -5,8 +5,14 @@ import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX; |
| 5 | 5 | import cn.iocoder.yudao.framework.mybatis.core.query.QueryWrapperX; |
| 6 | 6 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; |
| 7 | 7 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 8 | +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 8 | 9 | import org.apache.ibatis.annotations.Mapper; |
| 9 | 10 | |
| 11 | +import java.util.Map; | |
| 12 | + | |
| 13 | +import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | |
| 14 | +import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOUNT_EXISTS; | |
| 15 | + | |
| 10 | 16 | @Mapper |
| 11 | 17 | public interface MailAccountMapper extends BaseMapperX<MailAccountDO> { |
| 12 | 18 | |
| ... | ... | @@ -20,4 +26,11 @@ public interface MailAccountMapper extends BaseMapperX<MailAccountDO> { |
| 20 | 26 | ); |
| 21 | 27 | } |
| 22 | 28 | |
| 29 | + default MailAccountDO selectByParams(Map params){ | |
| 30 | + QueryWrapperX queryWrapperX = new QueryWrapperX<MailAccountDO>(); | |
| 31 | + params.forEach((k , v)->{ | |
| 32 | + queryWrapperX.eqIfPresent((String) k, v); | |
| 33 | + }); | |
| 34 | + return this.selectOne(queryWrapperX); | |
| 35 | + }; | |
| 23 | 36 | } | ... | ... |
yudao-module-system/yudao-module-system-impl/src/main/java/cn/iocoder/yudao/module/system/service/mail/impl/MailAccountServiceImpl.java
| 1 | 1 | package cn.iocoder.yudao.module.system.service.mail.impl; |
| 2 | 2 | |
| 3 | +import cn.hutool.core.util.StrUtil; | |
| 3 | 4 | import cn.hutool.extra.mail.MailAccount; |
| 4 | 5 | import cn.hutool.extra.mail.MailUtil; |
| 5 | 6 | import cn.iocoder.yudao.framework.common.pojo.PageResult; |
| 6 | 7 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountCreateReqVO; |
| 7 | 8 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountPageReqVO; |
| 8 | 9 | import cn.iocoder.yudao.module.system.controller.admin.mail.vo.account.MailAccountUpdateReqVO; |
| 9 | -import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailSendVO; | |
| 10 | +import cn.iocoder.yudao.module.system.controller.admin.mail.vo.send.MailReqVO; | |
| 10 | 11 | import cn.iocoder.yudao.module.system.convert.mail.MailAccountConvert; |
| 11 | 12 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailAccountDO; |
| 12 | 13 | import cn.iocoder.yudao.module.system.dal.dataobject.mail.MailTemplateDO; |
| 13 | 14 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailAccountMapper; |
| 14 | 15 | import cn.iocoder.yudao.module.system.dal.mysql.mail.MailTemplateMapper; |
| 15 | 16 | import cn.iocoder.yudao.module.system.service.mail.MailAccountService; |
| 16 | -import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; | |
| 17 | 17 | import org.springframework.stereotype.Service; |
| 18 | 18 | |
| 19 | 19 | import javax.annotation.Resource; |
| ... | ... | @@ -27,9 +27,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.MAIL_ACCOU |
| 27 | 27 | |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * <p> | |
| 31 | 30 | * 邮箱账号 Service 实现类 |
| 32 | - * </p> | |
| 33 | 31 | * |
| 34 | 32 | * @author wangjingyi |
| 35 | 33 | * @since 2022-03-21 |
| ... | ... | @@ -66,7 +64,6 @@ public class MailAccountServiceImpl implements MailAccountService { |
| 66 | 64 | mailAccountMapper.updateById(mailAccountDO); |
| 67 | 65 | } |
| 68 | 66 | |
| 69 | - | |
| 70 | 67 | @Override |
| 71 | 68 | public void delete(Long id) { |
| 72 | 69 | // 校验是否存在 |
| ... | ... | @@ -90,20 +87,16 @@ public class MailAccountServiceImpl implements MailAccountService { |
| 90 | 87 | } |
| 91 | 88 | |
| 92 | 89 | @Override |
| 93 | - public void sendMail(MailSendVO mailSendVO) { | |
| 94 | - // FIXME 查询模版信息 查询模版多条时 使用规则是什么 | |
| 95 | - // 回复:选择某一条模板,进行发送邮件。 | |
| 96 | - List<MailTemplateDO> mailTemplateDOList = mailTemplateMapper.selectList( | |
| 97 | - "username",mailSendVO.getFrom() | |
| 98 | - ); | |
| 90 | + public void sendMail(MailReqVO mailReqVO) { | |
| 91 | + MailTemplateDO mailTemplateDO = mailTemplateMapper.selectById(mailReqVO.getTemplateId()); | |
| 99 | 92 | //查询账号信息 |
| 100 | 93 | MailAccountDO mailAccountDO = mailAccountMapper.selectOne( |
| 101 | - "from",mailSendVO.getFrom() | |
| 94 | + "from", mailReqVO.getFrom() | |
| 102 | 95 | ); |
| 103 | - // FIXME 模版和邮件内容合成方式未知 | |
| 104 | - // 回复:参考短信的方式,通过 {name} {mobile} 这样的占位符。搜 formatSmsTemplateContent 方法 | |
| 105 | - String content = mailSendVO.getContent(); | |
| 106 | - String templateContent = ""; | |
| 96 | + String content = mailReqVO.getContent(); | |
| 97 | + Map<String , String> params = MailAccountConvert.INSTANCE.convertToMap(mailAccountDO , content); | |
| 98 | + content = StrUtil.format(mailTemplateDO.getContent(), params); | |
| 99 | + | |
| 107 | 100 | // 后续功能 TODO :附件查询 |
| 108 | 101 | //List<String> fileIds = mailSendVO.getFileIds(); |
| 109 | 102 | |
| ... | ... | @@ -111,7 +104,7 @@ public class MailAccountServiceImpl implements MailAccountService { |
| 111 | 104 | MailAccount account = MailAccountConvert.INSTANCE.convertAccount(mailAccountDO); |
| 112 | 105 | |
| 113 | 106 | //发送 |
| 114 | - MailUtil.send(account , mailSendVO.getTos() , mailSendVO.getTitle() , mailSendVO.getContent() , false); | |
| 107 | + MailUtil.send(account , mailReqVO.getTos() , mailReqVO.getTitle() , content , false); | |
| 115 | 108 | } |
| 116 | 109 | |
| 117 | 110 | private void validateMailAccountExists(Long id) { |
| ... | ... | @@ -121,12 +114,8 @@ public class MailAccountServiceImpl implements MailAccountService { |
| 121 | 114 | } |
| 122 | 115 | |
| 123 | 116 | private void validateMailAccountOnly(Map params){ |
| 124 | - // TODO wangjingyi:Service 里,不允许出现 MyBatis 操作。而是 Mapper 提供对应查询方法 | |
| 125 | - QueryWrapper queryWrapper = new QueryWrapper<MailAccountDO>(); | |
| 126 | - params.forEach((k , v)->{ | |
| 127 | - queryWrapper.like(k , v); // TODO wangjingyi:账号,应该是 equlas,不能是 like | |
| 128 | - }); | |
| 129 | - if (mailAccountMapper.selectOne(queryWrapper) != null) { | |
| 117 | + MailAccountDO mailAccountDO = mailAccountMapper.selectByParams(params); | |
| 118 | + if (mailAccountDO != null) { | |
| 130 | 119 | throw exception(MAIL_ACCOUNT_EXISTS); |
| 131 | 120 | } |
| 132 | 121 | } | ... | ... |