|
|
@@ -0,0 +1,78 @@ |
|
|
|
package com.hp.user.service.utils; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Value; |
|
|
|
import org.springframework.boot.ApplicationArguments; |
|
|
|
import org.springframework.boot.ApplicationRunner; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
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.profile.DefaultProfile; |
|
|
|
import com.google.gson.Gson; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class SendSmsUtil implements ApplicationRunner{ |
|
|
|
|
|
|
|
@Value("${aliyun.regionId}") |
|
|
|
private String regionId; |
|
|
|
@Value("${aliyun.accessKeyId}") |
|
|
|
private String accessKeyId; |
|
|
|
@Value("${aliyun.secret}") |
|
|
|
private String secret; |
|
|
|
@Value("${aliyun.signName}") |
|
|
|
private String signName; |
|
|
|
@Value("${aliyun.templateCode}") |
|
|
|
private String templateCode; |
|
|
|
|
|
|
|
private DefaultProfile profile = null; |
|
|
|
|
|
|
|
@Override |
|
|
|
public void run(ApplicationArguments args) throws Exception { |
|
|
|
if(profile == null) { |
|
|
|
profile = DefaultProfile.getProfile(regionId, accessKeyId, secret); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 验证码发送 |
|
|
|
* @param phoneNumbers 手机号码 |
|
|
|
* @param code 6位随机验证码 |
|
|
|
* @return |
|
|
|
* @throws ServerException |
|
|
|
* @throws ClientException |
|
|
|
*/ |
|
|
|
public Map sendVerificationCode(String phoneNumbers,String code) throws ServerException, ClientException { |
|
|
|
IAcsClient client = new DefaultAcsClient(profile); |
|
|
|
CommonRequest request = new CommonRequest(); |
|
|
|
request.setSysDomain("dysmsapi.aliyuncs.com"); |
|
|
|
request.setSysVersion("2017-05-25"); |
|
|
|
request.setSysAction("SendSms"); |
|
|
|
// 接收短信的手机号码 |
|
|
|
request.putQueryParameter("PhoneNumbers", phoneNumbers); |
|
|
|
// 短信签名名称。请在控制台签名管理页面签名名称一列查看(必须是已添加、并通过审核的短信签名)。 |
|
|
|
request.putQueryParameter("SignName", signName); |
|
|
|
// 短信模板ID |
|
|
|
request.putQueryParameter("TemplateCode", templateCode); |
|
|
|
// 短信模板变量对应的实际值,JSON格式。 |
|
|
|
JSONObject codeJson = new JSONObject(); |
|
|
|
codeJson.put("code", code); |
|
|
|
request.putQueryParameter("TemplateParam", codeJson.toJSONString()); |
|
|
|
CommonResponse commonResponse = client.getCommonResponse(request); |
|
|
|
String data = commonResponse.getData(); |
|
|
|
String sData = data.replaceAll("'\'", ""); |
|
|
|
// log_print("sendSms", sData); |
|
|
|
Gson gson = new Gson(); |
|
|
|
Map map = gson.fromJson(sData, Map.class); |
|
|
|
return map; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |