|
|
@@ -0,0 +1,62 @@
|
|
|
+package com.skyversation.xjcy.service;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
|
|
|
+import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
|
|
|
+import com.aliyun.teaopenapi.models.Config;
|
|
|
+import com.aliyun.dysmsapi20170525.Client;
|
|
|
+import com.aliyun.teautil.models.RuntimeOptions;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+
|
|
|
+@Service
|
|
|
+public class AlyPhoneMessageSendService implements PhoneMessageSendService {
|
|
|
+ private Client client;
|
|
|
+
|
|
|
+ @Value("${app.aly.message.key}")
|
|
|
+ private String key;
|
|
|
+ @Value("${app.aly.message.secret}")
|
|
|
+ private String secret;
|
|
|
+ @Value("${app.aly.endpoint}")
|
|
|
+ private String endpoint;
|
|
|
+
|
|
|
+ @Value("${app.aly.code.sign}")
|
|
|
+ private String codeSign;
|
|
|
+ @Value("${app.aly.code.template-code}")
|
|
|
+ private String templateCode;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() throws Exception {
|
|
|
+ Config config = new Config();
|
|
|
+ config.setAccessKeyId(key);
|
|
|
+ config.setAccessKeySecret(secret);
|
|
|
+ config.setEndpoint(endpoint);
|
|
|
+ this.client = new Client(config);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean sendMessage(String phone, String code) {
|
|
|
+ System.out.println(this);
|
|
|
+ System.out.println("通过阿里云服务发送短信");
|
|
|
+ System.out.println("发送到手机号---->"+phone);
|
|
|
+ System.out.println("验证码---->"+ code);
|
|
|
+
|
|
|
+ JSONObject obj = new JSONObject();
|
|
|
+ obj.put("code", code);
|
|
|
+
|
|
|
+ SendSmsRequest sendSmsRequest = new SendSmsRequest()
|
|
|
+ .setPhoneNumbers(phone)
|
|
|
+ .setSignName(codeSign)
|
|
|
+ .setTemplateCode(templateCode)
|
|
|
+ .setTemplateParam(obj.toJSONString());
|
|
|
+ try {
|
|
|
+ SendSmsResponse sendSmsResponse = this.client.sendSms(sendSmsRequest);
|
|
|
+ return sendSmsResponse.getStatusCode() == 200;
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|