Commit 9eb6393b authored by qinxunjia's avatar qinxunjia

规范接口

parent 561456b1
......@@ -79,6 +79,22 @@
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
<build>
<finalName>dmhub-plugin</finalName>
......
......@@ -8,8 +8,6 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
@RestController
@RequestMapping("/test")
public class BizController {
......@@ -41,9 +39,9 @@ public class BizController {
* @return
*/
@PostMapping("/sms/batch")
public DmHubBatchSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
public DmHubSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
DmHubBatchSendResponse response = messageService.batchSend(request);
DmHubSendResponse response = messageService.batchSend(request);
// log.info("**********批量发送入参*******:{}", response);
return response;
}
......
package com.jz.sms.api;
import com.jz.sms.chuanglan.dto.CLBizResponse;
import com.jz.sms.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.chuanglan.dto.ReportDto;
import com.jz.sms.chuanglan.service.ChuangLanSmsService;
......@@ -40,8 +41,7 @@ public class ChuangLanApi {
public String report(@RequestParam ReportDto report) {
log.info("回送报告接口入参:{}", report);
chuangLanSmsService.report(report);
CLBizResponse responses = chuangLanSmsService.report(report);
String response = "success";
log.info("创蓝异步通知接口出参:{}", response);
......
package com.jz.sms.chuanglan.dto;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.config.ResponseCode;
import java.io.Serializable;
public class CLTemplateResponse implements Serializable {
public class CLBizResponse implements Serializable {
private String code;
......@@ -13,28 +14,40 @@ public class CLTemplateResponse implements Serializable {
private String id;
public CLTemplateResponse() {
private JSONObject data;
public CLBizResponse() {
}
public CLTemplateResponse(ResponseCode responseCode, String id) {
public CLBizResponse(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
}
public CLBizResponse(ResponseCode responseCode,JSONObject data) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
this.data = data;
}
public CLBizResponse(ResponseCode responseCode, String id) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
this.id = id;
}
public CLTemplateResponse(ResponseCode responseCode, String msg,int nullInfo) {
public CLBizResponse(ResponseCode responseCode, String msg, int nullInfo) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg()+msg;
}
public CLTemplateResponse(String code, String msg) {
public CLBizResponse(String code, String msg) {
this.code = code;
this.msg = msg;
}
public CLTemplateResponse(String code, String msg, String id) {
public CLBizResponse(String code, String msg, String id) {
this.code = code;
this.msg = msg;
this.id = id;
......@@ -61,6 +74,15 @@ public class CLTemplateResponse implements Serializable {
}
public void setId(String id) {
this.id = id;
}
public JSONObject getData() {
return data;
}
public void setData(JSONObject data) {
this.data = data;
}
}
......@@ -18,6 +18,10 @@ public class CLSendSMSResponse {
*/
private String code;
private String failNum;
private String successNum;
public CLSendSMSResponse(){}
......@@ -58,6 +62,22 @@ public class CLSendSMSResponse {
this.code = code;
}
public String getFailNum() {
return failNum;
}
public void setFailNum(String failNum) {
this.failNum = failNum;
}
public String getSuccessNum() {
return successNum;
}
public void setSuccessNum(String successNum) {
this.successNum = successNum;
}
@Override
public String toString() {
return "SmsSingleResponse [time=" + time + ", msgId=" + msgId + ", errorMsg=" + errorMsg + ", code=" + code
......
......@@ -58,6 +58,16 @@ public class CLSendVariableRequest {
this.report = report;
}
public CLSendVariableRequest(String account, String password, String msg, String params, String report,String uid) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.params = params;
this.report = report;
this.uid = uid;
}
// public SmsVarableRequest(String account, String password, String msg, String params, String sendtime) {
// super();
// this.account = account;
......
......@@ -2,22 +2,21 @@ package com.jz.sms.chuanglan.service;
import com.jz.sms.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.chuanglan.dto.CLSendSMSResponse;
import com.jz.sms.chuanglan.dto.CLTemplateResponse;
import com.jz.sms.chuanglan.dto.CLBizResponse;
import com.jz.sms.chuanglan.dto.ReportDto;
import com.jz.sms.dto.*;
public interface ChuangLanSmsService {
CLTemplateResponse createTemplate(String content, String type) throws Exception;
CLBizResponse createTemplate(String content, String type) throws Exception;
CLSendSMSResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception;
CLBizResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception;
CLSendSMSResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception;
CLBizResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception;
void asyncNotify(CLNotifyRequest notifyInfo);
CLBizResponse asyncNotify(CLNotifyRequest notifyInfo);
void report(ReportDto report);
CLBizResponse report(ReportDto report);
}
......@@ -10,7 +10,6 @@ import com.jz.sms.chuanglan.service.ChuangLanSmsService;
import com.jz.sms.chuanglan.utils.ChuangLanSmsUtil;
import com.jz.sms.chuanglan.config.ChuangLanSMSConfig;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.dto.*;
import com.jz.sms.repository.domain.SmsTemplateInfo;
import com.jz.sms.service.SmsTemplateService;
import com.jz.sms.util.HttpUtil;
......@@ -22,7 +21,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
......@@ -42,7 +40,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
private SmsTemplateService templateService;
@Override
public CLTemplateResponse createTemplate(String content, String type) throws Exception {
public CLBizResponse createTemplate(String content, String type) throws Exception {
int i = 1 / 0;
log.info("进入创蓝创建短信接口模板的逻辑:{}", content);
Map<String, Object> requestMap = new HashMap<>();
......@@ -56,7 +54,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
} else {
// TODO 自定义异常,发送警告
log.error("错误,传入的模板类型未知,类型为:{}", type);
return new CLTemplateResponse(ResponseCode.SYSTEM_ERROR, "未知的模板类型:" + type, 0);
return new CLBizResponse(ResponseCode.SYSTEM_ERROR, "未知的模板类型:" + type, 0);
}
content = content.replaceAll("回TD退订", "");
......@@ -75,16 +73,15 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
String status = retJson.getString("status");
if (SUCCESS.equals(status)) {
JSONObject data = retJson.getJSONObject("data");
String id = data.getString("id");
return new CLTemplateResponse(ResponseCode.SUCCESS, id);
return new CLBizResponse(ResponseCode.SUCCESS, data);
} else {
String msg = retJson.getString("msg");
return new CLTemplateResponse(ResponseCode.UPSTREAM_FAIL.getCode(), msg);
return new CLBizResponse(ResponseCode.UPSTREAM_FAIL.getCode(), msg);
}
}
@Override
public CLSendSMSResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception {
public CLBizResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
......@@ -109,14 +106,24 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
if (StringUtils.isBlank(responseStr)) {
return new CLSendSMSResponse("500", "渠道同步响应为空");
return new CLBizResponse(ResponseCode.UPSTREAM_BLANK);
}
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
return response;
String code = response.getCode();
String errorMsg = response.getErrorMsg();
String msgId = response.getMsgId();
if (!"0".equals(code)) {
return new CLBizResponse(ResponseCode.UPSTREAM_FAIL.getCode(), errorMsg);
} else {
JSONObject data = new JSONObject();
data.put("msgId", msgId);
return new CLBizResponse(ResponseCode.SUCCESS, data);
}
}
@Override
public CLSendSMSResponse sendByVariable(String batchId, String content, String params, String type) throws Exception {
public CLBizResponse sendByVariable(String batchId, String content, String params, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
......@@ -133,43 +140,58 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
}
content = "【九章数据】" + content;
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, params, "true", batchId);
CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson,ChuangLanSMSConfig.sendVariable);
log.info("【{}】发送固定内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendVariable, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, responseStr);
if (StringUtils.isBlank(responseStr)) {
return new CLSendSMSResponse("500", "渠道同步响应为空");
return new CLBizResponse(ResponseCode.UPSTREAM_BLANK);
}
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
return response;
String code = response.getCode();
String errorMsg = response.getErrorMsg();
String successNum = response.getSuccessNum();
String failNum = response.getFailNum();
String msgId = response.getMsgId();
if (!"0".equals(code)) {
return new CLBizResponse(ResponseCode.UPSTREAM_FAIL.getCode(), errorMsg);
} else {
JSONObject data = new JSONObject();
data.put("successNum", successNum);
data.put("failNum", failNum);
data.put("msgId", msgId);
return new CLBizResponse(ResponseCode.SUCCESS, data);
}
}
@Override
public void asyncNotify(CLNotifyRequest notifyInfo) {
public CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) {
String action = notifyInfo.getAction();
if (!"messageModel".equals(action)) {
return;
return new CLBizResponse(ResponseCode.UPSTREAM_UNKNOWN_MODEL);
}
String id = notifyInfo.getId();
String status = notifyInfo.getAuditStatus();
if (!"1".equals(status)) {
String msg = notifyInfo.getAuditReason();
boolean update = templateService.updateForSet("status = abnormal,up_reject_msg = " + msg, new EntityWrapper<SmsTemplateInfo>().eq("up_template_id", id));
// TODO 发送钉钉告警
log.error("短信模板审核不通过,原因:{}", msg);
}
return new CLBizResponse(ResponseCode.SUCCESS);
}
@Override
public void report(ReportDto report) {
public CLBizResponse report(ReportDto report) {
String uid = report.getUid();
return null;
}
......
package com.jz.sms.config;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
public class OkHttpConfig {
@Bean
public OkHttpClient okHttpClient() {
return new OkHttpClient().newBuilder().retryOnConnectionFailure(false).connectionPool(pool())
.connectTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS)
.build();
}
@Bean
public ConnectionPool pool() {
return new ConnectionPool(50, 5, TimeUnit.MINUTES);
}
}
......@@ -2,9 +2,12 @@ package com.jz.sms.config;
public enum ResponseCode {
SUCCESS("200", "处理成功"),
UPSTREAM_FAIL("UF01", "渠道交易失败"),
UPSTREAM_FAIL("US01", "渠道交易失败"),
UPSTREAM_UNKNOWN_MODEL("US01", "未知通知模式"),
UPSTREAM_BLANK("US00", "渠道同步响应为空"),
SYSTEM_ERROR("99", "系统异常:");
private String code;
private String msg;
......@@ -17,6 +20,7 @@ public enum ResponseCode {
return code;
}
public String getMsg() {
return msg;
}
......
package com.jz.sms.dto;
public class DmHubBatchSendResponse {
private String code;
private String msg;
public DmHubBatchSendResponse() {
}
public DmHubBatchSendResponse(String code, String msg) {
this.code = code;
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
package com.jz.sms.dto;
import com.jz.sms.config.ResponseCode;
public class DmHubSendResponse {
private String code;
private String msg;
public DmHubSendResponse() {
}
public DmHubSendResponse(String code, String msg) {
this.code = code;
this.msg = msg;
}
public DmHubSendResponse(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
}
......@@ -26,5 +26,5 @@ public interface MessageService {
* @param requestDTO
* @return
*/
DmHubBatchSendResponse batchSend(DmHubBatchSendRequest requestDTO);
DmHubSendResponse batchSend(DmHubBatchSendRequest requestDTO);
}
......@@ -3,8 +3,7 @@ package com.jz.sms.service.impl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jz.sms.chuanglan.dto.CLSendSMSResponse;
import com.jz.sms.chuanglan.dto.CLTemplateResponse;
import com.jz.sms.chuanglan.dto.CLBizResponse;
import com.jz.sms.chuanglan.service.ChuangLanSmsService;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.dto.*;
......@@ -87,7 +86,7 @@ public class MessageServiceImpl implements MessageService {
//TODO 发送告警
}
String chaunglanCreate = dmHub2ChuangLanCreate(chuanglanSend);
CLTemplateResponse template = chuangLanSmsService.createTemplate(chaunglanCreate, smsType);
CLBizResponse template = chuangLanSmsService.createTemplate(chaunglanCreate, smsType);
if (ResponseCode.SUCCESS.getCode().equals(template.getCode())) {
String id = template.getId();
SmsTemplateInfo updateInfo = new SmsTemplateInfo();
......@@ -223,27 +222,39 @@ public class MessageServiceImpl implements MessageService {
sysBatchInfo.setLastUpdated(new Date());
boolean insert = sysBatchService.insert(sysBatchInfo);
CLBizResponse response;
// 判断模板是固定短信还是变量短信
if (paramsArr == null || paramsArr.isEmpty()) {
CLSendSMSResponse chuanglanResponse = chuangLanSmsService.sendByFixed(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
response = chuangLanSmsService.sendByFixed(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
String code = response.getCode();
String msg = response.getMsg();
// TODO 根据DM hub需要的返回数据封装
if (ResponseCode.SUCCESS.getCode().equals(code)) {
return new DmHubSendResponse(code, msg);
} else {
// 4、封裝數據
CLSendSMSResponse response = chuangLanSmsService.sendByVariable(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
return new DmHubSendResponse(code, msg);
}
} else {
response = chuangLanSmsService.sendByVariable(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
String code = response.getCode();
String msg = response.getMsg();
// TODO 根据DM hub需要的返回数据封装
if (ResponseCode.SUCCESS.getCode().equals(code)) {
return new DmHubSendResponse(code, msg);
} else {
return new DmHubSendResponse(code, msg);
}
}
} catch (Exception e) {
log.error("调用创蓝逻辑错误:{}", e);
return new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
return null;
}
@Override
public DmHubBatchSendResponse batchSend(DmHubBatchSendRequest request) {
public DmHubSendResponse batchSend(DmHubBatchSendRequest request) {
String batchId = request.getBatchId();
String templateId = request.getTemplateId();
......@@ -252,7 +263,7 @@ public class MessageServiceImpl implements MessageService {
SmsTemplateInfo templateInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId));
if (null == templateInfo) {
return new DmHubBatchSendResponse("500", "模板不存在");
return new DmHubSendResponse("500", "模板不存在");
}
// 模板参数占位符
......@@ -310,24 +321,41 @@ public class MessageServiceImpl implements MessageService {
info.setDateCreated(new Date());
info.setLastUpdated(new Date());
sysBatchService.insert(info);
CLBizResponse response;
if (paramsArr == null || paramsArr.isEmpty()) {
try {
CLSendSMSResponse response = chuangLanSmsService.sendByFixed(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
response = chuangLanSmsService.sendByFixed(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
String code = response.getCode();
String msg = response.getMsg();
// TODO 根据DM hub需要的返回数据封装
if (ResponseCode.SUCCESS.getCode().equals(code)) {
return new DmHubSendResponse(code, msg);
} else {
return new DmHubSendResponse(code, msg);
}
} catch (Exception e) {
log.error("调用创蓝逻辑错误:{}", e);
return null;
// TODO 根据DM hub需要的返回数据封装
return new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
} else {
try {
CLSendSMSResponse response = chuangLanSmsService.sendByVariable(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
response = chuangLanSmsService.sendByVariable(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
String code = response.getCode();
String msg = response.getMsg();
// TODO 根据DM hub需要的返回数据封装
if (ResponseCode.SUCCESS.getCode().equals(code)) {
return new DmHubSendResponse(code, msg);
} else {
return new DmHubSendResponse(code, msg);
}
} catch (Exception e) {
log.error("调用创蓝逻辑错误:{}", e);
return null;
return new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
}
return null;
}
public String test(Object value, StringBuilder keyBuffer) {
......
package com.jz.sms.util;
import com.baomidou.mybatisplus.toolkit.MapUtils;
import com.jz.sms.api.BizController;
import okhttp3.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Iterator;
import java.util.Map;
@Component
public class OkHttpUtil {
private static final Logger log = LoggerFactory.getLogger(OkHttpUtil.class);
private static OkHttpClient okHttpClient;
@Autowired
public OkHttpUtil(OkHttpClient okHttpClient) {
OkHttpUtil.okHttpClient = okHttpClient;
}
/**
* get
*
* @param url 请求的url
* @param queries 请求的参数,在浏览器?后面的数据,没有可以传null
* @return
*/
public static String get(String url, Map<String, String> queries) {
String responseBody = "";
StringBuffer sb = new StringBuffer(url);
if (MapUtils.isNotEmpty(queries)) {
boolean firstFlag = true;
Iterator iterator = queries.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = (Map.Entry<String, String>) iterator.next();
if (firstFlag) {
sb.append("?" + entry.getKey() + "=" + entry.getValue());
firstFlag = false;
} else {
sb.append("&" + entry.getKey() + "=" + entry.getValue());
}
}
}
Request request = new Request.Builder().url(sb.toString()).build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
//int status = response.code();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
log.error("okhttp3 put error >> ex = {}", e);
} finally {
if (response != null) {
response.close();
}
}
return responseBody;
}
/**
* Post请求发送JSON数据....{"name":"zhangsan","pwd":"123456"} 参数一:请求Url 参数二:请求的JSON
* 参数三:请求回调
*/
public static String postJsonParams(String url, String jsonParams) {
String responseBody = "";
RequestBody requestBody = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonParams);
Request request = new Request.Builder().url(url).post(requestBody).build();
Response response = null;
try {
response = okHttpClient.newCall(request).execute();
// int status = response.code();
if (response.isSuccessful()) {
return response.body().string();
}
} catch (Exception e) {
log.error("okhttp3 post error >> ex = {}", e);
} finally {
if (response != null) {
response.close();
}
}
return responseBody;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment