From 73307b0ada907f8ac857b3dfc4e7d50e3b7b92e9 Mon Sep 17 00:00:00 2001 From: Tomas <tomasysh@gmail.com> Date: 星期一, 24 一月 2022 16:24:52 +0800 Subject: [PATCH] Merge branch 'Phase3' of https://dev.pollex.com.tw:8443/r/pcalife/PAM into Phase3 --- pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java | 63 ++++++++++++++++++++++++------- 1 files changed, 49 insertions(+), 14 deletions(-) diff --git a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java index 05d8d73..a5be71e 100644 --- a/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java +++ b/pamapi/src/main/java/com/pollex/pam/service/SendMsgService.java @@ -2,7 +2,10 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.pollex.pam.config.ApplicationProperties; +import com.pollex.pam.config.ApplicationProperties.Email; import com.pollex.pam.config.ApplicationProperties.SMS; +import com.pollex.pam.config.Constants; +import com.pollex.pam.enums.SendEmailMsgMethod; import com.pollex.pam.repository.ConsultantRepository; import com.pollex.pam.service.dto.*; import com.pollex.pam.service.util.HttpRequestUtil; @@ -11,9 +14,12 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.env.Environment; +import org.springframework.core.env.Profiles; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; import org.thymeleaf.spring5.SpringTemplateEngine; +import tech.jhipster.config.JHipsterConstants; import java.nio.charset.StandardCharsets; import java.time.LocalDateTime; @@ -38,13 +44,19 @@ @Autowired SpringTemplateEngine springTemplateEngine; + @Autowired + Environment environment; + + @Autowired + MailService mailService; + public SendSMSResponse sendMsgBySMS(String toMobile, String content) throws SendSMSFailException { - if(!applicationProperties.isSendNotifyMsg()) { + + SMS smsProperties = applicationProperties.getSms(); + if(!smsProperties.isSendNotifyMsg()) { // return getMockSMSResponse(); - return null; - } - - SMS smsProperties = applicationProperties.getSms(); + return null; + } SendSMSRequest sendSMSRequest = new SendSMSRequest(); sendSMSRequest.setpKey(UUID.randomUUID().toString()); @@ -94,7 +106,7 @@ public String sendMsgByEmail(String toAddress, String subject, String content, boolean htmlFormat, List<String> toCCAddress, List<String> attachments) throws SendEmailFailException { String fromAddress = applicationProperties.getEmail().getSenderEmail(); - + SendMailRequest sendMailRequest = new SendMailRequest(); sendMailRequest.setSendMailAddresses(Collections.singletonList(toAddress)); sendMailRequest.setFrom(fromAddress); @@ -109,25 +121,48 @@ } public String sendMsgByEmail(SendMailRequest sendMailRequest) throws SendEmailFailException{ - if(!applicationProperties.isSendNotifyMsg()) { - return null; - } - try { + final Email emailProperties = applicationProperties.getEmail(); + + if(!emailProperties.isSendNotifyMsg()) { + return null; + } + + if(emailProperties.getMethod() == SendEmailMsgMethod.POLLEX_GMAIL) { + return sendMsgByPollexGmail(sendMailRequest); + } + else if(emailProperties.getMethod() == SendEmailMsgMethod.PAM_EMAIL_SERVICE) { + return sendMsgByPamEmailService(sendMailRequest); + } + + return null; + } + + private String sendMsgByPollexGmail(SendMailRequest sendMailRequest) { + String subject = sendMailRequest.getSubject(); + String content = sendMailRequest.getContent(); + boolean isHtml = sendMailRequest.isHtmlFormat(); + sendMailRequest.getSendMailAddresses().forEach(receiver -> mailService.sendEmail(receiver, subject, content, false, isHtml)); + + return null; + } + + private String sendMsgByPamEmailService(SendMailRequest sendMailRequest) { + final Email emailProperties = applicationProperties.getEmail(); + try { ResponseEntity<String> responseEntity = - HttpRequestUtil.postWithJson( applicationProperties.getEmail().getUrl(), sendMailRequest, String.class); + HttpRequestUtil.postWithJson(emailProperties.getUrl(), sendMailRequest, String.class); log.debug("responseEntity = {}", responseEntity); String rawResponseString = responseEntity.getBody(); SendMailResponse sendMailResponse = new ObjectMapper().readValue(rawResponseString, SendMailResponse.class); log.debug("sendMailResponse = {}", sendMailResponse); - if(sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) { + if (sendMailResponse == null || sendMailResponse.getData() == null || !"ADDED".equalsIgnoreCase(sendMailResponse.getData().getMessageStatus())) { throw new SendEmailFailException("send email service return error msg! raw response string= " + rawResponseString); } return responseEntity.getBody(); - } - catch (SendEmailFailException e) { + } catch (SendEmailFailException e) { throw e; } catch (Exception e) { log.warn("send email fail by other reason", e); -- Gitblit v1.8.0