feat: 短信服务
This commit is contained in:
+12
@@ -21,6 +21,8 @@ public class SmsProperties {
|
||||
private String secretId;
|
||||
private String secretKey;
|
||||
private String signName;
|
||||
private String verifyCodeTemplateId;
|
||||
|
||||
public String getAppId() { return appId; }
|
||||
public void setAppId(String appId) { this.appId = appId; }
|
||||
public String getSecretId() { return secretId; }
|
||||
@@ -29,17 +31,27 @@ public class SmsProperties {
|
||||
public void setSecretKey(String secretKey) { this.secretKey = secretKey; }
|
||||
public String getSignName() { return signName; }
|
||||
public void setSignName(String signName) { this.signName = signName; }
|
||||
public String getVerifyCodeTemplateId() { return verifyCodeTemplateId; }
|
||||
public void setVerifyCodeTemplateId(String verifyCodeTemplateId) {
|
||||
this.verifyCodeTemplateId = verifyCodeTemplateId;
|
||||
}
|
||||
}
|
||||
|
||||
public static class AliyunConfig {
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String signName;
|
||||
private String verifyCodeTemplateId;
|
||||
|
||||
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; }
|
||||
public String getVerifyCodeTemplateId() { return verifyCodeTemplateId; }
|
||||
public void setVerifyCodeTemplateId(String verifyCodeTemplateId) {
|
||||
this.verifyCodeTemplateId = verifyCodeTemplateId;
|
||||
}
|
||||
}
|
||||
}
|
||||
+40
-20
@@ -1,5 +1,6 @@
|
||||
package com.central.sms.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.aliyuncs.DefaultAcsClient;
|
||||
import com.aliyuncs.IAcsClient;
|
||||
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
||||
@@ -16,6 +17,10 @@ import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 阿里云短信服务实现
|
||||
* @author system
|
||||
*/
|
||||
@Service
|
||||
public class AliyunSmsService implements ISmsService {
|
||||
|
||||
@@ -26,30 +31,45 @@ public class AliyunSmsService implements ISmsService {
|
||||
|
||||
@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);
|
||||
SmsProperties.AliyunConfig config = smsProperties.getAliyun();
|
||||
logger.info("[短信发送][服务商:ALIYUN] 手机号={}, 模板ID={}, 参数={}", phone, templateId, params);
|
||||
|
||||
// 创建DefaultAcsClient实例并初始化
|
||||
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()) {
|
||||
String templateParam = JSON.toJSONString(params);
|
||||
request.setTemplateParam(templateParam);
|
||||
logger.debug("[短信发送][服务商:ALIYUN] 模板参数JSON: {}", templateParam);
|
||||
}
|
||||
|
||||
// 发送短信
|
||||
SendSmsResponse response = client.getAcsResponse(request);
|
||||
logger.info("[阿里云短信] 发送结果: {}", response.getMessage());
|
||||
|
||||
logger.info("[短信发送][服务商:ALIYUN] 响应: code={}, message={}, requestId={}, bizId={}",
|
||||
response.getCode(), response.getMessage(), response.getRequestId(), response.getBizId());
|
||||
|
||||
return "OK".equalsIgnoreCase(response.getCode());
|
||||
} catch (ClientException e) {
|
||||
logger.error("[阿里云短信] 发送失败,手机号:{},模板:{},参数:{},异常:{}", phone, templateId, params, e.getMessage(), e);
|
||||
logger.error("[短信发送][服务商:ALIYUN] SDK异常,手机号:{},模板ID:{},参数:{},错误码:{},异常:{}",
|
||||
phone, templateId, params, e.getErrCode(), e.getMessage(), e);
|
||||
throw new SmsSendException("阿里云短信发送失败: " + e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
logger.error("[短信发送][服务商:ALIYUN] 失败,手机号:{},模板ID:{},参数:{},异常:{}",
|
||||
phone, templateId, params, e.getMessage(), e);
|
||||
throw new SmsSendException("阿里云短信发送失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
+42
-7
@@ -4,8 +4,13 @@ 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.exception.TencentCloudSDKException;
|
||||
import com.tencentcloudapi.common.profile.ClientProfile;
|
||||
import com.tencentcloudapi.common.profile.HttpProfile;
|
||||
import com.tencentcloudapi.sms.v20210111.SmsClient;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
|
||||
import com.tencentcloudapi.sms.v20210111.models.SendStatus;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -26,26 +31,56 @@ public class TencentSmsService implements ISmsService {
|
||||
try {
|
||||
SmsProperties.TencentConfig config = smsProperties.getTencent();
|
||||
logger.info("[短信发送][服务商:TENCENT] 手机号={}, 模板ID={}, 参数={}", phone, templateId, params);
|
||||
|
||||
// 实例化认证对象
|
||||
Credential cred = new Credential(config.getSecretId(), config.getSecretKey());
|
||||
|
||||
// 实例化http配置
|
||||
HttpProfile httpProfile = new HttpProfile();
|
||||
httpProfile.setEndpoint("sms.tencentcloudapi.com");
|
||||
|
||||
// 实例化客户端配置
|
||||
ClientProfile clientProfile = new ClientProfile();
|
||||
clientProfile.setHttpProfile(httpProfile);
|
||||
/*SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);
|
||||
|
||||
// 实例化SMS客户端
|
||||
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]));
|
||||
|
||||
// 设置手机号(需要加+86前缀)
|
||||
String[] phoneNumbers = new String[]{"+86" + phone};
|
||||
req.setPhoneNumberSet(phoneNumbers);
|
||||
|
||||
// 设置模板参数
|
||||
if (params != null && !params.isEmpty()) {
|
||||
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;
|
||||
SendStatus[] statusSet = resp.getSendStatusSet();
|
||||
|
||||
if (statusSet != null && statusSet.length > 0) {
|
||||
SendStatus status = statusSet[0];
|
||||
logger.info("[短信发送][服务商:TENCENT] 响应: code={}, message={}, requestId={}",
|
||||
status.getCode(), status.getMessage(), resp.getRequestId());
|
||||
return "Ok".equalsIgnoreCase(status.getCode());
|
||||
}
|
||||
|
||||
logger.warn("[短信发送][服务商:TENCENT] 响应状态集为空");
|
||||
return false;
|
||||
} catch (TencentCloudSDKException e) {
|
||||
logger.error("[短信发送][服务商:TENCENT] SDK异常,手机号:{},模板ID:{},参数:{},错误码:{},异常:{}",
|
||||
phone, templateId, params, e.getErrorCode(), e.getMessage(), e);
|
||||
throw new SmsSendException("腾讯云短信发送失败: " + e.getMessage(), e);
|
||||
} catch (Exception e) {
|
||||
logger.error("[短信发送][服务商:TENCENT] 失败,手机号:{},模板ID:{},参数:{},异常:{}", phone, templateId, params, e.getMessage(), e);
|
||||
logger.error("[短信发送][服务商:TENCENT] 失败,手机号:{},模板ID:{},参数:{},异常:{}",
|
||||
phone, templateId, params, e.getMessage(), e);
|
||||
throw new SmsSendException("腾讯云短信发送失败", e);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user