package com.sinata.rest.core.aliyun.sms;
|
|
import com.alibaba.fastjson.JSON;
|
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.sinata.common.keys.AliyunConfig;
|
import com.sinata.rest.core.aliyun.sms.model.AliSmsCodeParam;
|
import com.sinata.rest.core.aliyun.sms.model.AliSmsResponse;
|
import lombok.extern.slf4j.Slf4j;
|
|
/**
|
* 阿里云短信接口
|
* <dependency>
|
* <groupId>com.aliyun</groupId>
|
* <artifactId>aliyun-java-sdk-core</artifactId>
|
* <version>4.0.3</version>
|
* </dependency>
|
*/
|
@Slf4j
|
public class AliSmsApi {
|
|
/**
|
* 阿里云账号配置
|
*/
|
private static final String accessKeyId = AliyunConfig.accessKeyId;
|
private static final String accessSecret = AliyunConfig.accessKeySecret;
|
|
// 签名
|
private static final String signName = AliyunConfig.signName;
|
|
// 短信验证码模板code
|
public static final String templateCodeVerify = AliyunConfig.templateCodeVerify;
|
public static final String templateCodeVerify_international = AliyunConfig.templateCodeVerify;
|
|
/**
|
* 发送短信验证码
|
*
|
* @param areaNo
|
* @param phone
|
* @param code
|
* @return
|
*/
|
public static String sendSms(String areaNo, String phone, String code) {
|
// 默认发送短信验证码
|
String templateCode = templateCodeVerify;
|
if (isInternationalSms(areaNo)) {
|
// 使用发送短信验证码(国际/港澳台)
|
templateCode = templateCodeVerify_international;
|
}
|
return send(areaNo, phone, JSON.toJSONString(new AliSmsCodeParam(code)), templateCode);
|
}
|
|
/**
|
* 发送短信验证码、短信通知
|
*
|
* @param areaNo 区号,非86则发送国际/港澳台短信
|
* @param phone 手机号
|
* @param templateParam 短信参数
|
* @param templateCode 短信模板
|
*/
|
public static String send(String areaNo, String phone, String templateParam, String templateCode) {
|
DefaultProfile profile = DefaultProfile.getProfile("default", accessKeyId, accessSecret);
|
IAcsClient client = new DefaultAcsClient(profile);
|
|
CommonRequest request = new CommonRequest();
|
//request.setProtocol(ProtocolType.HTTPS);
|
request.setMethod(MethodType.POST);
|
request.setDomain("dysmsapi.aliyuncs.com");
|
request.setVersion("2017-05-25");
|
request.setAction("SendSms");
|
|
if (isInternationalSms(areaNo)) {
|
// 国际/港澳台消息:国际区号+号码,例如85200000000
|
request.putQueryParameter("PhoneNumbers", areaNo + phone);
|
} else {
|
// 国内短信:11位手机号码,例如15951955195
|
request.putQueryParameter("PhoneNumbers", phone);
|
}
|
request.putQueryParameter("SignName", signName);
|
request.putQueryParameter("TemplateCode", templateCode);
|
request.putQueryParameter("TemplateParam", templateParam);
|
|
try {
|
CommonResponse response = client.getCommonResponse(request);
|
log.debug(response.getData());
|
AliSmsResponse data = JSON.parseObject(response.getData(), AliSmsResponse.class);
|
return data.getMessage() == null ? "NO" : data.getMessage();
|
} catch (ServerException e) {
|
e.printStackTrace();
|
} catch (ClientException e) {
|
e.printStackTrace();
|
}
|
return "NO";
|
}
|
|
/**
|
* 判断是否使用(国际/港澳台)短信,默认(否)使用国内短信
|
*
|
* @return
|
*/
|
public static boolean isInternationalSms(String areaNo) {
|
if (areaNo != null && !"86".equals(areaNo)) {
|
return true;
|
} else {
|
return false;
|
}
|
}
|
}
|