package chat.sms;
|
|
import com.aliyuncs.CommonRequest;
|
import com.aliyuncs.CommonResponse;
|
import com.aliyuncs.DefaultAcsClient;
|
import com.aliyuncs.IAcsClient;
|
import com.aliyuncs.exceptions.ClientException;
|
import com.aliyuncs.exceptions.ServerException;
|
import com.aliyuncs.http.MethodType;
|
import com.aliyuncs.profile.DefaultProfile;
|
import com.google.gson.Gson;
|
|
import chat.consts.RestResult;
|
|
public class SmsService {
|
|
private static class AliyunCommonResponse {
|
String Message;
|
String Code;
|
}
|
|
public RestResult.RestCode sendAliyunCode(String mobile, String code) {
|
DefaultProfile profile = DefaultProfile.getProfile("default", "LTAI4GGfU6RdYxSFvQ3LiwSP", "gXh3SVeyOf2eoVTg7OkKutyC8rHg2a");
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
String templateparam = "{\"code\":\"" + code + "\"}";
|
CommonRequest request = new CommonRequest();
|
request.setMethod(MethodType.POST);
|
request.setDomain("dysmsapi.aliyuncs.com");
|
request.setVersion("2017-05-25");
|
request.setAction("SendSms");
|
request.putQueryParameter("PhoneNumbers", mobile);
|
request.putQueryParameter("SignName", "医视无忧");
|
|
request.putQueryParameter("TemplateCode", "SMS_206100254");
|
request.putQueryParameter("TemplateParam", templateparam);
|
try {
|
CommonResponse response = client.getCommonResponse(request);
|
System.out.println(response.getData());
|
if (response.getData() != null) {
|
AliyunCommonResponse aliyunCommonResponse = new Gson().fromJson(response.getData(), AliyunCommonResponse.class);
|
if (aliyunCommonResponse != null) {
|
if (aliyunCommonResponse.Code.equalsIgnoreCase("OK")) {
|
return RestResult.RestCode.SUCCESS;
|
} else {
|
System.out.println("Send aliyun sms failure with message:" + aliyunCommonResponse.Message);
|
}
|
}
|
}
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}
|
return RestResult.RestCode.ERROR_SERVER_ERROR;
|
}
|
}
|