Commit 99c7201b authored by qinxunjia's avatar qinxunjia

处理模板变量长度

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