fix(短信服务): 服务商短信sdk集成

This commit is contained in:
2025-07-15 22:53:39 +08:00
parent d49a90bed6
commit b8b580c366
16 changed files with 337 additions and 19 deletions
+1
View File
@@ -21,6 +21,7 @@
<module>zlt-common-core</module>
<module>zlt-elasticsearch-spring-boot-starter</module>
<module>zlt-oss-spring-boot-starter</module>
<module>zlt-sms-spring-boot-starter</module>
<module>zlt-zookeeper-spring-boot-starter</module>
<module>zlt-wechat-common-core</module>
<module>zlt-wechat-spring-boot-starter</module>
@@ -0,0 +1,58 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.zlt</groupId>
<artifactId>zlt-commons</artifactId>
<version>6.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<packaging>jar</packaging>
<artifactId>zlt-sms-spring-boot-starter</artifactId>
<description>短信服务通用组件</description>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java</artifactId>
<version>3.1.825</version>
</dependency>
<!-- 腾讯云短信SDK依赖可后续补充 -->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.5.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.83</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.4</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.tencentcloudapi</groupId>
<artifactId>tencentcloud-sdk-java-common</artifactId>
<version>3.1.1040</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,19 @@
package com.central.sms.config;
import com.central.sms.service.ISmsService;
import com.central.sms.service.SmsServiceFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(SmsProperties.class)
public class SmsAutoConfigure {
@Bean
@ConditionalOnMissingBean(ISmsService.class)
public ISmsService smsService(SmsServiceFactory factory) {
return factory.getSmsService();
}
}
@@ -0,0 +1,45 @@
package com.central.sms.config;
import com.central.sms.enums.SmsProviderType;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "sms")
public class SmsProperties {
private SmsProviderType provider = SmsProviderType.TENCENT;
private TencentConfig tencent = new TencentConfig();
private AliyunConfig aliyun = new AliyunConfig();
public SmsProviderType getProvider() { return provider; }
public void setProvider(SmsProviderType provider) { this.provider = provider; }
public TencentConfig getTencent() { return tencent; }
public void setTencent(TencentConfig tencent) { this.tencent = tencent; }
public AliyunConfig getAliyun() { return aliyun; }
public void setAliyun(AliyunConfig aliyun) { this.aliyun = aliyun; }
public static class TencentConfig {
private String appId;
private String secretId;
private String secretKey;
private String signName;
public String getAppId() { return appId; }
public void setAppId(String appId) { this.appId = appId; }
public String getSecretId() { return secretId; }
public void setSecretId(String secretId) { this.secretId = secretId; }
public String getSecretKey() { return secretKey; }
public void setSecretKey(String secretKey) { this.secretKey = secretKey; }
public String getSignName() { return signName; }
public void setSignName(String signName) { this.signName = signName; }
}
public static class AliyunConfig {
private String accessKeyId;
private String accessKeySecret;
private String signName;
public String getAccessKeyId() { return accessKeyId; }
public void setAccessKeyId(String accessKeyId) { this.accessKeyId = accessKeyId; }
public String getAccessKeySecret() { return accessKeySecret; }
public void setAccessKeySecret(String accessKeySecret) { this.accessKeySecret = accessKeySecret; }
public String getSignName() { return signName; }
public void setSignName(String signName) { this.signName = signName; }
}
}
@@ -0,0 +1,6 @@
package com.central.sms.enums;
public enum SmsProviderType {
TENCENT,
ALIYUN
}
@@ -0,0 +1,7 @@
package com.central.sms.exception;
public class SmsSendException extends RuntimeException {
public SmsSendException(String message, Throwable cause) {
super(message, cause);
}
}
@@ -0,0 +1,14 @@
package com.central.sms.service;
import java.util.Map;
public interface ISmsService {
/**
* 发送短信
* @param phone 手机号
* @param templateId 短信模板ID
* @param params 模板参数
* @return 是否发送成功
*/
boolean sendSms(String phone, String templateId, Map<String, String> params);
}
@@ -0,0 +1,30 @@
package com.central.sms.service;
import com.central.sms.config.SmsProperties;
import com.central.sms.enums.SmsProviderType;
import com.central.sms.service.impl.AliyunSmsService;
import com.central.sms.service.impl.TencentSmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class SmsServiceFactory {
@Autowired
private TencentSmsService tencentSmsService;
@Autowired
private AliyunSmsService aliyunSmsService;
@Autowired
private SmsProperties smsProperties;
public ISmsService getSmsService() {
SmsProviderType type = smsProperties.getProvider();
switch (type) {
case ALIYUN:
return aliyunSmsService;
case TENCENT:
default:
return tencentSmsService;
}
}
}
@@ -0,0 +1,56 @@
package com.central.sms.service.impl;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.central.sms.config.SmsProperties;
import com.central.sms.exception.SmsSendException;
import com.central.sms.service.ISmsService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class AliyunSmsService implements ISmsService {
private static final Logger logger = LoggerFactory.getLogger(AliyunSmsService.class);
@Autowired
private SmsProperties smsProperties;
@Override
public boolean sendSms(String phone, String templateId, Map<String, String> params) {
SmsProperties.AliyunConfig config = smsProperties.getAliyun();
DefaultProfile profile = DefaultProfile.getProfile(
"cn-hangzhou", // 地域ID
config.getAccessKeyId(),
config.getAccessKeySecret()
);
IAcsClient client = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);
request.setSignName(config.getSignName());
request.setTemplateCode(templateId);
// 阿里云短信模板参数为JSON字符串
if (params != null && !params.isEmpty()) {
request.setTemplateParam(com.alibaba.fastjson.JSON.toJSONString(params));
}
try {
logger.info("[阿里云短信] 发送请求: 手机号={}, 模板={}, 参数={}", phone, templateId, params);
SendSmsResponse response = client.getAcsResponse(request);
logger.info("[阿里云短信] 发送结果: {}", response.getMessage());
return "OK".equalsIgnoreCase(response.getCode());
} catch (ClientException e) {
logger.error("[阿里云短信] 发送失败,手机号:{},模板:{},参数:{},异常:{}", phone, templateId, params, e.getMessage(), e);
throw new SmsSendException("阿里云短信发送失败", e);
}
}
}
@@ -0,0 +1,52 @@
package com.central.sms.service.impl;
import com.central.sms.config.SmsProperties;
import com.central.sms.exception.SmsSendException;
import com.central.sms.service.ISmsService;
import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;
@Service
public class TencentSmsService implements ISmsService {
private static final Logger logger = LoggerFactory.getLogger(TencentSmsService.class);
@Autowired
private SmsProperties smsProperties;
@Override
public boolean sendSms(String phone, String templateId, Map<String, String> params) {
try {
SmsProperties.TencentConfig config = smsProperties.getTencent();
logger.info("[短信发送][服务商:TENCENT] 手机号={}, 模板ID={}, 参数={}", phone, templateId, params);
Credential cred = new Credential(config.getSecretId(), config.getSecretKey());
HttpProfile httpProfile = new HttpProfile();
httpProfile.setEndpoint("sms.tencentcloudapi.com");
ClientProfile clientProfile = new ClientProfile();
clientProfile.setHttpProfile(httpProfile);
/*SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
SendSmsRequest req = new SendSmsRequest();
req.setSmsSdkAppId(config.getAppId());
req.setSignName(config.getSignName());
req.setTemplateId(templateId);
req.setPhoneNumberSet(new String[]{phone});
req.setTemplateParamSet(params.values().toArray(new String[0]));
SendSmsResponse resp = client.SendSms(req);
logger.info("[短信发送][服务商:TENCENT] 响应: code={}, message={}", resp.getSendStatusSet()[0].getCode(), resp.getSendStatusSet()[0].getMessage());
return "Ok".equalsIgnoreCase(resp.getSendStatusSet()[0].getCode());*/
return true;
} catch (Exception e) {
logger.error("[短信发送][服务商:TENCENT] 失败,手机号:{},模板ID:{},参数:{},异常:{}", phone, templateId, params, e.getMessage(), e);
throw new SmsSendException("腾讯云短信发送失败", e);
}
}
}