|
@@ -0,0 +1,69 @@
|
|
|
|
|
+package com.ydtech.components.aliyun;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+import com.aliyun.dysmsapi20170525.Client;
|
|
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
|
|
+import com.ydtech.exception.SystemException;
|
|
|
|
|
+import com.ydtech.utils.aliyun.SendSms;
|
|
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Component
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class SendSmsComponents {
|
|
|
|
|
+
|
|
|
|
|
+ private final AliyunApiSmsProperties properties;
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @description: TODO
|
|
|
|
|
+ * @author: wenks
|
|
|
|
|
+ * @date: 2024/11/6 8:46
|
|
|
|
|
+ * @return:
|
|
|
|
|
+ **/
|
|
|
|
|
+ private Client createClient() throws Exception {
|
|
|
|
|
+ // 工程代码泄露可能会导致 AccessKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考。
|
|
|
|
|
+ // 建议使用更安全的 STS 方式,更多鉴权访问方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
|
|
|
|
+ Config config = new Config()
|
|
|
|
|
+ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_ID。
|
|
|
|
|
+ .setAccessKeyId(properties.getAccessKeyId())
|
|
|
|
|
+ // 必填,请确保代码运行环境设置了环境变量 ALIBABA_CLOUD_ACCESS_KEY_SECRET。
|
|
|
|
|
+ .setAccessKeySecret(properties.getAccessKeyId());
|
|
|
|
|
+ // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
|
|
|
|
+ config.endpoint = "dysmsapi.aliyuncs.com";
|
|
|
|
|
+ return new Client(config);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ public void sendSms(String phoneNumber, SendSmsEnum sendSmsEnum) throws Exception {
|
|
|
|
|
+ Client client = SendSms.createClient();
|
|
|
|
|
+ SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
|
|
|
|
+ .setPhoneNumbers(phoneNumber)
|
|
|
|
|
+ .setSignName(properties.getSignName())
|
|
|
|
|
+ .setTemplateCode(sendSmsEnum.getCode());
|
|
|
|
|
+
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 复制代码运行请自行打印 API 的返回值
|
|
|
|
|
+ client.sendSmsWithOptions(sendSmsRequest, new RuntimeOptions());
|
|
|
|
|
+ } catch (TeaException error) {
|
|
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
|
|
+ // 错误 message
|
|
|
|
|
+ log.info(error.getMessage());
|
|
|
|
|
+ // 诊断地址
|
|
|
|
|
+ log.info((String) error.getData().get("Recommend"));
|
|
|
|
|
+ } catch (Exception _error) {
|
|
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
|
|
+ // 错误 message
|
|
|
|
|
+ log.info(error.getMessage());
|
|
|
|
|
+ // 诊断地址
|
|
|
|
|
+ log.info((String) error.getData().get("Recommend"));
|
|
|
|
|
+ }
|
|
|
|
|
+ throw new SystemException("发送短信异常请重试");
|
|
|
|
|
+ }
|
|
|
|
|
+}
|