Browse Source

修改邮件发送服务

wangyc 7 years ago
parent
commit
d910b3da1f

+ 4 - 7
src/main/java/com/uas/ps/message/Application.java

@@ -1,19 +1,16 @@
 package com.uas.ps.message;
 
 import com.uas.ps.core.util.ContextUtils;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.PrintStream;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
-import org.springframework.boot.autoconfigure.domain.EntityScan;
 import org.springframework.boot.context.event.ApplicationPreparedEvent;
 import org.springframework.context.ApplicationListener;
-import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
 import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.PrintStream;
-
 
 /**
  * 应用入口

+ 19 - 0
src/main/java/com/uas/ps/message/RestTemplateConfiguration.java

@@ -0,0 +1,19 @@
+package com.uas.ps.message;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.client.RestTemplate;
+
+/**
+ * RestTemplate 配置类
+ * @author hulh
+ */
+@Configuration
+public class RestTemplateConfiguration {
+
+    @Bean
+    public RestTemplate servletRegistrationBean() {
+        return new RestTemplate();
+    }
+
+}

+ 35 - 15
src/main/java/com/uas/ps/message/service/impl/MessageServiceImpl.java

@@ -1,6 +1,7 @@
 package com.uas.ps.message.service.impl;
 
 import com.alibaba.fastjson.JSONObject;
+import com.uas.message.mail.domain.MailLog;
 import com.uas.message.mail.service.MailService;
 import com.uas.ps.message.dao.AppDao;
 import com.uas.ps.message.dao.MessageDao;
@@ -27,9 +28,14 @@ import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpEntity;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
 import org.springframework.stereotype.Service;
 import org.springframework.util.CollectionUtils;
 import org.springframework.util.StringUtils;
+import org.springframework.web.client.RestTemplate;
 
 /**
  * Created by wangyc on 2018/1/13.
@@ -43,23 +49,36 @@ public class MessageServiceImpl implements MessageService {
 
     private final AppDao appDao;
 
-    private final MailService mailService;
+    private RestTemplate restTemplate;
 
     private static final String EMAIL_REGEX = "\\w[-\\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\\.)+[A-Za-z]{2,14}";
 
     private static final String TEL_REGEXP = "^((\\(\\d{3}\\))|(\\d{3}\\-))?(13|15|18|17)\\d{9}$";
 
-    private final static String PROD_URL = "http://113.105.74.140:8092/tigase/baiduPush";
+    private static final String PROD_URL = "http://113.105.74.140:8092/tigase/baiduPush";
+    /**
+     * 邮件服务主机地址
+     */
+//    @Value("#{sys.messageServiceIp ?: 'http://message.ubtob.com/'}")
+    private static final String MAIL_CONSOLE_HOST = "http://message.ubtob.com/";
 
+    /**
+     * 发送邮件给单个人url
+     */
+    private static final String MAIL_SEND_URL = "mail/send";
+
+    /**
+     * 发送邮件给多个人 url
+     */
+    private static final String MAIL_SEND_MANY_URL = "mail/send/o2m";
     /**
      * 短信接口
      */
     private final String messageUrl = "http://message.ubtob.com/sms/send";
 
     @Autowired
-    public MessageServiceImpl(MessageDao messageDao, AppDao appDao, MailService mailService) {
+    public MessageServiceImpl(MessageDao messageDao, AppDao appDao) {
         this.messageDao = messageDao;
-        this.mailService = mailService;
         this.appDao = appDao;
     }
 
@@ -252,17 +271,18 @@ public class MessageServiceImpl implements MessageService {
      * @param message 消息
      */
     private void sendMail(Message message) {
-        Map<String, Object> model = new HashMap<>();
-        // TODO 账户中心获取用户邮箱
-        String templetId = "";
-        User user = FastjsonUtils.fromJson(message.getReceiver(), User.class);
-        if (user.getUserEmail() != null && user.getUserEmail().matches(EMAIL_REGEX)) {
-            try {
-                mailService.send(templetId, user.getUserEmail(), model);
-            } catch (Exception e) {
-                e.printStackTrace();
-            }
-        }
+        HttpHeaders headers = new HttpHeaders();
+        MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
+        headers.setContentType(type);
+        headers.add("Accept", MediaType.APPLICATION_JSON.toString());
+
+        JSONObject object = new JSONObject();
+        object.put("templateId", message.getMailTemplate());
+        object.put("receiver", message.getReceiver());
+        object.put("params", message.getContent());
+
+        HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
+        ResponseEntity<MailLog> responseEntity = restTemplate.postForEntity(MAIL_CONSOLE_HOST + MAIL_SEND_URL, formEntity, MailLog.class);
     }
 
     /**

+ 12 - 13
src/main/resources/application.yml

@@ -1,21 +1,20 @@
 spring:
-  profiles:
-      active: test
   jpa:
-  database: MYSQL
-  show-sql: false
-  properties:
-   hibernate:
-    dialect: org.hibernate.dialect.MySQL5Dialect
-    cache:
-     region:
-      factory_class: org.hibernate.cache.ehcache.EhCacheRegionFactory
-    hbm2ddl:
-     auto: update
+    database: MYSQL
+    show-sql: false
+    properties:
+      hibernate:
+        dialect: org.hibernate.dialect.MySQL5Dialect
+        cache:
+         use_second_level_cache: true
+         region:
+          factory_class: org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
+        hbm2ddl:
+         auto: update
 
 security:
   basic:
-    enabled: true
+    enabled: false
     path: /**
   user:
     name: message-admin