Commit 99c7201b authored by qinxunjia's avatar qinxunjia

处理模板变量长度

parent 405e88c0
...@@ -46,7 +46,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService { ...@@ -46,7 +46,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
@Override @Override
public CLBizResponse createTemplate(String content, String type) throws Exception { public CLBizResponse createTemplate(String content, String type) throws Exception {
log.info("进入创蓝创建短信接口模板的逻辑:{}", content); log.info("进入创蓝创建短信接口模板的逻辑,模板内容:{},模板类型:{}", content, type);
Map<String, Object> requestMap = new HashMap<>(); Map<String, Object> requestMap = new HashMap<>();
String appId; String appId;
String username = ChuangLanSMSConfig.interfaceUser; String username = ChuangLanSMSConfig.interfaceUser;
......
package com.jz.sms.config; package com.jz.sms.config;
public enum ResponseCode { public enum ResponseCode {
// 系统判断错误
SUCCESS("200", "处理成功"), SUCCESS("200", "处理成功"),
UNKNOWN_TEMPLATE_TYPE("500", "未知模板类型"), UNKNOWN_TEMPLATE_TYPE("500", "未知模板类型"),
TEMPLATE_ALREADY_EXISTED("500", "DM Hub模板ID冲突"),
SYSTEM_ERROR("99", "系统异常:"),
// 渠道异常错误
UPSTREAM_FAIL("US01", "渠道交易失败"), UPSTREAM_FAIL("US01", "渠道交易失败"),
UPSTREAM_UNKNOWN_MODEL("US01", "未知通知模式"), UPSTREAM_UNKNOWN_MODEL("US01", "未知通知模式"),
UPSTREAM_BLANK("US00", "渠道同步响应为空"), UPSTREAM_BLANK("US00", "渠道同步响应为空");
SYSTEM_ERROR("99", "系统异常:");
private String code; private String code;
......
...@@ -2,12 +2,14 @@ package com.jz.sms.service.bean; ...@@ -2,12 +2,14 @@ package com.jz.sms.service.bean;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import java.util.List;
public class TemplateChangeBean { public class TemplateChangeBean {
private String newStr; // 创蓝发送短信时需要用的模板内容
private String upSendStr;
// 创蓝创建短信模板需要的模板内容
private String upCreateStr;
private JSONArray params; private JSONArray params;
...@@ -16,17 +18,26 @@ public class TemplateChangeBean { ...@@ -16,17 +18,26 @@ public class TemplateChangeBean {
} }
public TemplateChangeBean(String newStr, JSONArray params) { public TemplateChangeBean(String upSendStr, JSONArray params, String upCreateStr) {
this.newStr = newStr; this.upSendStr = upSendStr;
this.params = params; this.params = params;
this.upCreateStr = upCreateStr;
}
public String getUpSendStr() {
return upSendStr;
}
public void setUpSendStr(String upSendStr) {
this.upSendStr = upSendStr;
} }
public String getNewStr() { public String getUpCreateStr() {
return newStr; return upCreateStr;
} }
public void setNewStr(String newStr) { public void setUpCreateStr(String upCreateStr) {
this.newStr = newStr; this.upCreateStr = upCreateStr;
} }
public JSONArray getParams() { public JSONArray getParams() {
......
...@@ -65,9 +65,13 @@ public class MessageServiceImpl implements MessageService { ...@@ -65,9 +65,13 @@ public class MessageServiceImpl implements MessageService {
// 替换模板格式,保存适合创蓝发送短信的模板,避免发送时修改模板格式 // 替换模板格式,保存适合创蓝发送短信的模板,避免发送时修改模板格式
TemplateChangeBean bean = dmHub2ChuangLanSend(templateContent); TemplateChangeBean bean = dmHub2ChuangLanSend(templateContent);
String chuanglanSend = bean.getNewStr(); String chuanglanSend = bean.getUpSendStr();
String params = bean.getParams().toString(); String params = bean.getParams().toString();
SmsTemplateInfo dbInfo = smsTemplateService.selectOne(new EntityWrapper<SmsTemplateInfo>().eq("dm_template_id", templateId));
if (dbInfo != null) {
log.error("Dm Hub请求创建模板,模板id已存在,模板id:【{}】", templateId);
return new DmHubTemplateResponse(ResponseCode.TEMPLATE_ALREADY_EXISTED);
}
SmsTemplateInfo info = new SmsTemplateInfo(); SmsTemplateInfo info = new SmsTemplateInfo();
info.setContent(templateContent); info.setContent(templateContent);
info.setType(smsType); info.setType(smsType);
...@@ -84,10 +88,10 @@ public class MessageServiceImpl implements MessageService { ...@@ -84,10 +88,10 @@ public class MessageServiceImpl implements MessageService {
boolean insert = smsTemplateService.insert(info); boolean insert = smsTemplateService.insert(info);
if (!insert) { if (!insert) {
//TODO 发送告警 //TODO 发送告警
log.error("模板插入DB异常:【{}】", JSONObject.toJSONString(info));
return new DmHubTemplateResponse(ResponseCode.SYSTEM_ERROR); return new DmHubTemplateResponse(ResponseCode.SYSTEM_ERROR);
} }
String chaunglanCreate = dmHub2ChuangLanCreate(chuanglanSend); CLBizResponse template = chuangLanSmsService.createTemplate(bean.getUpCreateStr(), smsType);
CLBizResponse template = chuangLanSmsService.createTemplate(chaunglanCreate, smsType);
if (ResponseCode.SUCCESS.getCode().equals(template.getCode())) { if (ResponseCode.SUCCESS.getCode().equals(template.getCode())) {
String id = template.getId(); String id = template.getId();
SmsTemplateInfo updateInfo = new SmsTemplateInfo(); SmsTemplateInfo updateInfo = new SmsTemplateInfo();
...@@ -127,25 +131,50 @@ public class MessageServiceImpl implements MessageService { ...@@ -127,25 +131,50 @@ public class MessageServiceImpl implements MessageService {
} }
JSONArray arr = new JSONArray(); JSONArray arr = new JSONArray();
if (groups.isEmpty()) { if (groups.isEmpty()) {
return new TemplateChangeBean(content, arr); return new TemplateChangeBean(content, arr, content);
} }
String upCreateStr = content;
String upSendStr = content;
for (String g : groups) { for (String g : groups) {
String t = g; String t = g;
g = Matcher.quoteReplacement(g); g = Matcher.quoteReplacement(g);
g = escapeRegex(g); g = escapeRegex(g);
content = content.replaceAll(g, "\\{\\$var\\}"); upSendStr = upSendStr.replaceAll(g, "\\{\\$var\\}");
if (t.contains("${surl!")) {
// 短链没传长度,固定长度20
upCreateStr = upCreateStr.replaceAll(g, "\\{s20\\}");
} else {
int i = t.indexOf("@");
int i1 = t.indexOf("!");
String str = t.substring(i + 1, i1);
if (isNumeric(str)) {
Integer integer = Integer.valueOf(str);
if (integer == 0) {
upCreateStr = upCreateStr.replaceAll(g, "\\{s20\\}");
} else {
upCreateStr = upCreateStr.replaceAll(g, "\\{s" + integer + "\\}");
}
} else {
upCreateStr = upCreateStr.replaceAll(g, "\\{s20\\}");
}
}
} }
arr.addAll(groups); arr.addAll(groups);
return new TemplateChangeBean(content, arr); return new TemplateChangeBean(upSendStr, arr, upCreateStr);
} }
private boolean isNumeric(String str) {
public String dmHub2ChuangLanCreate(String content) { if (StringUtils.isBlank(str)) {
// TODO 简单处理,没有处理长度 return false;
content = content.replaceAll("\\{\\$var\\}", "\\{s20\\}"); }
return content; for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))) {
return false;
}
}
return true;
} }
public static String escapeRegex(String s) { public static String escapeRegex(String s) {
......
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