新增腾讯短信对接

This commit is contained in:
朱毅骏 2023-04-07 11:52:50 +08:00
parent cac6f19978
commit 41a1203f23
7 changed files with 52 additions and 30 deletions

View File

@ -29,13 +29,19 @@ public class SmsRequest implements Serializable {
*/
private List<String> telNums;
/**
* 发送账号安全认证的Access Key ID
* 应用id 腾讯SmsSdkAppId
*/
private String appId;
/**
* 发送账号安全认证的Access Key ID
*/
private String accessKey;
/**
* 发送账号安全认证的Secret Access Key
*/
private String appSecret;
//秘钥
private String accessSecret;
/**
* 发送使用签名
*/

View File

@ -175,8 +175,8 @@ public class AliSmsHandleGranter extends AbstractSmsGranter {
@SneakyThrows
public static AsyncClient createClient(SmsRequest smsRequest) {
StaticCredentialProvider provider = StaticCredentialProvider.create(Credential.builder()
.accessKeyId(smsRequest.getAppId())
.accessKeySecret(smsRequest.getAppSecret())
.accessKeyId(smsRequest.getAccessKey())
.accessKeySecret(smsRequest.getAccessSecret())
//.securityToken("<your-token>") // use STS token
.build());

View File

@ -84,20 +84,22 @@ public class TenSmsHandleGranter extends AbstractSmsGranter {
@Override
public TaskResultDTO sendSms(SmsRequest smsRequest) {
SendSmsRequest sendSmsRequest = new SendSmsRequest();
return getSmsResponse(smsRequest, sendSmsRequest);
}
private TaskResultDTO getSmsResponse(SmsRequest smsRequest, SendSmsRequest sendSmsRequest) {
sendSmsRequest.setSmsSdkAppId(smsRequest.getAppId());
sendSmsRequest.setTemplateId(smsRequest.getTemplateCode());
sendSmsRequest.setSignName(smsRequest.getSignName());
sendSmsRequest.setSessionContext(String.valueOf(smsRequest.getTaskId()));
sendSmsRequest.setSmsSdkAppId(smsRequest.getAppId());
List<Kv> param = JSON.parseArray(smsRequest.getTemplateParams(), Kv.class);
List<String> paramList = new ArrayList<>();
for (Kv kv : param) {
paramList.add((String) kv.getV());
}
sendSmsRequest.setTemplateParamSet(paramList.toArray(String[]::new));
return getSmsResponse(smsRequest, sendSmsRequest);
}
private TaskResultDTO getSmsResponse(SmsRequest smsRequest, SendSmsRequest sendSmsRequest) {
SmsClient client = createClient(smsRequest);
List<List<String>> phones = Lists.partition(smsRequest.getTelNums(), 200);
try {
@ -156,7 +158,7 @@ public class TenSmsHandleGranter extends AbstractSmsGranter {
@SneakyThrows
public static SmsClient createClient(SmsRequest smsRequest) {
com.tencentcloudapi.common.Credential credential = new com.tencentcloudapi.common.Credential(smsRequest.getAppId(), smsRequest.getAppSecret());
com.tencentcloudapi.common.Credential credential = new com.tencentcloudapi.common.Credential(smsRequest.getAccessKey(), smsRequest.getAccessSecret());
if (!StringUtils.isEmpty(smsRequest.getEndPoint())){
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint(smsRequest.getEndPoint());

View File

@ -30,7 +30,7 @@ public enum ProviderType {
/**
* 腾讯
*/
TENCENT("0", "腾讯云短信", "\\{([^\\}]+)\\}"),
TENCENT("OK,0", "腾讯云短信", "\\{([^\\}]+)\\}"),
/**
* 百度
*/
@ -64,10 +64,12 @@ public enum ProviderType {
}
public SendStatus getTaskStatus(String code) {
if (this.val.equalsIgnoreCase(code)) {
return SendStatus.SUCCESS;
} else {
return SendStatus.FAIL;
String[] values = this.val.split(",");
for (String value : values) {
if (value.equalsIgnoreCase(code)){
return SendStatus.SUCCESS;
}
}
return SendStatus.FAIL;
}
}

View File

@ -18,5 +18,5 @@ import java.util.List;
@Builder
public class TaskResultDTO {
private List<SmsResponse> responses;
private TaskStatus status;
private TaskStatus status = TaskStatus.FAIL;
}

View File

@ -57,14 +57,25 @@ public class SmsTemplatePO {
@TableField(value = "app_id", condition = LIKE)
private String appId;
/**
* 应用密码
* 发送账号安全认证的Access Key ID
*/
@ApiModelProperty(value = "应用ID")
@NotEmpty(message = "请填写应用ID")
@Size(max = 255, message = "应用ID长度不能超过255")
@TableField(value = "access_key", condition = LIKE)
private String accessKey;
/**
* 发送账号安全认证的Secret Access Key
*/
@ApiModelProperty(value = "应用密码")
@NotEmpty(message = "请填写应用密码")
@Size(max = 255, message = "应用密码长度不能超过255")
@TableField(value = "app_secret", condition = LIKE)
private String appSecret;
@TableField(value = "access_secret", condition = LIKE)
//秘钥
private String accessSecret;
/**
* SMS服务域名

View File

@ -168,7 +168,7 @@ public class SmsTaskServiceImpl extends ServiceImpl<SmsTaskMapper,SmsTaskPO> imp
}
});
TaskResultDTO taskResultDTO = null;
Boolean flag = false;
boolean failFlag = false;
try {
SmsRequest request = SmsRequest.builder()
.smsContent(data.getContent())
@ -177,17 +177,19 @@ public class SmsTaskServiceImpl extends ServiceImpl<SmsTaskMapper,SmsTaskPO> imp
.appId(template.getAppId())
.templateCode(template.getTemplateCode())
.signName(template.getSignName())
.appSecret(template.getAppSecret())
.appId(template.getAppId())
.accessKey(template.getAccessKey())
.accessSecret(template.getAccessSecret())
.telNums(sendPhones)
.region(template.getRegion())
.build();
taskResultDTO = smsGranter.sendSms(template.getProviderType(),request);
}catch (Exception e){
log.warn("短信发送任务发送失败", e);
flag = true;
failFlag = true;
}
if (Boolean.FALSE.equals(flag)) {
data.setStatus(Objects.isNull(taskResultDTO.getStatus()) ? TaskStatus.FAIL : taskResultDTO.getStatus());
if (!failFlag) {
data.setStatus(taskResultDTO.getStatus());
} else {
data.setStatus(TaskStatus.FAIL);
}
@ -273,10 +275,9 @@ public class SmsTaskServiceImpl extends ServiceImpl<SmsTaskMapper,SmsTaskPO> imp
if (StringUtils.isEmpty(template)) {
return StringUtils.EMPTY;
}
if (CollectionUtils.isEmpty(params)) {
return StringUtils.EMPTY;
if (CollectionUtils.isEmpty(params)){
params = new LinkedList<>();
}
StringBuilder sb = new StringBuilder();
Matcher m = Pattern.compile(regex).matcher(template);
int index = 0;
@ -295,9 +296,9 @@ public class SmsTaskServiceImpl extends ServiceImpl<SmsTaskMapper,SmsTaskPO> imp
}
m.appendTail(sb);
if (index < params.size()) {
throw new SmsRuntimeException(StringUtils.format("模板中识别出的参数个数: {} 少于传入的参数个数: {}", params.size(), index));
throw new SmsRuntimeException(StringUtils.format("模板中识别出的参数个数: {} 少于传入的参数个数: {}", index,params.size() ));
} else if (index > params.size()) {
throw new SmsRuntimeException(StringUtils.format("模板中识别出的参数个数: {} 多于传入的参数个数: {}", params.size(), index));
throw new SmsRuntimeException(StringUtils.format("模板中识别出的参数个数: {} 多于传入的参数个数: {}",index, params.size()));
}
return sb.toString();
}