Commit db54dd3b authored by qinxunjia's avatar qinxunjia

对外接口异常处理

parent 2c61d539
......@@ -23,8 +23,12 @@ public class ChuangLanApi {
@PostMapping("/notify")
public String notify(@RequestParam CLNotifyRequest notifyInfo) {
log.info("创蓝异步通知接口入参:{}", JSONObject.toJSONString(notifyInfo));
chuangLanSmsService.asyncNotify(notifyInfo);
String response = "success";
try {
chuangLanSmsService.asyncNotify(notifyInfo);
} catch (Exception e) {
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response);
return response;
}
......@@ -32,8 +36,12 @@ public class ChuangLanApi {
@GetMapping("/report")
public String report(ReportDto report) {
log.info("回送报告接口入参:{}", JSONObject.toJSONString(report));
CLBizResponse responses = chuangLanSmsService.report(report);
String response = "success";
try {
CLBizResponse responses = chuangLanSmsService.report(report);
} catch (Exception e) {
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response);
return response;
}
......
......@@ -2,6 +2,7 @@ package com.jz.sms.channel.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.channel.dto.*;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -27,7 +28,12 @@ public class DmHubApi {
@PostMapping("/sms/template")
public DmHubTemplateResponse template(@RequestBody DmHubTemplateRequest params) {
log.info("**********创建模板接口入参*******:{}", JSONObject.toJSONString(params));
DmHubTemplateResponse template = messageService.createTemplate(params);
DmHubTemplateResponse template;
try {
template = messageService.createTemplate(params);
} catch (Exception e) {
template = new DmHubTemplateResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********创建模板接口出参*******:{}", JSONObject.toJSONString(template));
return template;
}
......@@ -41,8 +47,13 @@ public class DmHubApi {
@PostMapping("/sms/batch")
public DmHubSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response = messageService.batchSend(request);
// log.info("**********批量发送入参*******:{}", response);
DmHubSendResponse response;
try {
response = messageService.batchSend(request);
} catch (Exception e) {
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********批量发送出参*******:{}", response);
return response;
}
......@@ -54,9 +65,14 @@ public class DmHubApi {
*/
@RequestMapping("/sms/send")
public DmHubSendResponse send(@RequestBody DmHubSendRequest request) {
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response = messageService.send(request);
DmHubSendResponse response;
try {
response = messageService.send(request);
} catch (Exception e) {
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********单条发送出参*******:{}", JSONObject.toJSONString(response));
return response;
}
......
......@@ -15,7 +15,7 @@ public interface ChuangLanSmsService {
CLBizResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception;
CLBizResponse asyncNotify(CLNotifyRequest notifyInfo);
CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception;
CLBizResponse report(ReportDto report);
CLBizResponse report(ReportDto report) throws Exception;
}
......@@ -98,7 +98,6 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
throw new Exception("123");
}
content = "【九章数据】" + content;
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, sendParams, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
......@@ -139,10 +138,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
log.info("发送短信的模式为:{未知类型},type:{}", type);
throw new Exception("123");
}
content = "【九章数据】" + content;
CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送变量内容短信,请求上游数据为:{},\r\n请求地址为:{}", batchId, requestJson, ChuangLanSMSConfig.sendVariable);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendVariable, requestJson);
......@@ -170,7 +166,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override
public CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) {
public CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception {
String action = notifyInfo.getAction();
if (!"messageModel".equals(action)) {
return new CLBizResponse(ResponseCode.UPSTREAM_UNKNOWN_MODEL);
......@@ -188,7 +184,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override
public CLBizResponse report(ReportDto report) {
public CLBizResponse report(ReportDto report) throws Exception{
// TODO 状态报告要特殊处理,一个个接受,批量发送
String uid = report.getUid();
Map<String, Object> ret = dmHubService.smsReport(report);
......@@ -196,16 +192,4 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
}
public static void main(String[] args) {
String s = "\"亲爱的${name@0!\"\"},你当前是${stage@10!\"\"},请点击链接升级 ${surl!'abc.ddd.cvdfa?fadfadfihjfiahkck?daa=${acd}'} 回TD退订\"";
String p = "((?:\\$\\{surl.*'\\}))|\\$\\{[^}]+\\}";
Pattern pt = Pattern.compile(p);
Matcher matcher = pt.matcher(s);
while (matcher.find()) {
String group = matcher.group();
System.err.println(group);
}
System.err.println(11);
}
}
......@@ -30,12 +30,11 @@ public class DmHubConfig {
public void setTokenUrl(String tokenUrl) {
DmHubConfig.tokenUrl = tokenUrl;
}
@Value("{system.config.dmHub.report}")
public void setReport(String report) {
DmHubConfig.report = report;
}
}
......@@ -11,7 +11,7 @@ public interface DmHubService {
*
* @return token信息
*/
String getToken();
String getToken() throws Exception;
/**
......@@ -20,5 +20,5 @@ public interface DmHubService {
* @param dto
* @return
*/
Map<String, Object> smsReport(ReportDto dto);
Map<String, Object> smsReport(ReportDto dto) throws Exception;
}
......@@ -8,6 +8,7 @@ import com.jz.sms.channel.dto.DmhubReport;
import com.jz.sms.channel.dto.ReportDetail;
import com.jz.sms.util.HttpUtil;
import com.jz.sms.util.OkHttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -26,12 +27,19 @@ public class DmHubServiceImpl implements DmHubService {
private static final Logger log = LoggerFactory.getLogger(DmHubServiceImpl.class);
private static final String TOKEN_KEY = "DM_HUB_API_TOKEN";
@Autowired
RedisTemplate redisTemplate;
@Override
public String getToken() {
public String getToken() throws Exception {
String token = redisTemplate.opsForValue().get(TOKEN_KEY).toString();
Long expire = redisTemplate.opsForValue().getOperations().getExpire(TOKEN_KEY);
if (expire != null && expire > 0L) {
return token;
} else {
String appid = DmHubConfig.applicationId;
String secret = DmHubConfig.applicationKey;
String tokenUrl = DmHubConfig.tokenUrl;
......@@ -47,20 +55,24 @@ public class DmHubServiceImpl implements DmHubService {
if ("0".equals(errorCode)) {
String accessToken = retJson.getString("access_token");
String expires = retJson.getString("expires_in");
// TODO 存redis,过期时间=expires-600(秒)
redisTemplate.opsForValue().set("DM_HUB_TOKEN", accessToken, Long.parseLong(expires) - 600L, TimeUnit.SECONDS);
return accessToken;
} else {
// TODO 系统告警,获取API接口TOKEN失败。
return null;
}
}
}
@Override
public Map<String, Object> smsReport(ReportDto dto) {
DmhubReport report = new DmhubReport();
public Map<String, Object> smsReport(ReportDto dto) throws Exception {
String token = getToken();
if (StringUtils.isBlank(token)) {
return null;
}
DmhubReport report = new DmhubReport();
report.setAccess_token(token);
report.setType("sms");
ReportDetail detail = new ReportDetail();
detail.setAudienceId(dto.getMobile());
......@@ -79,6 +91,7 @@ public class DmHubServiceImpl implements DmHubService {
log.info("短信状态回执参数:{}", JSONObject.toJSONString(report));
String retStr = HttpUtil.sendPost(DmHubConfig.report, JSONObject.toJSONString(report));
log.info("短信状态回执响应:{}", retStr);
//TODO 目前没有测试环境的请求地址
return null;
}
}
package com.jz.sms.channel.dto;
import com.jz.sms.config.ResponseCode;
public class DmHubTemplateResponse {
private String code;
private String msg;
......@@ -13,6 +15,11 @@ public class DmHubTemplateResponse {
this.msg = msg;
}
public DmHubTemplateResponse(ResponseCode responseCode) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg();
}
public String getCode() {
return code;
......
......@@ -6,6 +6,7 @@ public class DmhubReport implements Serializable {
private static final long serialVersionUID = 810019212627996764L;
private String type;
private String access_token;
private ReportDetail reports;
......@@ -24,4 +25,12 @@ public class DmhubReport implements Serializable {
public void setReports(ReportDetail reports) {
this.reports = reports;
}
public String getAccess_token() {
return access_token;
}
public void setAccess_token(String access_token) {
this.access_token = access_token;
}
}
......@@ -92,7 +92,7 @@ public class MessageServiceImpl implements MessageService {
SmsTemplateInfo updateInfo = new SmsTemplateInfo();
updateInfo.setId(info.getId());
updateInfo.setUpTemplateId(id);
smsTemplateService.updateById(updateInfo);
boolean update = smsTemplateService.updateForSet("up_template_id = " + templateId, new EntityWrapper<SmsTemplateInfo>().eq("id", info.getId()));
return responseDTO;
} else {
//TODO 发送告警,模板添加失败。
......
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