Commit 509a4926 authored by qinxunjia's avatar qinxunjia

发送短信的逻辑

parent e32b047a
package com.jz.sms.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.dto.DmHubBatchSendRequest;
import com.jz.sms.dto.DmHubBatchSendResponse;
import com.jz.sms.dto.DmHubTemplateRequest;
import com.jz.sms.dto.DmHubTemplateResponse;
import com.jz.sms.dto.*;
import com.jz.sms.service.MessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -44,11 +41,11 @@ public class BizController {
* @return
*/
@PostMapping("/sms/batch")
public String batch(@RequestBody DmHubBatchSendRequest request) {
public DmHubBatchSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
DmHubBatchSendResponse response = messageService.batchSend(request);
// log.info("**********批量发送入参*******:{}", response);
return "fail";
return response;
}
......@@ -58,10 +55,12 @@ public class BizController {
* @return
*/
@RequestMapping("/sms/send")
public String send(@RequestBody Map<String, Object> params) {
public DmHubSendResponse send(@RequestBody DmHubSendRequest request) {
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(params));
return "fail";
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response = messageService.send(request);
log.info("**********单条发送出参*******:{}", JSONObject.toJSONString(response));
return response;
}
......
package com.jz.sms.chuanglan.dto;
public class CLSendFixedResponse {
public class CLSendSMSResponse {
/**
* 响应时间
*/
......@@ -19,9 +19,9 @@ public class CLSendFixedResponse {
private String code;
public CLSendFixedResponse(){}
public CLSendSMSResponse(){}
public CLSendFixedResponse(String code, String errorMsg){
public CLSendSMSResponse(String code, String errorMsg){
this.code = code;
this.errorMsg = errorMsg;
}
......
package com.jz.sms.chuanglan.service;
import com.jz.sms.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.chuanglan.dto.CLSendFixedResponse;
import com.jz.sms.chuanglan.dto.CLSendSMSResponse;
import com.jz.sms.chuanglan.dto.CLTemplateResponse;
import com.jz.sms.dto.*;
import java.util.List;
public interface ChuangLanSmsService {
CLTemplateResponse createTemplate(String content, String type) throws Exception;
CLSendFixedResponse sendByFixed(String batchId, String content, List<String> phoneList, String type) throws Exception;
CLSendFixedResponse sendByVariable(String batchId, String content, List<String> phoneList, String type) throws Exception;
DmHubSendResponse sendBatchByFixed(DmHubSendRequest requestDTO);
CLSendSMSResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception;
DmHubBatchSendResponse sendBatchByVariable(DmHubBatchSendRequest requestDTO);
CLSendSMSResponse sendByVariable(String batchId, String content, String sendParams, String type) throws Exception;
void asyncNotify(CLNotifyRequest notifyInfo);
}
......@@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jz.sms.api.BizController;
import com.jz.sms.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.chuanglan.dto.CLSendFixedRequest;
import com.jz.sms.chuanglan.dto.CLSendFixedResponse;
import com.jz.sms.chuanglan.dto.CLSendSMSResponse;
import com.jz.sms.chuanglan.dto.CLTemplateResponse;
import com.jz.sms.chuanglan.service.ChuangLanSmsService;
import com.jz.sms.chuanglan.utils.ChuangLanSmsUtil;
......@@ -85,7 +85,7 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
}
@Override
public CLSendFixedResponse sendByFixed(String batchId, String content, List<String> phoneList, String type) throws Exception {
public CLSendSMSResponse sendByFixed(String batchId, String content, String sendParams, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
......@@ -101,7 +101,8 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
throw new Exception("123");
}
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, listToString(phoneList), "true", batchId);
content = "【九章数据】" + content;
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, sendParams, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{}", batchId, requestJson);
......@@ -109,19 +110,14 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
if (StringUtils.isBlank(responseStr)) {
return new CLSendFixedResponse("500", "渠道同步响应为空");
return new CLSendSMSResponse("500", "渠道同步响应为空");
}
CLSendFixedResponse response = JSON.parseObject(responseStr, CLSendFixedResponse.class);
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
return response;
}
private String listToString(List<String> list) {
String s = list.toString();
return s;
}
@Override
public CLSendFixedResponse sendByVariable(String batchId, String content, List<String> params, String type) throws Exception {
public CLSendSMSResponse sendByVariable(String batchId, String content, String params, String type) throws Exception {
String account;
String password;
if ("notification".equals(type)) {
......@@ -136,31 +132,22 @@ public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
log.info("发送短信的模式为:{未知类型}");
throw new Exception("123");
}
content = "【九章数据】" + content;
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, listToString(params), "true", batchId);
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, params, "true", batchId);
String requestJson = JSONObject.toJSONString(request);
log.info("【{}】发送固定内容短信,请求上游数据为:{}", batchId, requestJson);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendFixed, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, responseStr);
if (StringUtils.isBlank(responseStr)) {
return new CLSendFixedResponse("500", "渠道同步响应为空");
return new CLSendSMSResponse("500", "渠道同步响应为空");
}
CLSendFixedResponse response = JSON.parseObject(responseStr, CLSendFixedResponse.class);
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
return response;
}
@Override
public DmHubSendResponse sendBatchByFixed(DmHubSendRequest requestDTO) {
return null;
}
@Override
public DmHubBatchSendResponse sendBatchByVariable(DmHubBatchSendRequest requestDTO) {
return null;
}
@Override
public void asyncNotify(CLNotifyRequest notifyInfo) {
......
package com.jz.sms.repository.mapper;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.jz.sms.repository.domain.DmBatchInfo;
public interface DmBatchMapper extends BaseMapper<DmBatchInfo> {
}
package com.jz.sms.service;
import com.baomidou.mybatisplus.service.IService;
import com.jz.sms.repository.domain.DmBatchInfo;
public interface DmBatchService extends IService<DmBatchInfo> {
}
......@@ -2,7 +2,7 @@ package com.jz.sms.service;
import com.jz.sms.dto.*;
public interface MessageService {
public interface MessageService {
/**
* DM hub创建模板同步插件接口
......
package com.jz.sms.service;
public interface SysBatchService {
import com.baomidou.mybatisplus.service.IService;
import com.jz.sms.repository.domain.SysBatchInfo;
public interface SysBatchService extends IService<SysBatchInfo> {
}
......@@ -9,14 +9,14 @@ public class TemplateChangeBean {
private String newStr;
private List<String> params;
private JSONArray params;
public TemplateChangeBean() {
}
public TemplateChangeBean(String newStr, List<String> params) {
public TemplateChangeBean(String newStr, JSONArray params) {
this.newStr = newStr;
this.params = params;
}
......@@ -29,11 +29,11 @@ public class TemplateChangeBean {
this.newStr = newStr;
}
public List<String> getParams() {
public JSONArray getParams() {
return params;
}
public void setParams(List<String> params) {
public void setParams(JSONArray params) {
this.params = params;
}
}
package com.jz.sms.service.impl;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jz.sms.repository.domain.DmBatchInfo;
import com.jz.sms.repository.mapper.DmBatchMapper;
import com.jz.sms.service.DmBatchService;
import org.springframework.stereotype.Service;
@Service
public class DmBatchServiceImpl extends ServiceImpl<DmBatchMapper, DmBatchInfo> implements DmBatchService {
}
......@@ -3,13 +3,15 @@ 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.CLSendFixedResponse;
import com.jz.sms.chuanglan.dto.CLSendSMSResponse;
import com.jz.sms.chuanglan.dto.CLTemplateResponse;
import com.jz.sms.chuanglan.service.ChuangLanSmsService;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.dto.*;
import com.jz.sms.repository.domain.DmBatchInfo;
import com.jz.sms.repository.domain.SmsTemplateInfo;
import com.jz.sms.repository.domain.SysBatchInfo;
import com.jz.sms.service.DmBatchService;
import com.jz.sms.service.MessageService;
import com.jz.sms.service.SmsTemplateService;
import com.jz.sms.service.bean.TemplateChangeBean;
......@@ -39,6 +41,9 @@ public class MessageServiceImpl implements MessageService {
@Autowired
private ChuangLanSmsService chuangLanSmsService;
@Autowired
private DmBatchService dmBatchService;
/**
* 短信模板创建
*
......@@ -120,8 +125,9 @@ public class MessageServiceImpl implements MessageService {
String group = matcher.group();
groups.add(group);
}
JSONArray arr = new JSONArray();
if (groups.isEmpty()) {
return new TemplateChangeBean(content, groups);
return new TemplateChangeBean(content, arr);
}
int idx = content.indexOf("${surl!'");
if (idx >= 0) {
......@@ -144,11 +150,13 @@ public class MessageServiceImpl implements MessageService {
content = content.replaceAll(g, "\\{\\$var\\}");
}
return new TemplateChangeBean(content, groups);
arr.addAll(groups);
return new TemplateChangeBean(content, arr);
}
public String dmHub2ChuangLanCreate(String content) {
// TODO 简单处理,没有处理长度
content = content.replaceAll("\\{\\$var\\}", "\\{s20\\}");
return content;
......@@ -179,10 +187,41 @@ public class MessageServiceImpl implements MessageService {
return null;
}
// 2、根据请求信息获取用户的手机号码(此处不考虑配置DMHUB系统不是SMS的情况),直接拿请求体中的_audienceId字段值,该值为手机号码
String phone = data.getString("_audienceId");
List<String> phoneList = new ArrayList<>();
phoneList.add(phone);
// 模板参数占位符
String params = templateInfo.getParams();
JSONArray paramsArr = JSONArray.parseArray(params);
// 封装参数
StringBuilder sendParams = new StringBuilder();
Set<String> keys = data.keySet();
String mobile = data.getString("_audienceId");
data.remove("name");
data.remove("id");
data.remove("_audienceId");
sendParams.append(mobile);
if (!data.isEmpty()) {
for (Object parm : paramsArr) {
String s = parm.toString();
for (String key : keys) {
String newKey = key;
Object value = data.get(key);
if (value instanceof JSONObject || value instanceof Map) {
StringBuilder keyBuffer = new StringBuilder(key);
value = test(value, keyBuffer);
newKey = keyBuffer.toString();
}
if (s.contains(newKey)) {
sendParams.append(",").append(value.toString());
}
}
}
sendParams.append(";");
} else {
sendParams.append(",");
}
String type = templateInfo.getType();
// 3、记录批次信息
......@@ -199,33 +238,13 @@ public class MessageServiceImpl implements MessageService {
// 判断模板是固定短信还是变量短信
String content = templateInfo.getContent();
// String content2 = "亲爱的${name@6!\"\"},您的手机号码:${mobile@12!\"\"}参与了优惠活动,详见: ${surl!'https://www.baidu.com/?cl_sr=短消息'} 回TD退订";
String regex = "\\$\\{[^}]+\\}";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(content);
List<String> list = new ArrayList<>();
while (matcher.find()) {
String group = matcher.group();
list.add(group);
}
log.info("模板变量参数为:{}", list.toString());
if (list.size() > 0) {
// 变量模板
// 3、模板参数文本替换。获取模板中的动态参内容,替换成创蓝需要的格式${var}.将请求中的参数信息和模板中的变量名对应起来,封装创蓝请求参数
for (String str : list) {
}
CLSendFixedResponse chuanglanResponse = chuangLanSmsService.sendByFixed(dmHubBatchId, content, phoneList, type);
if (paramsArr == null || paramsArr.isEmpty()) {
CLSendSMSResponse chuanglanResponse = chuangLanSmsService.sendByFixed(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
} else {
// 固定模板
// 4、封裝數據
chuangLanSmsService.sendByVariable(dmHubBatchId, content, phoneList, type);
CLSendSMSResponse response = chuangLanSmsService.sendByVariable(dmHubBatchId, templateInfo.getUpContent(), sendParams.toString(), type);
}
......@@ -249,26 +268,101 @@ public class MessageServiceImpl implements MessageService {
return new DmHubBatchSendResponse("500", "模板不存在");
}
// 模板参数占位符
String params = templateInfo.getParams();
JSONArray paramsArr = JSONArray.parseArray(params);
// 封装参数
StringBuilder sendParams = new StringBuilder();
for (JSONObject json : data) {
Set<String> keys = json.keySet();
StringBuilder sb = new StringBuilder();
String mobile = json.getString("_audienceId");
json.remove("name");
json.remove("id");
json.remove("_audienceId");
sb.append(mobile);
sendParams.append(mobile);
if (!json.isEmpty()) {
for (String key : keys) {
String value = json.getString(key);
for (Object parm : paramsArr) {
String s = parm.toString();
for (String key : keys) {
String newKey = key;
Object value = json.get(key);
if (value instanceof JSONObject || value instanceof Map) {
StringBuilder keyBuffer = new StringBuilder(key);
value = test(value, keyBuffer);
newKey = keyBuffer.toString();
}
if (s.contains(newKey)) {
sendParams.append(",").append(value.toString());
}
}
}
sendParams.append(";");
} else {
sendParams.append(",");
}
}
System.out.println(sendParams.toString());
DmBatchInfo dmInfo = new DmBatchInfo();
dmInfo.setDmBatchId(batchId);
dmInfo.setDmTemplateId(templateId);
dmInfo.setSmsNum(data.size());
dmInfo.setId(IdHandler.nextId());
dmInfo.setDateCreated(new Date());
dmInfo.setLastUpdated(new Date());
dmBatchService.insert(dmInfo);
SysBatchInfo info = new SysBatchInfo();
info.setId(info.getBatchId());
info.setDmBatchId(batchId);
info.setSmsNum(data.size());
info.setDmTemplateId(templateId);
Long sysBatchId = IdHandler.nextId();
info.setBatchId(sysBatchId);
info.setDateCreated(new Date());
info.setLastUpdated(new Date());
sysBatchService.insert(info);
if (paramsArr == null || paramsArr.isEmpty()) {
try {
CLSendSMSResponse response = chuangLanSmsService.sendByFixed(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
} catch (Exception e) {
log.error("调用创蓝逻辑错误:{}", e);
return null;
}
} else {
try {
CLSendSMSResponse response = chuangLanSmsService.sendByVariable(batchId, templateInfo.getUpContent(), sendParams.toString(), templateInfo.getType());
} catch (Exception e) {
log.error("调用创蓝逻辑错误:{}", e);
return null;
}
}
return null;
}
public String test(Object value, StringBuilder keyBuffer) {
String s = JSONObject.toJSONString(value);
JSONObject valueJson = JSONObject.parseObject(s);
Set<String> set = valueJson.keySet();
if (set.isEmpty()) {
return value.toString();
}
for (String str : set) {
keyBuffer.append(".").append(str);
Object info = valueJson.get(str);
if (info instanceof JSONObject || info instanceof Map) {
String test = test(info, keyBuffer);
return test;
} else {
return info.toString();
}
}
return value.toString();
}
}
......
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