diff --git a/server/zyjblogs-sms/pom.xml b/server/zyjblogs-sms/pom.xml index 20703a9..d292677 100644 --- a/server/zyjblogs-sms/pom.xml +++ b/server/zyjblogs-sms/pom.xml @@ -22,10 +22,6 @@ cn.zyjblogs.starter zyjblogs-common-spring-boot-starter - - com.aliyun - alibabacloud-dysmsapi20170525 - cn.zyjblogs.starter zyjblogs-mybatisplus-spring-boot-starter @@ -38,10 +34,7 @@ cn.zyjblogs.starter zyjblogs-oauth-spring-boot-starter - - com.tencentcloudapi - tencentcloud-sdk-java-sms - + zyjblogs-web-spring-boot-starter cn.zyjblogs.starter @@ -54,5 +47,39 @@ com.alibaba.fastjson2 fastjson2 + + + + + com.aliyun + alibabacloud-dysmsapi20170525 + + + + com.tencentcloudapi + tencentcloud-sdk-java-sms + + + + com.baidubce + bce-java-sdk + 0.10.253 + + + log4j + log4j + + + slf4j-log4j12 + org.slf4j + + + org.apache.hbase + hbase-client + + + + + \ No newline at end of file diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/configuration/SmsConfiguration.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/configuration/SmsConfiguration.java index 2cfd89d..e3e41b7 100644 --- a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/configuration/SmsConfiguration.java +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/configuration/SmsConfiguration.java @@ -4,6 +4,7 @@ package cn.zyjblogs.sms.configuration; import cn.zyjblogs.sms.provider.CompositeSmsGranter; import cn.zyjblogs.sms.provider.SmsGranter; 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.task.constant.ProviderType; import org.springframework.context.annotation.Bean; @@ -22,6 +23,7 @@ public class SmsConfiguration { List list = new ArrayList<>(); list.add(new AliSmsHandleGranter(ProviderType.ALI)); list.add(new TenSmsHandleGranter(ProviderType.TENCENT)); + list.add(new BaiduBceSmsHandleGranter(ProviderType.BAIDU)); return new CompositeSmsGranter(list); } } diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/exception/SmsRuntimeException.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/exception/SmsRuntimeException.java index 617a26d..e4a8a8d 100644 --- a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/exception/SmsRuntimeException.java +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/exception/SmsRuntimeException.java @@ -8,10 +8,27 @@ import cn.zyjblogs.starter.common.exception.AbstractBusinessException; */ public class SmsRuntimeException extends AbstractBusinessException { + private String code; + private String requestId; + + public String getCode() { + return code; + } + + public String getRequestId() { + return requestId; + } + public SmsRuntimeException() { super(); } + public SmsRuntimeException(String message, String code, String requestId) { + super(message); + this.code = code; + this.requestId = requestId; + } + public SmsRuntimeException(String message) { super(message); } diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/CompositeSmsGranter.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/CompositeSmsGranter.java index ca64904..92cf996 100644 --- a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/CompositeSmsGranter.java +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/CompositeSmsGranter.java @@ -2,10 +2,11 @@ package cn.zyjblogs.sms.provider; 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.TaskStatus; import cn.zyjblogs.sms.task.dto.TaskResultDTO; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.LinkedList; @@ -38,7 +39,7 @@ public class CompositeSmsGranter implements SmsGranter { if (granter1 != null) { return granter1.sendSms(providerType, smsRequest); } - return new TaskResultDTO(new LinkedList<>(), TaskStatus.SUCCESS); + return new TaskResultDTO(new LinkedList<>(), TaskStatus.FAIL); } diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/aliyun/AliSmsHandleGranter.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/aliyun/AliSmsHandleGranter.java index e210f59..a730204 100644 --- a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/aliyun/AliSmsHandleGranter.java +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/aliyun/AliSmsHandleGranter.java @@ -19,6 +19,7 @@ import com.aliyun.sdk.service.dysmsapi20170525.models.SendSmsResponseBody; import darabonba.core.client.ClientOverrideConfiguration; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; @@ -34,7 +35,9 @@ import java.util.concurrent.CompletableFuture; @Slf4j public class AliSmsHandleGranter extends AbstractSmsGranter { private static final ProviderType PROVIDER_TYPE = ProviderType.ALI; - + public AliSmsHandleGranter() { + super(PROVIDER_TYPE); + } public AliSmsHandleGranter(ProviderType providerType) { super(providerType); } diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/baidu/BaiduBceSmsHandleGranter.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/baidu/BaiduBceSmsHandleGranter.java new file mode 100644 index 0000000..cf68d62 --- /dev/null +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/baidu/BaiduBceSmsHandleGranter.java @@ -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> phones = Lists.partition(smsRequest.getTelNums(), 200); + try { + List responses = new LinkedList<>(); + for (List phone : phones) { + sendSmsRequest.setMobile(String.join(",",phone)); + SendMessageV3Response sendMessageV3Response = client.sendMessage(sendSmsRequest); + if (sendMessageV3Response != null && sendMessageV3Response.isSuccess()) { + List 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 telNum1 = smsRequest.getTelNums(); + List 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 telNum1 = smsRequest.getTelNums(); + List 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; + } +} diff --git a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/tencent/TenSmsHandleGranter.java b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/tencent/TenSmsHandleGranter.java index 7b9320a..935e458 100644 --- a/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/tencent/TenSmsHandleGranter.java +++ b/server/zyjblogs-sms/src/main/java/cn/zyjblogs/sms/provider/tencent/TenSmsHandleGranter.java @@ -22,6 +22,7 @@ import com.tencentcloudapi.sms.v20210111.models.SendStatus; import lombok.SneakyThrows; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.HashMap; @@ -34,9 +35,11 @@ import java.util.Set; * @author zhuyijun */ 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; - + public TenSmsHandleGranter() { + super(PROVIDER_TYPE); + } public TenSmsHandleGranter(ProviderType providerType) { super(providerType); }