Commit 338aee31 authored by qinxunjia's avatar qinxunjia

删除多余的代码

parent 716063ae
...@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit; ...@@ -23,6 +23,7 @@ import java.util.concurrent.TimeUnit;
/** /**
* Dm hub系统交互业务层 * Dm hub系统交互业务层
*/ */
@SuppressWarnings("Duplicates")
@Service @Service
public class DmHubServiceImpl implements DmHubService { public class DmHubServiceImpl implements DmHubService {
......
package com.jz;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(" com.jz.sms.repository.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
package com.jz.exception;
import com.jz.sms.config.ResponseCode;
public class ServiceException extends RuntimeException {
private static final long serialVersionUID = 1L;
private String msg;
private String code = "500";
public ServiceException(String msg) {
super(msg);
this.msg = msg;
}
public ServiceException(String msg, Throwable e) {
super(msg, e);
this.msg = msg;
}
public ServiceException(String msg, String code) {
super(msg);
this.msg = msg;
this.code = code;
}
public ServiceException(String msg, String code, Throwable e) {
super(msg, e);
this.msg = msg;
this.code = code;
}
public ServiceException(ResponseCode responseCode) {
super(responseCode.getMsg());
this.msg = responseCode.getMsg();
this.code = responseCode.getCode();
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
package com.jz.sms.channel.api;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.channel.chuanglan.dto.CLBizResponse;
import com.jz.sms.channel.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.channel.chuanglan.dto.ReportDto;
import com.jz.sms.channel.chuanglan.service.ChuangLanSmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RequestMapping("/test/chuanglan")
@RestController
public class ChuangLanApi {
private static final Logger log = LoggerFactory.getLogger(ChuangLanApi.class);
@Autowired
private ChuangLanSmsService chuangLanSmsService;
@PostMapping("/notify")
public String notify(@RequestParam CLNotifyRequest notifyInfo) {
log.info("创蓝异步通知接口入参:{}", JSONObject.toJSONString(notifyInfo));
String response = "success";
try {
chuangLanSmsService.asyncNotify(notifyInfo);
} catch (Exception e) {
log.error("处理创建模板异步通知异常:", e);
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response);
return response;
}
@GetMapping("/report")
public String report(ReportDto report) {
log.info("回送报告接口入参:{}", JSONObject.toJSONString(report));
String response = "success";
try {
CLBizResponse responses = chuangLanSmsService.report(report);
} catch (Exception e) {
log.error("处理回执逻辑异常:", e);
response = "fail";
}
log.info("创蓝异步通知接口出参:{}", response);
return response;
}
}
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;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/test")
public class DmHubApi {
private static final Logger log = LoggerFactory.getLogger(DmHubApi.class);
@Autowired
private MessageService messageService;
/**
* 创建短信模板
*
* @return
*/
@PostMapping("/sms/template")
public DmHubTemplateResponse template(@RequestBody DmHubTemplateRequest params) {
log.info("**********创建模板接口入参*******:{}", JSONObject.toJSONString(params));
DmHubTemplateResponse template;
try {
template = messageService.createTemplate(params);
} catch (Exception e) {
log.error("创建模板短信异常", e);
template = new DmHubTemplateResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********创建模板接口出参*******:{}", JSONObject.toJSONString(template));
return template;
}
/**
* 批量发送(通知或营销类)
*
* @return
*/
@PostMapping("/sms/batch")
public DmHubSendResponse batch(@RequestBody DmHubBatchSendRequest request) {
log.info("**********批量发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response;
try {
response = messageService.batchSend(request);
} catch (Exception e) {
log.error("发送批量短信异常", e);
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********批量发送出参*******:{}", response);
return response;
}
/**
* 发送单条(通知或营销类)
*
* @return
*/
@RequestMapping("/sms/send")
public DmHubSendResponse send(@RequestBody DmHubSendRequest request) {
log.info("**********单条发送入参*******:{}", JSONObject.toJSONString(request));
DmHubSendResponse response;
try {
response = messageService.send(request);
} catch (Exception e) {
log.error("发送单条短信异常", e);
response = new DmHubSendResponse(ResponseCode.SYSTEM_ERROR);
}
log.info("**********单条发送出参*******:{}", JSONObject.toJSONString(response));
return response;
}
}
package com.jz.sms.channel.chuanglan.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* 短信参数配置
*/
@Component
public class ChuangLanSMSConfig {
// 通知类短信账号
public static String notificationAccount;
// 通知类短信密码
public static String notificationPassword;
// 营销类短信账号
public static String marketingAccount;
// 营销类短信密码
public static String marketingPassword;
// 固定内容发送接口地址
public static String sendFixed;
// 变量短信发送接口地址
public static String sendVariable;
// 系统接口回执报告地址
public static String reportUrl;
// 拉取短信报告地址
public static String pullReport;
// 创建短信模板
public static String addTemplate;
// 接口用户名
public static String interfaceUser;
// 接口密码
public static String interfacePassword;
@Value("${system.config.chuanglan.notificationAccount}")
public void setNotificationAccount(String notificationAccount) {
ChuangLanSMSConfig.notificationAccount = notificationAccount;
}
@Value("${system.config.chuanglan.notificationPassword}")
public void setNotificationPassword(String notificationPassword) {
ChuangLanSMSConfig.notificationPassword = notificationPassword;
}
@Value("${system.config.chuanglan.marketingAccount}")
public void setMarketingAccount(String marketingAccount) {
ChuangLanSMSConfig.marketingAccount = marketingAccount;
}
@Value("${system.config.chuanglan.marketingPassword}")
public void setMarketingPassword(String marketingPassword) {
ChuangLanSMSConfig.marketingPassword = marketingPassword;
}
@Value("${system.config.chuanglan.sendFixed}")
public void setSendFixed(String sendFixed) {
ChuangLanSMSConfig.sendFixed = sendFixed;
}
@Value("${system.config.chuanglan.sendVariable}")
public void setSendVariable(String sendVariable) {
ChuangLanSMSConfig.sendVariable = sendVariable;
}
@Value("${system.config.chuanglan.reportUrl}")
public void setReportUrl(String reportUrl) {
ChuangLanSMSConfig.reportUrl = reportUrl;
}
@Value("${system.config.chuanglan.pullReport}")
public void setPullReport(String pullReport) {
ChuangLanSMSConfig.pullReport = pullReport;
}
@Value("${system.config.chuanglan.addTemplate}")
public void setAddTemplate(String addTemplate) {
ChuangLanSMSConfig.addTemplate = addTemplate;
}
@Value("${system.config.chuanglan.interfaceUser}")
public void setInterfaceUser(String interfaceUser) {
ChuangLanSMSConfig.interfaceUser = interfaceUser;
}
@Value("${system.config.chuanglan.interfacePassword}")
public void setInterfacePassword(String interfacePassword) {
ChuangLanSMSConfig.interfacePassword = interfacePassword;
}
}
package com.jz.sms.channel.chuanglan.config;
/**
* 内容枚举类
*/
public enum ContentEnum {
SMS_TYPE_NOTIFICATION("notification", "通知类短信标识"),
SMS_TYPE_MARKETING("marketing", "营销类短信标识"),
SMS_STATUS_SUCCESS("DELIVRD", "创蓝短信发送状态--成功"),
SMS_STATUS_UNKNOWN("UNKNOWN", "创蓝短信发送状态--未知"),
SMS_STATUS_REFUSE("REJECTD", "创蓝短信发送状态--短信中心拒绝"),
SMS_STATUS_MBBLACK("MBBLACK", "创蓝短信发送状态--黑名单号码"),
SMS_STATUS_REJECT("REJECT", "创蓝短信发送状态--驳回");
private final String value;
private final String desc;
ContentEnum(String value, String desc) {
this.value = value;
this.desc = desc;
}
public String getValue() {
return value;
}
public String getDesc() {
return desc;
}
}
package com.jz.sms.channel.chuanglan.dto;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.config.ResponseCode;
import java.io.Serializable;
public class CLBizResponse implements Serializable {
private static final long serialVersionUID = 8100192126279967648L;
private String code;
private String msg;
private String id;
private JSONObject data;
public CLBizResponse() {
}
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 CLBizResponse(ResponseCode responseCode, String msg, int nullInfo) {
this.code = responseCode.getCode();
this.msg = responseCode.getMsg()+msg;
}
public CLBizResponse(String code, String msg) {
this.code = code;
this.msg = msg;
}
public CLBizResponse(String code, String msg, String id) {
this.code = code;
this.msg = msg;
this.id = id;
}
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;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public JSONObject getData() {
return data;
}
public void setData(JSONObject data) {
this.data = data;
}
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class CLNotifyRequest implements Serializable {
private static final long serialVersionUID = 1L;
// 接口用户名
private String username;
// 十位时间戳
private String timestamp;
// 签名串
private String signature;
// 模块
private String action;
// 模板id
private String id;
// 审核状态(模板(1审核,2驳回),签名(2通过,3驳回))
private String auditStatus;
// 驳回原因
private String auditReason;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getSignature() {
return signature;
}
public void setSignature(String signature) {
this.signature = signature;
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuditStatus() {
return auditStatus;
}
public void setAuditStatus(String auditStatus) {
this.auditStatus = auditStatus;
}
public String getAuditReason() {
return auditReason;
}
public void setAuditReason(String auditReason) {
this.auditReason = auditReason;
}
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class CLSendFixedRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创蓝API账号,必填
*/
private String account;
/**
* 创蓝API密码,必填
*/
private String password;
/**
* 短信内容。长度不能超过536个字符,必填
*/
private String msg;
/**
* 机号码。多个手机号码使用英文逗号分隔,必填
*/
private String phone;
/**
* 定时发送短信时间。格式为yyyyMMddHHmm,值小于或等于当前时间则立即发送,默认立即发送,选填
*/
private String sendtime;
/**
* 是否需要状态报告(默认false),选填
*/
private String report;
/**
* 下发短信号码扩展码,纯数字,建议1-3位,选填
*/
private String extend;
/**
* 该条短信在您业务系统内的ID,如订单号或者短信发送记录流水号,选填
*/
private String uid;
public CLSendFixedRequest() {
}
public CLSendFixedRequest(String account, String password, String msg, String phone) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
}
public CLSendFixedRequest(String account, String password, String msg, String phone, String report) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.report = report;
}
public CLSendFixedRequest(String account, String password, String msg, String phone, String report, String uid) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.report = report;
this.uid = uid;
}
public CLSendFixedRequest(String account, String password, String msg, String phone, String sendtime, String report, String uid) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.phone = phone;
this.sendtime = sendtime;
this.report = report;
this.uid = uid;
}
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getSendtime() {
return sendtime;
}
public void setSendtime(String sendtime) {
this.sendtime = sendtime;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class CLSendSMSResponse implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 响应时间
*/
private String time;
/**
* 消息id
*/
private String msgId;
/**
* 状态码说明(成功返回空)
*/
private String errorMsg;
/**
* 状态码(详细参考提交响应状态码)
*/
private String code;
private String failNum;
private String successNum;
public CLSendSMSResponse() {
}
public CLSendSMSResponse(String code, String errorMsg) {
this.code = code;
this.errorMsg = errorMsg;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
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
+ "]";
}
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class CLSendVariableRequest implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 创蓝API账号,必填
*/
private String account;
/**
* 创蓝API密码,必填
*/
private String password;
/**
* 短信内容。长度不能超过536个字符,必填
*/
private String msg;
/**
* 手机号码和变量参数,多组参数使用英文分号;区分,必填
*/
private String params;
/**
* 定时发送短信时间。格式为yyyyMMddHHmm,值小于或等于当前时间则立即发送,默认立即发送,选填
*/
private String sendtime;
/**
* 是否需要状态报告(默认false),选填
*/
private String report;
/**
* 下发短信号码扩展码,纯数字,建议1-3位,选填
*/
private String extend;
/**
* 该条短信在您业务系统内的ID,如订单号或者短信发送记录流水号,选填
*/
private String uid;
public CLSendVariableRequest() {
}
public CLSendVariableRequest(String account, String password, String msg, String params) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.params = params;
}
public CLSendVariableRequest(String account, String password, String msg, String params, String report) {
super();
this.account = account;
this.password = password;
this.msg = msg;
this.params = params;
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;
// this.password = password;
// this.msg = msg;
// this.params = params;
// this.sendtime = sendtime;
// }
public String getAccount() {
return account;
}
public void setAccount(String account) {
this.account = account;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public String getSendtime() {
return sendtime;
}
public void setSendtime(String sendtime) {
this.sendtime = sendtime;
}
public String getReport() {
return report;
}
public void setReport(String report) {
this.report = report;
}
public String getExtend() {
return extend;
}
public void setExtend(String extend) {
this.extend = extend;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getParams() {
return params;
}
public void setParams(String params) {
this.params = params;
}
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class CLSendVariableResponse implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 响应时间
*/
private String time;
/**
* 消息id
*/
private String msgId;
/**
* 状态码说明(成功返回空)
*/
private String errorMsg;
/**
* 失败的个数
*/
private String failNum;
/**
* 成功的个数
*/
private String successNum;
/**
* 状态码(详细参考提交响应状态码)
*/
private String code;
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getMsgId() {
return msgId;
}
public void setMsgId(String msgId) {
this.msgId = msgId;
}
public String getErrorMsg() {
return errorMsg;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public String getCode() {
return code;
}
public void setCode(String code) {
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 "SmsVarableResponse [time=" + time + ", msgId=" + msgId + ", errorMsg=" + errorMsg + ", failNum="
+ failNum + ", successNum=" + successNum + ", code=" + code + "]";
}
}
package com.jz.sms.channel.chuanglan.dto;
public class CLTemplateRequest {
}
package com.jz.sms.channel.chuanglan.dto;
import java.io.Serializable;
public class ReportDto implements Serializable {
private static final long serialVersionUID = 1L;
private String receiver;
private String pswd;
private String msgid;
private String reportTime;
private String mobile;
private String notifyTime;
private String uid;
private String length;
private String status;
private String statusDesc;
public String getReceiver() {
return receiver;
}
public void setReceiver(String receiver) {
this.receiver = receiver;
}
public String getPswd() {
return pswd;
}
public void setPswd(String pswd) {
this.pswd = pswd;
}
public String getMsgid() {
return msgid;
}
public void setMsgid(String msgid) {
this.msgid = msgid;
}
public String getReportTime() {
return reportTime;
}
public void setReportTime(String reportTime) {
this.reportTime = reportTime;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getNotifyTime() {
return notifyTime;
}
public void setNotifyTime(String notifyTime) {
this.notifyTime = notifyTime;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getStatusDesc() {
return statusDesc;
}
public void setStatusDesc(String statusDesc) {
this.statusDesc = statusDesc;
}
@Override
public String toString() {
return "ReportDto{" +
"receiver='" + receiver + '\'' +
", pswd='" + pswd + '\'' +
", msgid='" + msgid + '\'' +
", reportTime='" + reportTime + '\'' +
", mobile='" + mobile + '\'' +
", notifyTime='" + notifyTime + '\'' +
", uid='" + uid + '\'' +
", length='" + length + '\'' +
", status='" + status + '\'' +
", statusDesc='" + statusDesc + '\'' +
'}';
}
}
package com.jz.sms.channel.chuanglan.service;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.channel.chuanglan.dto.CLNotifyRequest;
import com.jz.sms.channel.chuanglan.dto.CLBizResponse;
import com.jz.sms.channel.chuanglan.dto.ReportDto;
public interface ChuangLanSmsService {
CLBizResponse createTemplate(String content, String type) throws Exception;
CLBizResponse sendByFixed(JSONObject uidJson, String content, String sendParams, String type) throws Exception;
CLBizResponse sendByVariable(JSONObject uidJson, String content, String sendParams, String type) throws Exception;
CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception;
CLBizResponse report(ReportDto report) throws Exception;
}
package com.jz.sms.channel.chuanglan.service.impl;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jz.sms.channel.chuanglan.config.ContentEnum;
import com.jz.util.HttpUtil;
import com.jz.sms.channel.chuanglan.dto.*;
import com.jz.sms.channel.chuanglan.service.ChuangLanSmsService;
import com.jz.sms.channel.chuanglan.utils.ChuangLanSmsUtil;
import com.jz.sms.channel.chuanglan.config.ChuangLanSMSConfig;
import com.jz.sms.config.ResponseCode;
import com.jz.sms.channel.dmHub.service.DmHubService;
import com.jz.sms.repository.domain.SmsTemplateInfo;
import com.jz.sms.service.SmsTemplateService;
import com.jz.util.Md5Util;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@SuppressWarnings("DuplicatedCode")
@Service
public class ChuangLanSmsServiceImpl implements ChuangLanSmsService {
private static final Logger log = LoggerFactory.getLogger(ChuangLanSmsServiceImpl.class);
private static final String SUCCESS = "success";
private static final String sendSuccessCode = "0"; // 发送响应成功字段
private static final String createTemplateStatus = "status"; // 响应状态字段
private static final String retMsg = "msg"; // 响应描述
private static final String createTemplateData = "data"; // 创建模板响应数据
private static final String notificationId = "49"; // 创蓝通知类短信ID
private static final String marketingId = "52"; // 创蓝营销类短信ID
@Autowired
private SmsTemplateService templateService;
@Autowired
private DmHubService dmHubService;
@Override
public CLBizResponse createTemplate(String content, String type) throws Exception {
log.info("进入创蓝创建短信接口模板的逻辑,模板内容:{},模板类型:{}", content, type);
Map<String, Object> requestMap = new HashMap<>();
String appId;
String username = ChuangLanSMSConfig.interfaceUser;
String password = ChuangLanSMSConfig.interfacePassword;
if (ContentEnum.SMS_TYPE_NOTIFICATION.getValue().equals(type)) {
appId = notificationId;
} else if (ContentEnum.SMS_TYPE_MARKETING.getValue().equals(type)) {
appId = marketingId;
// 两个系统模式不同,DMHUB会带上回TD退订,但是创蓝回自动追加回TD退订。只是营销类
content = content.replaceAll("回TD退订", "");
} else {
// TODO 发送警告
log.error("错误,传入的模板类型未知,类型为:{}", type);
return new CLBizResponse(ResponseCode.SYSTEM_ERROR, "未知的模板类型:" + type, 0);
}
String timestamp = String.valueOf(System.currentTimeMillis() / 1000);
String encrypt = Md5Util.encrypt(new StringBuffer(username).append(password).append(timestamp).toString());
requestMap.put("username", username);
requestMap.put("timestamp", timestamp);
requestMap.put("signature", encrypt);
requestMap.put("appid", appId);
requestMap.put("unsubscribe", "回TD退订");
requestMap.put("content", content);
log.info("请求创蓝创建短信模板的请求参数为:{}", requestMap);
String retStr = HttpUtil.doPost(ChuangLanSMSConfig.addTemplate, requestMap);
log.info("请求创蓝创建短信模板的返回数据为:{}", retStr);
JSONObject retJson = JSONObject.parseObject(retStr);
String status = retJson.getString(createTemplateStatus);
if (SUCCESS.equals(status)) {
JSONObject data = retJson.getJSONObject(createTemplateData);
return new CLBizResponse(ResponseCode.SUCCESS, data);
} else {
String msg = retJson.getString(retMsg);
return new CLBizResponse(ResponseCode.UPSTREAM_FAIL.getCode(), msg);
}
}
@Override
public CLBizResponse sendByFixed(JSONObject uidJson, String content, String sendParams, String type) throws Exception {
String account;
String password;
if (ContentEnum.SMS_TYPE_NOTIFICATION.getValue().equals(type)) {
account = ChuangLanSMSConfig.notificationAccount;
password = ChuangLanSMSConfig.notificationPassword;
log.info("发送短信的模式为:{通知类}");
} else if (ContentEnum.SMS_TYPE_MARKETING.getValue().equals(type)) {
account = ChuangLanSMSConfig.marketingAccount;
password = ChuangLanSMSConfig.marketingPassword;
log.info("发送短信的模式为:{营销类}");
} else {
log.error("发送短信的模式为:{未知类型}");
return new CLBizResponse(ResponseCode.UNKNOWN_TEMPLATE_TYPE);
}
CLSendFixedRequest request = new CLSendFixedRequest(account, password, content, sendParams, "true", uidJson.toJSONString());
String requestJson = JSONObject.toJSONString(request);
String batchId = uidJson.getString("dmHubId");
log.info("【{}】发送固定内容短信,请求上游数据为:{}", batchId, requestJson);
String responseStr = ChuangLanSmsUtil.sendSmsByPost(ChuangLanSMSConfig.sendFixed, requestJson);
log.info("【{}】发送固定内容短信,上游返回数据为:{}", batchId, requestJson);
if (StringUtils.isBlank(responseStr)) {
return new CLBizResponse(ResponseCode.UPSTREAM_BLANK);
}
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
String code = response.getCode();
String errorMsg = response.getErrorMsg();
String msgId = response.getMsgId();
if (!sendSuccessCode.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 CLBizResponse sendByVariable(JSONObject uidJson, String content, String params, String type) throws Exception {
String account;
String password;
if (ContentEnum.SMS_TYPE_NOTIFICATION.getValue().equals(type)) {
account = ChuangLanSMSConfig.notificationAccount;
password = ChuangLanSMSConfig.notificationPassword;
log.info("发送短信的模式为:{通知类}");
} else if (ContentEnum.SMS_TYPE_MARKETING.getValue().equals(type)) {
account = ChuangLanSMSConfig.marketingAccount;
password = ChuangLanSMSConfig.marketingPassword;
log.info("发送短信的模式为:{营销类}");
} else {
log.info("发送短信的模式为:{未知类型},type:{}", type);
return new CLBizResponse(ResponseCode.UNKNOWN_TEMPLATE_TYPE);
}
CLSendVariableRequest request = new CLSendVariableRequest(account, password, content, params, "true", uidJson.toJSONString());
String requestJson = JSONObject.toJSONString(request);
String batchId = uidJson.getString("dmHubId");
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 CLBizResponse(ResponseCode.UPSTREAM_BLANK);
}
CLSendSMSResponse response = JSON.parseObject(responseStr, CLSendSMSResponse.class);
String code = response.getCode();
String errorMsg = response.getErrorMsg();
String successNum = response.getSuccessNum();
String failNum = response.getFailNum();
String msgId = response.getMsgId();
if (!sendSuccessCode.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 CLBizResponse asyncNotify(CLNotifyRequest notifyInfo) throws Exception {
String action = notifyInfo.getAction();
if (!"messageModel".equals(action)) {
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 CLBizResponse report(ReportDto report) throws Exception {
// TODO 状态报告要特殊处理,一个个接受,批量发送(处理方式待定)
dmHubService.smsReport(report);
return new CLBizResponse(ResponseCode.SUCCESS);
}
}
package com.jz.sms.channel.chuanglan.utils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
//import java.io.PrintWriter;
/**
* @author tianyh
* @Description:HTTP 请求
*/
public class ChuangLanSmsUtil {
/**
* @param path
* @param postContent
* @return String
* @throws
* @author tianyh
* @Description
*/
public static String sendSmsByPost(String path, String postContent) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// printWriter.write(postContent);
// printWriter.flush();
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
os.write(postContent.getBytes("UTF-8"));
os.flush();
StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 开始获取数据
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String sendTemplateByPost(String path, String postContent) {
URL url = null;
try {
url = new URL(path);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");// 提交模式
httpURLConnection.setConnectTimeout(10000);//连接超时 单位毫秒
httpURLConnection.setReadTimeout(10000);//读取超时 单位毫秒
// 发送POST请求必须设置如下两行
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setRequestProperty("Charset", "UTF-8");
httpURLConnection.setRequestProperty("Content-Type", "application/json");
// PrintWriter printWriter = new PrintWriter(httpURLConnection.getOutputStream());
// printWriter.write(postContent);
// printWriter.flush();
httpURLConnection.connect();
OutputStream os = httpURLConnection.getOutputStream();
os.write(postContent.getBytes("UTF-8"));
os.flush();
StringBuilder sb = new StringBuilder();
int httpRspCode = httpURLConnection.getResponseCode();
if (httpRspCode == HttpURLConnection.HTTP_OK) {
// 开始获取数据
BufferedReader br = new BufferedReader(
new InputStreamReader(httpURLConnection.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.jz.sms.channel.dmHub.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
@Component
public class DmHubConfig {
// 应用过id
public static String applicationId;
// 应用key
public static String applicationKey;
public static String tokenUrl;
public static String report;
@Value("${system.config.dmHub.applicationId}")
public void setApplicationId(String applicationId) {
DmHubConfig.applicationId = applicationId;
}
@Value("{system.config.dmHub.applicationKey}")
public void setApplicationKey(String applicationKey) {
DmHubConfig.applicationKey = applicationKey;
}
@Value("{system.config.dmHub.tokenUrl}")
public void setTokenUrl(String tokenUrl) {
DmHubConfig.tokenUrl = tokenUrl;
}
@Value("{system.config.dmHub.report}")
public void setReport(String report) {
DmHubConfig.report = report;
}
}
package com.jz.sms.channel.dmHub.service;
import com.jz.sms.channel.chuanglan.dto.ReportDto;
import java.util.Map;
public interface DmHubService {
/**
* 获取DM hub token,用户请求DM hub接口
*
* @return token信息
*/
String getToken() ;
/**
* 回执报告给DM hub
*
* @param dto
* @return
*/
Map<String, Object> smsReport(ReportDto dto) throws Exception;
}
package com.jz.sms.channel.dmHub.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.jz.sms.channel.chuanglan.config.ContentEnum;
import com.jz.sms.channel.chuanglan.dto.ReportDto;
import com.jz.sms.channel.dmHub.config.DmHubConfig;
import com.jz.sms.channel.dmHub.service.DmHubService;
import com.jz.sms.channel.dto.DmhubReport;
import com.jz.sms.channel.dto.ReportDetail;
import com.jz.util.HttpUtil;
import com.jz.util.OkHttpUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
/**
* Dm hub系统交互业务层
*/
@Service
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
private RedisTemplate<String, Object> redisTemplate;
@Override
public String getToken() {
try {
Long expire = redisTemplate.getExpire(TOKEN_KEY, TimeUnit.SECONDS);
Object token = redisTemplate.opsForValue().get(TOKEN_KEY);
if (expire != null && expire > 0L && token != null) {
return token.toString();
} else {
String appid = DmHubConfig.applicationId;
String secret = DmHubConfig.applicationKey;
String tokenUrl = DmHubConfig.tokenUrl;
String grantType = "client_credentials";
Map<String, String> requestMap = new HashMap<>();
requestMap.put("appid", appid);
requestMap.put("secret", secret);
requestMap.put("grant_type", grantType);
String retStr = OkHttpUtil.get(tokenUrl, requestMap);
JSONObject retJson = JSONObject.parseObject(retStr);
String errorCode = retJson.getString("error_code");
if ("0".equals(errorCode)) {
String accessToken = retJson.getString("access_token");
String expires = retJson.getString("expires_in");
long exTime = Long.parseLong(expires) - 60 * 10L;
redisTemplate.opsForValue().set(TOKEN_KEY, accessToken, exTime, TimeUnit.SECONDS);
return accessToken;
} else {
// TODO 系统告警,获取API接口TOKEN失败。
return null;
}
}
} catch (Exception e) {
log.error("获取API接口TOKEN业务逻辑异常,异常信息:\r\n", e);
return null;
}
}
@Override
public Map<String, Object> smsReport(ReportDto dto) throws Exception {
try {
String token = getToken();
if (StringUtils.isBlank(token)) {
return null;
}
String uidStr = dto.getUid();
JSONObject uidJson = JSONObject.parseObject(uidStr);
String dmHubBatchId = uidJson.getString("dmHubId");
String sysBatchId = uidJson.getString("sysBatchId");
DmhubReport report = new DmhubReport();
report.setAccess_token(token);
report.setType("sms");
ReportDetail detail = new ReportDetail();
detail.setAudienceId(dto.getMobile());
detail.setBatchId(dmHubBatchId);
detail.setReportDate(dto.getReportTime());
detail.setMobile(dto.getMobile());
detail.setChargeNum(Integer.valueOf(dto.getLength()));
detail.setSendDate(dto.getReportTime());
if (ContentEnum.SMS_STATUS_SUCCESS.getValue().equals(dto.getStatus())) {
detail.setIsReceive(true);
} else {
detail.setIsReceive(false);
}
detail.setErrMessage(dto.getStatusDesc());
report.setReports(detail);
log.info("短信状态回执参数:{}", JSONObject.toJSONString(report));
String retStr = HttpUtil.sendPost(DmHubConfig.report, JSONObject.toJSONString(report));
log.info("短信状态回执响应:{}", retStr);
//TODO 目前没有测试环境的请求地址
} catch (Exception e) {
log.info("发送短信回执到DMHub异常\r\n短信信息:{},\r\n,异常:", dto, e);
}
return null;
}
}
package com.jz.sms.channel.dto;
import com.alibaba.fastjson.JSONObject;
import java.io.Serializable;
import java.util.List;
public class DmHubBatchSendRequest implements Serializable {
private static final long serialVersionUID = 1L;
// 批量发送短信的模板id
private String templateId;
// 批次号
private String batchId;
// 账号类型(mobile就传手机号)
private String audienceIdType;
// 短信数据
private List<JSONObject> data;
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getBatchId() {
return batchId;
}
public void setBatchId(String batchId) {
this.batchId = batchId;
}
public List<JSONObject> getData() {
return data;
}
public void setData(List<JSONObject> data) {
this.data = data;
}
public String getAudienceIdType() {
return audienceIdType;
}
public void setAudienceIdType(String audienceIdType) {
this.audienceIdType = audienceIdType;
}
}
package com.jz.sms.channel.dto;
import com.alibaba.fastjson.JSONObject;
import java.io.Serializable;
public class DmHubSendRequest implements Serializable {
private static final long serialVersionUID = 1L;
// 发送短信的模板ID
private String templateId;
// DMHUb的批次号
private String batchId;
// 渠道类型(初期给到DMHUb系统库的类型,如果是sms,data里面会直接反手机号,
// 如果是customerIdentity.memberId,data返回memberId,需要查询系统memberId用户对应的手机号发送短信)
private String audienceIdType;
// 发送数据
private JSONObject data;
public String getTemplateId() {
return templateId;
}
public void setTemplateId(String templateId) {
this.templateId = templateId;
}
public String getBatchId() {
return batchId;
}
public void setBatchId(String batchId) {
this.batchId = batchId;
}
public String getAudienceIdType() {
return audienceIdType;
}
public void setAudienceIdType(String audienceIdType) {
this.audienceIdType = audienceIdType;
}
public JSONObject getData() {
return data;
}
public void setData(JSONObject data) {
this.data = data;
}
}
package com.jz.sms.channel.dto;
import com.jz.sms.config.ResponseCode;
import java.io.Serializable;
public class DmHubSendResponse implements Serializable {
private static final long serialVersionUID = 1L;
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;
}
}
package com.jz.sms.channel.dto;
import java.io.Serializable;
public class DmHubTemplateRequest implements Serializable {
private static final long serialVersionUID = 1L;
private String tenantId;
private String templateId;
private String templateContent;
private String smsType;
private String signature;
private String templateName;
public String getTenantId() {
return tenantId;
}
public void setTenantId(String tenantId) {
this.tenantId = tenantId;