|
@@ -0,0 +1,163 @@
|
|
|
|
|
+package com.uas.sso.service.impl;
|
|
|
|
|
+
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
+import com.uas.message.common.domain.Page;
|
|
|
|
|
+import com.uas.message.common.domain.SimpleMessage;
|
|
|
|
|
+import com.uas.message.sms.domain.SmsLog;
|
|
|
|
|
+import com.uas.message.sms.service.SmsService;
|
|
|
|
|
+import com.uas.sso.util.FastjsonUtils;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
|
|
+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.web.client.RestTemplate;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 基于RestTemplate实现的短信息发送接口
|
|
|
|
|
+ * @author suntg
|
|
|
|
|
+ * @since 2017年10月20日15:13:23
|
|
|
|
|
+ */
|
|
|
|
|
+@Service
|
|
|
|
|
+public class RestSmsServiceImpl implements SmsService {
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ private RestTemplate restTemplate;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 短信服务主机地址
|
|
|
|
|
+ */
|
|
|
|
|
+ private String SMS_CONSOLE_HOST = "http://10.10.100.136:8080";
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送短信给单个人url
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String SMS_SEND_URL = "sms/send";
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送短信给多个人 url
|
|
|
|
|
+ */
|
|
|
|
|
+ private static final String SMS_SEND_MANY_URL = "sms/send/o2m";
|
|
|
|
|
+
|
|
|
|
|
+ private final Logger logger = LoggerFactory.getLogger(RestSmsServiceImpl.class);
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<Map<String, Object>> sendByDefault(String[] strings, String s) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public SmsLog send(SimpleMessage simpleMessage) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SmsLog> sendAll(SimpleMessage simpleMessage) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送短信给个人
|
|
|
|
|
+ * @param templateId
|
|
|
|
|
+ * @param receiver
|
|
|
|
|
+ * @param params
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public SmsLog send(String templateId, String receiver, Object[] params) {
|
|
|
|
|
+ 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", templateId);
|
|
|
|
|
+ object.put("receiver", receiver);
|
|
|
|
|
+ object.put("params", params);
|
|
|
|
|
+
|
|
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
|
|
|
|
|
+ logger.warn("短信发送开始, 模板:{},手机号:{}", templateId, receiver);
|
|
|
|
|
+ ResponseEntity<SmsLog> responseEntity = restTemplate.postForEntity(SMS_CONSOLE_HOST + SMS_SEND_URL, formEntity, SmsLog.class);
|
|
|
|
|
+ logger.warn("短信发送成功, 模板:{},手机号:{}", templateId, receiver);
|
|
|
|
|
+ return responseEntity.getBody();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送短信给多个人
|
|
|
|
|
+ * @param templateId
|
|
|
|
|
+ * @param receivers
|
|
|
|
|
+ * @param params
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SmsLog> sendAll(String templateId, Set<String> receivers, Object[] params) {
|
|
|
|
|
+ HttpHeaders headers = new HttpHeaders();
|
|
|
|
|
+ MediaType type = MediaType.parseMediaType("application/json; charset=UTF-8");
|
|
|
|
|
+ headers.setContentType(type);
|
|
|
|
|
+ headers.add("Accept", MediaType.APPLICATION_JSON.toString());
|
|
|
|
|
+
|
|
|
|
|
+ List<SmsLog> mailLogs = new ArrayList<>();
|
|
|
|
|
+ JSONObject object = new JSONObject();
|
|
|
|
|
+ object.put("templateId", templateId);
|
|
|
|
|
+ object.put("params", params);
|
|
|
|
|
+ for (String receiver : receivers) {
|
|
|
|
|
+ object.put("receiver", receiver);
|
|
|
|
|
+ HttpEntity<String> formEntity = new HttpEntity<String>(object.toJSONString(), headers);
|
|
|
|
|
+ ResponseEntity<String> responseEntity = restTemplate.postForEntity(SMS_CONSOLE_HOST + SMS_SEND_URL, formEntity, String.class);
|
|
|
|
|
+ SmsLog smsLog = FastjsonUtils.fromJson(responseEntity.getBody(), SmsLog.class);
|
|
|
|
|
+ mailLogs.add(smsLog);
|
|
|
|
|
+ }
|
|
|
|
|
+ return mailLogs;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送多个发送请求
|
|
|
|
|
+ * @param list
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<SmsLog> sendAll(List<SimpleMessage> list) {
|
|
|
|
|
+ if(! CollectionUtils.isEmpty(list)) {
|
|
|
|
|
+ for(SimpleMessage message : list) {
|
|
|
|
|
+ send(message.getTemplateId(), message.getReceiver(), message.getParams());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<SmsLog> findLogs(int i, int i1) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<SmsLog> findLogs(String s, int i, int i1) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<SmsLog> findLogs(boolean b, int i, int i1) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public Page<SmsLog> findLogs(String s, String s1, Boolean aBoolean, int i, int i1) {
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void deleteLogs(String s) {
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+}
|