|
@@ -0,0 +1,80 @@
|
|
|
+package com.rongwei.bscommon.sys.service.impl;
|
|
|
+
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
+import com.aliyun.tea.TeaException;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import com.rongwei.bscommon.sys.service.AliyunSmsService;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+@Service
|
|
|
+@Slf4j
|
|
|
+public class AliyunSmsServiceImpl implements AliyunSmsService {
|
|
|
+
|
|
|
+ @Value("${aliyun-sms.access-key-id:#{null}}")
|
|
|
+ private String accessKeyId;
|
|
|
+ @Value("${aliyun-sms.access-key-secret:#{null}}")
|
|
|
+ private String accessKeySecret;
|
|
|
+ @Value("${aliyun-sms.endpoint:#{null}}")
|
|
|
+ private String endpoint;
|
|
|
+ @Value("${aliyun-sms.sign-name:#{null}}")
|
|
|
+ private String signName;
|
|
|
+
|
|
|
+ private com.aliyun.dysmsapi20170525.Client createClient() throws Exception {
|
|
|
+ // 工程代码建议使用更安全的无AK方式,凭据配置方式请参见:https://help.aliyun.com/document_detail/378657.html。
|
|
|
+ com.aliyun.credentials.Client credential = new com.aliyun.credentials.Client();
|
|
|
+ com.aliyun.teaopenapi.models.Config config = new com.aliyun.teaopenapi.models.Config()
|
|
|
+ .setCredential(credential);
|
|
|
+ config.accessKeyId = accessKeyId;
|
|
|
+ config.accessKeySecret = accessKeySecret;
|
|
|
+ // Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi
|
|
|
+ config.endpoint = endpoint;
|
|
|
+ return new com.aliyun.dysmsapi20170525.Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 发送短信
|
|
|
+ *
|
|
|
+ * @param templateId 模板ID
|
|
|
+ * @param templateParamMap 模板参数
|
|
|
+ * @param phone 手机号
|
|
|
+ * @return 发送结果
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public SendSmsResponse sendSms(String templateId,
|
|
|
+ Map<String, String> templateParamMap, String phone) throws Exception {
|
|
|
+ String templateParam = com.aliyun.teautil.Common.toJSONString(templateParamMap);
|
|
|
+ com.aliyun.dysmsapi20170525.Client client = createClient();
|
|
|
+ SendSmsRequest sendSmsRequest = new SendSmsRequest();
|
|
|
+ sendSmsRequest.setSignName(signName);
|
|
|
+ sendSmsRequest.setTemplateCode(templateId);
|
|
|
+ sendSmsRequest.setTemplateParam(templateParam);
|
|
|
+ sendSmsRequest.setPhoneNumbers(phone);
|
|
|
+ RuntimeOptions runtime = new RuntimeOptions();
|
|
|
+ SendSmsResponse resp = null;
|
|
|
+ try {
|
|
|
+ resp = client.sendSmsWithOptions(sendSmsRequest, runtime);
|
|
|
+ log.info("发送短信结果:{}", com.aliyun.teautil.Common.toJSONString(resp));
|
|
|
+ } catch (TeaException error) {
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ log.error("发送短信结果:{}", error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ log.error("诊断地址:{}", error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ } catch (Exception _error) {
|
|
|
+ TeaException error = new TeaException(_error.getMessage(), _error);
|
|
|
+ // 此处仅做打印展示,请谨慎对待异常处理,在工程项目中切勿直接忽略异常。
|
|
|
+ // 错误 message
|
|
|
+ log.error("发送短信结果:{}", error.getMessage());
|
|
|
+ // 诊断地址
|
|
|
+ log.error("诊断地址:{}", error.getData().get("Recommend"));
|
|
|
+ com.aliyun.teautil.Common.assertAsString(error.message);
|
|
|
+ }
|
|
|
+ return resp;
|
|
|
+ }
|
|
|
+}
|