fix: 邮件模板配置枚举字段获取
This commit is contained in:
parent
4c3a2e87d0
commit
50a42f842c
|
@ -1,23 +0,0 @@
|
|||
package cn.bunny.common.service.utils.mail;
|
||||
|
||||
import cn.bunny.common.service.utils.EmptyUtil;
|
||||
import cn.bunny.dao.pojo.common.EmailSend;
|
||||
import cn.bunny.dao.pojo.constant.MailMessageConstant;
|
||||
|
||||
public class MailSendCheckUtil {
|
||||
/**
|
||||
* 检测发送对象是否为空的对象
|
||||
*
|
||||
* @param emailSend 邮件发送对象
|
||||
*/
|
||||
public static void check(EmailSend emailSend) {
|
||||
// 空发送对象
|
||||
EmptyUtil.isEmpty(emailSend, MailMessageConstant.EMPTY_SEND_OBJECT);
|
||||
// 收件人不能为空
|
||||
EmptyUtil.isEmpty(emailSend.getSendTo(), MailMessageConstant.ADDRESS_NOT_NULL);
|
||||
// 标题不能为空
|
||||
EmptyUtil.isEmpty(emailSend.getSubject(), MailMessageConstant.TITLE_NOT_NULL);
|
||||
// 发送消息不能为空
|
||||
EmptyUtil.isEmpty(emailSend.getMessage(), MailMessageConstant.SEND_MESSAGE_NOT_NULL);
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
package cn.bunny.dao.pojo.constant;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 邮箱消息
|
||||
*/
|
||||
@Data
|
||||
public class MailMessageConstant {
|
||||
public static final String EMPTY_SEND_OBJECT = "空发送对象";
|
||||
public static final String ADDRESS_NOT_NULL = "收件人不能为空";
|
||||
public static final String TITLE_NOT_NULL = "标题不能为空";
|
||||
public static final String SEND_MESSAGE_NOT_NULL = "发送消息不能为空";
|
||||
public static final String EMAIL_CONFIG_NOT_FOUND = "邮箱配置为空";
|
||||
}
|
|
@ -40,6 +40,7 @@ public enum ResultCodeEnum {
|
|||
DATA_ERROR(206, "数据异常"),
|
||||
EMAIL_USER_TEMPLATE_IS_EMPTY(206, "邮件模板为空"),
|
||||
EMAIL_TEMPLATE_IS_EMPTY(206, "邮件模板为空"),
|
||||
EMAIL_USER_IS_EMPTY(206, "关联邮件用户配置为空"),
|
||||
DATA_EXIST(206, "数据已存在"),
|
||||
DATA_NOT_EXIST(206, "数据不存在"),
|
||||
USER_IS_EMPTY(206, "用户不存在"),
|
||||
|
|
|
@ -19,6 +19,7 @@ import org.springframework.web.bind.annotation.*;
|
|||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -28,7 +29,7 @@ import java.util.List;
|
|||
* @author Bunny
|
||||
* @since 2024-10-10 21:24:08
|
||||
*/
|
||||
@Tag(name = "邮件模板表", description = "邮件模板表相关接口")
|
||||
@Tag(name = "邮件模板", description = "邮件模板相关接口")
|
||||
@RestController
|
||||
@RequestMapping("admin/emailTemplate")
|
||||
public class EmailTemplateController {
|
||||
|
@ -49,6 +50,13 @@ public class EmailTemplateController {
|
|||
return Mono.just(Result.success(pageResult));
|
||||
}
|
||||
|
||||
@Operation(summary = "获取模板类型字段", description = "获取模板类型字段")
|
||||
@GetMapping("getEmailTypes")
|
||||
public Mono<Result<List<Map<String, String>>>> getEmailTypes() {
|
||||
List<Map<String, String>> list = emailTemplateService.getEmailTypes();
|
||||
return Mono.just(Result.success(list));
|
||||
}
|
||||
|
||||
@Operation(summary = "添加邮件模板表", description = "添加邮件模板表")
|
||||
@PostMapping("addEmailTemplate")
|
||||
public Mono<Result<String>> addEmailTemplate(@Valid @RequestBody EmailTemplateAddDto dto) {
|
||||
|
|
|
@ -15,6 +15,7 @@ import cn.hutool.captcha.CircleCaptcha;
|
|||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import jakarta.mail.MessagingException;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
@ -23,17 +24,16 @@ public class EmailFactory {
|
|||
|
||||
@Autowired
|
||||
private EmailTemplateMapper emailTemplateMapper;
|
||||
|
||||
|
||||
@Autowired
|
||||
private EmailUsersMapper emailUsersMapper;
|
||||
|
||||
/**
|
||||
* 生成邮箱验证码
|
||||
*
|
||||
* @param email 接受者邮箱
|
||||
* @param emailSendInit 初始化发送参数
|
||||
* @param email 接受者邮箱
|
||||
*/
|
||||
public String sendmailCode(String email, EmailSendInit emailSendInit) {
|
||||
public String sendmailCode(String email) {
|
||||
// 查询验证码邮件模板
|
||||
LambdaQueryWrapper<EmailTemplate> lambdaQueryWrapper = new LambdaQueryWrapper<>();
|
||||
lambdaQueryWrapper.eq(EmailTemplate::getIsDefault, true);
|
||||
|
@ -41,14 +41,31 @@ public class EmailFactory {
|
|||
EmailTemplate emailTemplate = emailTemplateMapper.selectOne(lambdaQueryWrapper);
|
||||
|
||||
// 判断邮件模板是否为空
|
||||
if (emailTemplate == null) {
|
||||
throw new BunnyException(ResultCodeEnum.EMAIL_TEMPLATE_IS_EMPTY);
|
||||
if (emailTemplate == null) throw new BunnyException(ResultCodeEnum.EMAIL_TEMPLATE_IS_EMPTY);
|
||||
|
||||
// 查询配置发送邮箱
|
||||
Long emailUser = emailTemplate.getEmailUser();
|
||||
EmailUsers emailUsers;
|
||||
// 如果没有配置发件者邮箱改用用户列表中默认的
|
||||
if (emailUser == null) {
|
||||
emailUsers = emailUsersMapper.selectOne(Wrappers.<EmailUsers>lambdaQuery().eq(EmailUsers::getIsDefault, true));
|
||||
// 如果默认的也为空则报错
|
||||
if (emailUsers == null) throw new BunnyException(ResultCodeEnum.EMAIL_USER_IS_EMPTY);
|
||||
} else {
|
||||
emailUsers = emailUsersMapper.selectOne(Wrappers.<EmailUsers>lambdaQuery().eq(EmailUsers::getId, emailUser));
|
||||
}
|
||||
|
||||
// 查询发件者信息
|
||||
EmailSendInit emailSendInit = new EmailSendInit();
|
||||
BeanUtils.copyProperties(emailUsers, emailSendInit);
|
||||
emailSendInit.setUsername(emailUsers.getEmail());
|
||||
|
||||
// 生成验证码
|
||||
CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(150, 48, 4, 2);
|
||||
String code = captcha.getCode();
|
||||
String htmlContent = emailTemplate.getBody().replace("${verifyCode}", code);
|
||||
String htmlContent = emailTemplate.getBody()
|
||||
.replace("${sendEmailUser}", emailUsers.getEmail())
|
||||
.replace("${verifyCode}", code);
|
||||
|
||||
// 发送验证码
|
||||
MailSenderUtil mailSenderUtil = new MailSenderUtil(emailSendInit);
|
||||
|
|
|
@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.extension.service.IService;
|
|||
import jakarta.validation.Valid;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -49,4 +50,11 @@ public interface EmailTemplateService extends IService<EmailTemplate> {
|
|||
* @param ids 删除id列表
|
||||
*/
|
||||
void deleteEmailTemplate(List<Long> ids);
|
||||
|
||||
/**
|
||||
* * 获取模板类型字段
|
||||
*
|
||||
* @return 枚举字段列表
|
||||
*/
|
||||
List<Map<String, String>> getEmailTypes();
|
||||
}
|
||||
|
|
|
@ -5,6 +5,7 @@ import cn.bunny.dao.dto.system.email.EmailTemplateAddDto;
|
|||
import cn.bunny.dao.dto.system.email.EmailTemplateDto;
|
||||
import cn.bunny.dao.dto.system.email.EmailTemplateUpdateDto;
|
||||
import cn.bunny.dao.entity.system.EmailTemplate;
|
||||
import cn.bunny.dao.pojo.common.EmailTemplateTypes;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
import cn.bunny.dao.pojo.result.ResultCodeEnum;
|
||||
import cn.bunny.dao.vo.system.email.EmailTemplateVo;
|
||||
|
@ -18,7 +19,10 @@ import jakarta.validation.Valid;
|
|||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* <p>
|
||||
|
@ -103,4 +107,19 @@ public class EmailTemplateServiceImpl extends ServiceImpl<EmailTemplateMapper, E
|
|||
|
||||
baseMapper.deleteBatchIdsWithPhysics(ids);
|
||||
}
|
||||
|
||||
/**
|
||||
* * 获取模板类型字段
|
||||
*
|
||||
* @return 枚举字段列表
|
||||
*/
|
||||
@Override
|
||||
public List<Map<String, String>> getEmailTypes() {
|
||||
return Arrays.stream(EmailTemplateTypes.values()).map(emailTemplateTypes -> {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
map.put("key", emailTemplateTypes.getType());
|
||||
map.put("value", emailTemplateTypes.getSummary());
|
||||
return map;
|
||||
}).toList();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,9 +8,7 @@ import cn.bunny.dao.dto.system.files.FileUploadDto;
|
|||
import cn.bunny.dao.dto.system.user.*;
|
||||
import cn.bunny.dao.entity.system.AdminUser;
|
||||
import cn.bunny.dao.entity.system.AdminUserAndDept;
|
||||
import cn.bunny.dao.entity.system.EmailUsers;
|
||||
import cn.bunny.dao.entity.system.UserDept;
|
||||
import cn.bunny.dao.pojo.common.EmailSendInit;
|
||||
import cn.bunny.dao.pojo.constant.MinioConstant;
|
||||
import cn.bunny.dao.pojo.constant.RedisUserConstant;
|
||||
import cn.bunny.dao.pojo.result.PageResult;
|
||||
|
@ -22,7 +20,6 @@ import cn.bunny.dao.vo.system.user.RefreshTokenVo;
|
|||
import cn.bunny.dao.vo.system.user.UserVo;
|
||||
import cn.bunny.services.factory.EmailFactory;
|
||||
import cn.bunny.services.factory.UserFactory;
|
||||
import cn.bunny.services.mapper.EmailUsersMapper;
|
||||
import cn.bunny.services.mapper.UserDeptMapper;
|
||||
import cn.bunny.services.mapper.UserMapper;
|
||||
import cn.bunny.services.mapper.UserRoleMapper;
|
||||
|
@ -64,9 +61,6 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
|||
@Autowired
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
@Autowired
|
||||
private EmailUsersMapper emailUsersMapper;
|
||||
|
||||
@Autowired
|
||||
private EmailFactory emailFactory;
|
||||
|
||||
|
@ -89,15 +83,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, AdminUser> implemen
|
|||
*/
|
||||
@Override
|
||||
public void sendLoginEmail(@NotNull String email) {
|
||||
EmailUsers emailUsers = emailUsersMapper.selectOne(Wrappers.<EmailUsers>lambdaQuery().eq(EmailUsers::getIsDefault, true));
|
||||
if (emailUsers == null) throw new BunnyException(ResultCodeEnum.EMAIL_USER_TEMPLATE_IS_EMPTY);
|
||||
|
||||
EmailSendInit emailSendInit = new EmailSendInit();
|
||||
BeanUtils.copyProperties(emailUsers, emailSendInit);
|
||||
emailSendInit.setUsername(emailUsers.getEmail());
|
||||
|
||||
String emailCode = emailFactory.sendmailCode(email, emailSendInit);
|
||||
|
||||
String emailCode = emailFactory.sendmailCode(email);
|
||||
redisTemplate.opsForValue().set(RedisUserConstant.getAdminUserEmailCodePrefix(email), emailCode);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue