fix(短信服务): 服务商短信sdk集成
This commit is contained in:
+19
@@ -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();
|
||||
}
|
||||
}
|
||||
+45
@@ -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; }
|
||||
}
|
||||
}
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
package com.central.sms.enums;
|
||||
|
||||
public enum SmsProviderType {
|
||||
TENCENT,
|
||||
ALIYUN
|
||||
}
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
package com.central.sms.exception;
|
||||
|
||||
public class SmsSendException extends RuntimeException {
|
||||
public SmsSendException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
}
|
||||
+14
@@ -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);
|
||||
}
|
||||
+30
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+56
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+52
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
com.central.sms.config.SmsAutoConfigure
|
||||
Reference in New Issue
Block a user