无邮件服务配置无法发送提示

pull/37/head
Friday 1 year ago committed by smallbun
parent 18fc2e090a
commit a312b2ae39

@ -24,24 +24,16 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import cn.topiam.employee.common.constant.SettingConstants;
import cn.topiam.employee.common.entity.setting.SettingEntity;
import cn.topiam.employee.common.entity.setting.config.SmsConfig;
import cn.topiam.employee.common.jackjson.encrypt.EncryptionModule;
import cn.topiam.employee.common.message.mail.DefaultMailProviderSendImpl;
import cn.topiam.employee.common.message.mail.MailProviderConfig;
import cn.topiam.employee.common.message.mail.MailProviderSend;
import cn.topiam.employee.common.message.sms.SmsNoneProviderSend;
import cn.topiam.employee.common.message.sms.SmsProviderSend;
import cn.topiam.employee.common.message.sms.SmsSendProviderFactory;
import cn.topiam.employee.common.repository.setting.SettingRepository;
import cn.topiam.employee.core.setting.constant.MessageSettingConstants;
import static cn.topiam.employee.common.constant.ConfigBeanNameConstants.MAIL_PROVIDER_SEND;
import static cn.topiam.employee.common.constant.ConfigBeanNameConstants.SMS_PROVIDER_SEND;
import static cn.topiam.employee.core.help.SettingHelp.getMailProviderConfig;
import static cn.topiam.employee.core.help.SettingHelp.getSmsProviderConfig;
/**
@ -72,30 +64,17 @@ public class EiamMessageSendConfiguration {
/**
*
*
* @param messageSettingRepository {@link SettingRepository}
* @param taskExecutor {@link TaskExecutor}
* @return {@link MailProviderSend}
*/
@Bean(MAIL_PROVIDER_SEND)
@RefreshScope
public MailProviderSend mailProviderSend(SettingRepository messageSettingRepository,
TaskExecutor taskExecutor) {
try {
SettingEntity setting = messageSettingRepository
.findByName(MessageSettingConstants.MESSAGE_PROVIDER_EMAIL);
if (!Objects.isNull(setting)
&& !SettingConstants.NOT_CONFIG.equals(setting.getValue())) {
String value = setting.getValue();
ObjectMapper objectMapper = EncryptionModule.deserializerDecrypt();
// 指定序列化输入的类型
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// 根据提供商序列化
MailProviderConfig config = objectMapper.readValue(value, MailProviderConfig.class);
return new DefaultMailProviderSendImpl(config, taskExecutor);
}
public MailProviderSend mailProviderSend(TaskExecutor taskExecutor) {
MailProviderConfig config = getMailProviderConfig();
if (Objects.isNull(config)) {
return null;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return new DefaultMailProviderSendImpl(config, taskExecutor);
}
}

@ -34,11 +34,14 @@ import cn.topiam.employee.common.constant.SettingConstants;
import cn.topiam.employee.common.entity.setting.SettingEntity;
import cn.topiam.employee.common.entity.setting.config.SmsConfig;
import cn.topiam.employee.common.jackjson.encrypt.EncryptContextHelp;
import cn.topiam.employee.common.jackjson.encrypt.EncryptionModule;
import cn.topiam.employee.common.message.enums.SmsProvider;
import cn.topiam.employee.common.message.mail.MailProviderConfig;
import cn.topiam.employee.common.message.sms.aliyun.AliyunSmsProviderConfig;
import cn.topiam.employee.common.message.sms.qiniu.QiNiuSmsProviderConfig;
import cn.topiam.employee.common.message.sms.tencent.TencentSmsProviderConfig;
import cn.topiam.employee.common.repository.setting.SettingRepository;
import cn.topiam.employee.core.setting.constant.MessageSettingConstants;
import cn.topiam.employee.core.setting.constant.SecuritySettingConstants;
import cn.topiam.employee.support.context.ApplicationContextHelp;
import cn.topiam.employee.support.exception.TopIamException;
@ -60,7 +63,7 @@ public class SettingHelp {
/**
*
*
* @return {@link Boolean}
* @return {@link SmsConfig}
*/
public static SmsConfig getSmsProviderConfig() {
SettingEntity setting = getSettingRepository().findByName(MESSAGE_SMS_PROVIDER);
@ -101,6 +104,33 @@ public class SettingHelp {
return new SmsConfig();
}
/**
*
*
* @return {@link MailProviderConfig}
*/
public static MailProviderConfig getMailProviderConfig() {
try {
SettingEntity setting = getSettingRepository()
.findByName(MessageSettingConstants.MESSAGE_PROVIDER_EMAIL);
if (!Objects.isNull(setting)
&& !SettingConstants.NOT_CONFIG.equals(setting.getValue())) {
String value = setting.getValue();
ObjectMapper objectMapper = EncryptionModule.deserializerDecrypt();
// 指定序列化输入的类型
objectMapper.activateDefaultTyping(objectMapper.getPolymorphicTypeValidator(),
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
// 根据提供商序列化
MailProviderConfig config = objectMapper.readValue(value, MailProviderConfig.class);
return config;
}
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
return null;
}
/**
*
*

@ -20,6 +20,7 @@ package cn.topiam.employee.core.message.mail;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
@ -29,13 +30,16 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import cn.topiam.employee.common.enums.MailType;
import cn.topiam.employee.common.enums.MessageCategory;
import cn.topiam.employee.common.message.enums.MessageType;
import cn.topiam.employee.common.message.mail.MailProviderConfig;
import cn.topiam.employee.core.mq.NoticeMessagePublisher;
import cn.topiam.employee.support.context.ApplicationContextHelp;
import cn.topiam.employee.support.exception.TopIamException;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import static cn.topiam.employee.core.help.SettingHelp.getCodeValidTime;
import static cn.topiam.employee.core.help.SettingHelp.getMailProviderConfig;
import static cn.topiam.employee.core.message.MsgVariable.*;
import static cn.topiam.employee.support.constant.EiamConstants.DEFAULT_DATE_TIME_FORMATTER;
@ -77,6 +81,11 @@ public class MailMsgEventPublish {
*/
@SneakyThrows
public void publish(MailType type, String receiver, Map<String, Object> parameter) {
MailProviderConfig config = getMailProviderConfig();
if (Objects.isNull(config)) {
throw new TopIamException("未配置邮件服务");
}
if (StringUtils.isBlank(receiver)) {
log.warn("发送邮件通知失败, 接受者为空, type: {}", type);
return;

Loading…
Cancel
Save