新增百度短信发送

This commit is contained in:
朱毅骏 2023-04-07 19:00:55 +08:00
parent 40ef9fbe27
commit 200df01713
7 changed files with 205 additions and 13 deletions

View File

@ -22,10 +22,6 @@
<groupId>cn.zyjblogs.starter</groupId> <groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-common-spring-boot-starter</artifactId> <artifactId>zyjblogs-common-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
</dependency>
<dependency> <dependency>
<groupId>cn.zyjblogs.starter</groupId> <groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-mybatisplus-spring-boot-starter</artifactId> <artifactId>zyjblogs-mybatisplus-spring-boot-starter</artifactId>
@ -38,10 +34,7 @@
<groupId>cn.zyjblogs.starter</groupId> <groupId>cn.zyjblogs.starter</groupId>
<artifactId>zyjblogs-oauth-spring-boot-starter</artifactId> <artifactId>zyjblogs-oauth-spring-boot-starter</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
</dependency>
<dependency> <dependency>
<artifactId>zyjblogs-web-spring-boot-starter</artifactId> <artifactId>zyjblogs-web-spring-boot-starter</artifactId>
<groupId>cn.zyjblogs.starter</groupId> <groupId>cn.zyjblogs.starter</groupId>
@ -54,5 +47,39 @@
<groupId>com.alibaba.fastjson2</groupId> <groupId>com.alibaba.fastjson2</groupId>
<artifactId>fastjson2</artifactId> <artifactId>fastjson2</artifactId>
</dependency> </dependency>
<!--=============================sms短信sdk begin========================== -->
<!-- 阿里云-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
</dependency>
<!-- 腾讯云-->
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-sms</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.baidubce/bce-java-sdk -->
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>bce-java-sdk</artifactId>
<version>0.10.253</version>
<exclusions>
<exclusion>
<artifactId>log4j</artifactId>
<groupId>log4j</groupId>
</exclusion>
<exclusion>
<artifactId>slf4j-log4j12</artifactId>
<groupId>org.slf4j</groupId>
</exclusion>
<exclusion>
<groupId>org.apache.hbase</groupId>
<artifactId>hbase-client</artifactId>
</exclusion>
</exclusions>
</dependency>
<!--=============================sms短信sdk end========================== -->
</dependencies> </dependencies>
</project> </project>

View File

@ -4,6 +4,7 @@ package cn.zyjblogs.sms.configuration;
import cn.zyjblogs.sms.provider.CompositeSmsGranter; import cn.zyjblogs.sms.provider.CompositeSmsGranter;
import cn.zyjblogs.sms.provider.SmsGranter; import cn.zyjblogs.sms.provider.SmsGranter;
import cn.zyjblogs.sms.provider.aliyun.AliSmsHandleGranter; import cn.zyjblogs.sms.provider.aliyun.AliSmsHandleGranter;
import cn.zyjblogs.sms.provider.baidu.BaiduBceSmsHandleGranter;
import cn.zyjblogs.sms.provider.tencent.TenSmsHandleGranter; import cn.zyjblogs.sms.provider.tencent.TenSmsHandleGranter;
import cn.zyjblogs.sms.task.constant.ProviderType; import cn.zyjblogs.sms.task.constant.ProviderType;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -22,6 +23,7 @@ public class SmsConfiguration {
List<SmsGranter> list = new ArrayList<>(); List<SmsGranter> list = new ArrayList<>();
list.add(new AliSmsHandleGranter(ProviderType.ALI)); list.add(new AliSmsHandleGranter(ProviderType.ALI));
list.add(new TenSmsHandleGranter(ProviderType.TENCENT)); list.add(new TenSmsHandleGranter(ProviderType.TENCENT));
list.add(new BaiduBceSmsHandleGranter(ProviderType.BAIDU));
return new CompositeSmsGranter(list); return new CompositeSmsGranter(list);
} }
} }

View File

@ -8,10 +8,27 @@ import cn.zyjblogs.starter.common.exception.AbstractBusinessException;
*/ */
public class SmsRuntimeException extends AbstractBusinessException { public class SmsRuntimeException extends AbstractBusinessException {
private String code;
private String requestId;
public String getCode() {
return code;
}
public String getRequestId() {
return requestId;
}
public SmsRuntimeException() { public SmsRuntimeException() {
super(); super();
} }
public SmsRuntimeException(String message, String code, String requestId) {
super(message);
this.code = code;
this.requestId = requestId;
}
public SmsRuntimeException(String message) { public SmsRuntimeException(String message) {
super(message); super(message);
} }

View File

@ -2,10 +2,11 @@ package cn.zyjblogs.sms.provider;
import cn.zyjblogs.sms.common.SmsRequest; import cn.zyjblogs.sms.common.SmsRequest;
import cn.zyjblogs.sms.common.SmsResponse; import cn.zyjblogs.sms.provider.baidu.BaiduBceSmsHandleGranter;
import cn.zyjblogs.sms.task.constant.ProviderType; import cn.zyjblogs.sms.task.constant.ProviderType;
import cn.zyjblogs.sms.task.constant.TaskStatus; import cn.zyjblogs.sms.task.constant.TaskStatus;
import cn.zyjblogs.sms.task.dto.TaskResultDTO; import cn.zyjblogs.sms.task.dto.TaskResultDTO;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
@ -38,7 +39,7 @@ public class CompositeSmsGranter implements SmsGranter {
if (granter1 != null) { if (granter1 != null) {
return granter1.sendSms(providerType, smsRequest); return granter1.sendSms(providerType, smsRequest);
} }
return new TaskResultDTO(new LinkedList<>(), TaskStatus.SUCCESS); return new TaskResultDTO(new LinkedList<>(), TaskStatus.FAIL);
} }

View File

@ -19,6 +19,7 @@ import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponseBody;
import darabonba.core.client.ClientOverrideConfiguration; import darabonba.core.client.ClientOverrideConfiguration;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -34,7 +35,9 @@ import java.util.concurrent.CompletableFuture;
@Slf4j @Slf4j
public class AliSmsHandleGranter extends AbstractSmsGranter { public class AliSmsHandleGranter extends AbstractSmsGranter {
private static final ProviderType PROVIDER_TYPE = ProviderType.ALI; private static final ProviderType PROVIDER_TYPE = ProviderType.ALI;
public AliSmsHandleGranter() {
super(PROVIDER_TYPE);
}
public AliSmsHandleGranter(ProviderType providerType) { public AliSmsHandleGranter(ProviderType providerType) {
super(providerType); super(providerType);
} }

View File

@ -0,0 +1,139 @@
package cn.zyjblogs.sms.provider.baidu;
import cn.zyjblogs.sms.common.SmsRequest;
import cn.zyjblogs.sms.common.SmsResponse;
import cn.zyjblogs.sms.exception.SmsRuntimeException;
import cn.zyjblogs.sms.provider.AbstractSmsGranter;
import cn.zyjblogs.sms.provider.SmsGranter;
import cn.zyjblogs.sms.task.constant.ProviderType;
import cn.zyjblogs.sms.task.constant.SendStatus;
import cn.zyjblogs.sms.task.constant.TaskStatus;
import cn.zyjblogs.sms.task.dto.TaskResultDTO;
import cn.zyjblogs.starter.common.utils.collect.Lists;
import cn.zyjblogs.starter.common.utils.string.StringUtils;
import com.alibaba.fastjson2.JSON;
import com.baidubce.BceServiceException;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.sms.SmsClient;
import com.baidubce.services.sms.SmsClientConfiguration;
import com.baidubce.services.sms.model.SendMessageItem;
import com.baidubce.services.sms.model.SendMessageV3Request;
import com.baidubce.services.sms.model.SendMessageV3Response;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import java.util.LinkedList;
import java.util.List;
/**
* @author zhuyijun
*/
public class BaiduBceSmsHandleGranter extends AbstractSmsGranter {
private final Logger log = LoggerFactory.getLogger(BaiduBceSmsHandleGranter.class);
private static final ProviderType PROVIDER_TYPE = ProviderType.BAIDU;
public BaiduBceSmsHandleGranter() {
super(PROVIDER_TYPE);
}
public BaiduBceSmsHandleGranter(ProviderType providerType) {
super(providerType);
}
private static final String ENDPOINT = "http://smsv3.bj.baidubce.com";
@Override
public TaskResultDTO sendSms(SmsRequest smsRequest) {
return getSmsResponse(smsRequest);
}
private TaskResultDTO getSmsResponse(SmsRequest smsRequest) {
SmsClient client = createClient(smsRequest);
SendMessageV3Request sendSmsRequest = new SendMessageV3Request();
sendSmsRequest.setTemplate(smsRequest.getTemplateCode());
sendSmsRequest.setContentVar(parseParam(smsRequest.getTemplateParams()));
sendSmsRequest.setSignatureId(smsRequest.getSignName());
sendSmsRequest.setCustom(smsRequest.getTaskId());
List<List<String>> phones = Lists.partition(smsRequest.getTelNums(), 200);
try {
List<SmsResponse> responses = new LinkedList<>();
for (List<String> phone : phones) {
sendSmsRequest.setMobile(String.join(",",phone));
SendMessageV3Response sendMessageV3Response = client.sendMessage(sendSmsRequest);
if (sendMessageV3Response != null && sendMessageV3Response.isSuccess()) {
List<SendMessageItem> data = sendMessageV3Response.getData();
for (SendMessageItem sendStatus : data) {
responses.add(SmsResponse.builder()
.code(sendStatus.getCode())
.phoneNumber(sendStatus.getMobile())
.bizId(sendStatus.getMessageId())
.sendStatus(ProviderType.BAIDU.getTaskStatus(sendStatus.getCode()))
.ext(smsRequest.getTaskId())
.message(sendStatus.getMessage())
.build());
}
}else if (sendMessageV3Response != null && !sendMessageV3Response.isSuccess()){
throw new SmsRuntimeException(sendMessageV3Response.getMessage(),sendMessageV3Response.getRequestId(),sendMessageV3Response.getCode());
} else {
throw new SmsRuntimeException("发送失败","-1","-1");
}
}
return new TaskResultDTO(responses, TaskStatus.SUCCESS);
}catch (SmsRuntimeException e) {
log.error("发送短信失败 错误信息:{} ",e.getMessage());
List<String> telNum1 = smsRequest.getTelNums();
List<SmsResponse> responses = new LinkedList<>();
for (String phone : telNum1) {
responses.add(SmsResponse.builder()
.code(e.getCode())
.sendStatus(SendStatus.FAIL)
.phoneNumber(phone)
.bizId(e.getRequestId())
.ext(smsRequest.getTaskId())
.message(e.getMessage())
.build());
}
return new TaskResultDTO(responses, TaskStatus.FAIL);
} catch (Exception e) {
log.error("发送短信失败 错误信息:{} ",e.getMessage());
List<String> telNum1 = smsRequest.getTelNums();
List<SmsResponse> responses = new LinkedList<>();
for (String phone : telNum1) {
responses.add(SmsResponse.builder()
.code("-1")
.sendStatus(SendStatus.FAIL)
.phoneNumber(phone)
.bizId("-1")
.ext(smsRequest.getTaskId())
.message(e.getMessage())
.build());
}
return new TaskResultDTO(responses, TaskStatus.FAIL);
}
}
@SneakyThrows
public static SmsClient createClient(SmsRequest smsRequest) {
SmsClientConfiguration config = new SmsClientConfiguration();
config.setCredentials(new DefaultBceCredentials(smsRequest.getAccessKey(),smsRequest.getAccessSecret()));
if (!StringUtils.isEmpty(smsRequest.getEndPoint())){
config.setEndpoint(smsRequest.getEndPoint());
} else {
config.setEndpoint(ENDPOINT);
}
return new SmsClient(config);
}
@Override
public SmsGranter getGranter(ProviderType providerType) {
if (PROVIDER_TYPE.equals(providerType)) {
return this;
}
return null;
}
}

View File

@ -22,6 +22,7 @@ import com.tencentcloudapi.sms.v20210111.models.SendStatus;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
@ -34,9 +35,11 @@ import java.util.Set;
* @author zhuyijun * @author zhuyijun
*/ */
public class TenSmsHandleGranter extends AbstractSmsGranter { public class TenSmsHandleGranter extends AbstractSmsGranter {
Logger log = LoggerFactory.getLogger(TenSmsHandleGranter.class); private static Logger log = LoggerFactory.getLogger(TenSmsHandleGranter.class);
private static final ProviderType PROVIDER_TYPE = ProviderType.TENCENT; private static final ProviderType PROVIDER_TYPE = ProviderType.TENCENT;
public TenSmsHandleGranter() {
super(PROVIDER_TYPE);
}
public TenSmsHandleGranter(ProviderType providerType) { public TenSmsHandleGranter(ProviderType providerType) {
super(providerType); super(providerType);
} }