|
@@ -0,0 +1,45 @@
|
|
|
+package com.example.xiaoshiweixinback.service.common;
|
|
|
+
|
|
|
+import com.aliyuncs.DefaultAcsClient;
|
|
|
+import com.aliyuncs.IAcsClient;
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
|
|
|
+import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
|
|
|
+import com.aliyuncs.exceptions.ClientException;
|
|
|
+import com.aliyuncs.profile.DefaultProfile;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+@Component
|
|
|
+public class SmsService {
|
|
|
+
|
|
|
+ @Value("${SMS.regionId}")
|
|
|
+ private String regionId;
|
|
|
+
|
|
|
+ @Value("${SMS.accessKeyId}")
|
|
|
+ private String accessKeyId;
|
|
|
+
|
|
|
+ @Value("${SMS.secret}")
|
|
|
+ private String secret;
|
|
|
+
|
|
|
+ public void sendMessage(String phoneNum,String random){
|
|
|
+
|
|
|
+ DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
|
|
|
+ IAcsClient client = new DefaultAcsClient(profile);
|
|
|
+ SendSmsRequest request = new SendSmsRequest();
|
|
|
+// String templateParam = "{\"code\":\"" + random + "\",\"message\":\"" + "为您的登陆验证码" + "\",\"validTime\":\"" + "5分钟" + "\"}";
|
|
|
+ String templateParam = "{\"code\":\"" + random + "\"}";
|
|
|
+ request.setPhoneNumbers(phoneNum);//接收短信的手机号码
|
|
|
+ request.setSignName("小世数字科技");//短信签名名称
|
|
|
+ request.setTemplateCode("SMS_296725687");//短信模板CODE
|
|
|
+ request.setTemplateParam(templateParam);//短信模板变量对应的实际值
|
|
|
+ try {
|
|
|
+ SendSmsResponse response = client.getAcsResponse(request);
|
|
|
+ System.out.println(new Gson().toJson(response));
|
|
|
+ } catch (ClientException e) {
|
|
|
+ System.out.println("ErrCode:" + e.getErrCode());
|
|
|
+ System.out.println("ErrMsg:" + e.getErrMsg());
|
|
|
+ System.out.println("RequestId:" + e.getRequestId());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|