|
@@ -0,0 +1,88 @@
|
|
|
|
|
+package com.ydtech.components.tencent;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+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 com.ydtech.exception.SystemException;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.retry.annotation.Retryable;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class TencentSendSmsComponents {
|
|
|
|
|
+
|
|
|
|
|
+ private final TencentApiProperties tencentApiProperties;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 发送短信
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param phoneNumber 手机号数组(需包含国际区号,例如 "+8613711112222")
|
|
|
|
|
+ * @param templateParam 模板参数数组,需要与模板内容变量一一对应
|
|
|
|
|
+ * @return 发送请求ID,可用于日志追踪;发送失败时返回null。
|
|
|
|
|
+ */
|
|
|
|
|
+ public String sendSms(String phoneNumber, String templateParam) {
|
|
|
|
|
+ phoneNumber = "+86" + phoneNumber;
|
|
|
|
|
+ String[] phoneNumberSet = new String[]{phoneNumber};
|
|
|
|
|
+ String[] templateParamSet = new String[]{templateParam};
|
|
|
|
|
+ SmsClient client = getSmsClient();
|
|
|
|
|
+ SendSmsRequest req = new SendSmsRequest();
|
|
|
|
|
+ // 使用注入的配置设置参数
|
|
|
|
|
+ req.setSmsSdkAppId(tencentApiProperties.getSdkAppId());
|
|
|
|
|
+ req.setSignName(tencentApiProperties.getSignName());
|
|
|
|
|
+ req.setTemplateId(tencentApiProperties.getTemplateId());
|
|
|
|
|
+ req.setTemplateParamSet(templateParamSet);
|
|
|
|
|
+ req.setPhoneNumberSet(phoneNumberSet);
|
|
|
|
|
+ req.setSessionContext("");
|
|
|
|
|
+ req.setExtendCode("");
|
|
|
|
|
+ req.setSenderId("");
|
|
|
|
|
+ try {
|
|
|
|
|
+ log.info("尝试发送短信,目标手机号:{}", Arrays.toString(phoneNumberSet));
|
|
|
|
|
+ SendSmsResponse res = client.SendSms(req);
|
|
|
|
|
+ log.debug("腾讯云短信接口原始响应:{}", SendSmsResponse.toJsonString(res));
|
|
|
|
|
+
|
|
|
|
|
+ // 检查发送结果
|
|
|
|
|
+ if (res.getSendStatusSet() != null && res.getSendStatusSet().length > 0) {
|
|
|
|
|
+ SendStatus status = res.getSendStatusSet()[0];
|
|
|
|
|
+ if ("Ok".equals(status.getCode())) {
|
|
|
|
|
+ log.info("短信发送成功!请求ID:{}, 序列号:{}", res.getRequestId(), status.getSerialNo());
|
|
|
|
|
+ return res.getRequestId(); // 返回请求ID用于追踪
|
|
|
|
|
+ } else {
|
|
|
|
|
+ log.error("短信发送失败。错误码:{}, 错误信息:{}", status.getCode(), status.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return null;
|
|
|
|
|
+
|
|
|
|
|
+ } catch (TencentCloudSDKException e) {
|
|
|
|
|
+ log.error("调用腾讯云短信服务SDK失败,错误信息:{}", e.getMessage(), e);
|
|
|
|
|
+ // 重试后仍失败,抛出运行时异常或自定义异常
|
|
|
|
|
+ throw new SystemException("短信服务暂时不可用,请稍后重试" + e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private SmsClient getSmsClient() {
|
|
|
|
|
+ Credential cred = new Credential(tencentApiProperties.getSecretId(), tencentApiProperties.getSecretKey());
|
|
|
|
|
+ HttpProfile httpProfile = new HttpProfile();
|
|
|
|
|
+ httpProfile.setEndpoint(tencentApiProperties.getEndpoint());
|
|
|
|
|
+ httpProfile.setReqMethod("GET");
|
|
|
|
|
+ httpProfile.setConnTimeout(10);
|
|
|
|
|
+ httpProfile.setWriteTimeout(10);
|
|
|
|
|
+ httpProfile.setReadTimeout(10);
|
|
|
|
|
+ ClientProfile clientProfile = new ClientProfile();
|
|
|
|
|
+ clientProfile.setSignMethod("HmacSHA256");
|
|
|
|
|
+ clientProfile.setHttpProfile(httpProfile);
|
|
|
|
|
+ // 使用注入的配置创建客户端
|
|
|
|
|
+ return new SmsClient(cred, tencentApiProperties.getRegion(), clientProfile);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|