|
|
|
@ -1,30 +1,28 @@ |
|
|
|
|
package com.blockchain.server.user.common.utils; |
|
|
|
|
|
|
|
|
|
import com.aliyuncs.CommonRequest; |
|
|
|
|
import com.aliyuncs.CommonResponse; |
|
|
|
|
import com.aliyuncs.DefaultAcsClient; |
|
|
|
|
import com.aliyuncs.IAcsClient; |
|
|
|
|
import com.aliyuncs.http.MethodType; |
|
|
|
|
import com.aliyuncs.profile.DefaultProfile; |
|
|
|
|
import lombok.Data; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
|
|
|
|
import org.springframework.context.annotation.Configuration; |
|
|
|
|
import org.springframework.http.ResponseEntity; |
|
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
import org.springframework.util.LinkedMultiValueMap; |
|
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
|
|
import org.springframework.web.client.RestTemplate; |
|
|
|
|
|
|
|
|
|
import java.util.Arrays; |
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 短信API服务调用 - 阿里云短信 |
|
|
|
|
* 短信API服务调用 |
|
|
|
|
**/ |
|
|
|
|
@Data |
|
|
|
|
@Configuration |
|
|
|
|
@Component |
|
|
|
|
@ConfigurationProperties(prefix = "aliyunsms") |
|
|
|
|
@ConfigurationProperties(prefix = "feigesms") |
|
|
|
|
public class SendSmsgCode { |
|
|
|
|
@Autowired |
|
|
|
|
private RestTemplate restTemplate; |
|
|
|
@ -34,93 +32,69 @@ public class SendSmsgCode { |
|
|
|
|
private boolean closed;//是否开启
|
|
|
|
|
private long timeout;//超时时间,单位:分钟
|
|
|
|
|
|
|
|
|
|
//阿里云
|
|
|
|
|
private String ACCESS_KEY_ID; |
|
|
|
|
private String ACCESS_SECRET; |
|
|
|
|
private String ENGLISH_MSG; |
|
|
|
|
private String BIG5_MSG; |
|
|
|
|
private String SginName; |
|
|
|
|
private String TemplateCode; |
|
|
|
|
//参数
|
|
|
|
|
private String URL; |
|
|
|
|
private String Account; |
|
|
|
|
private String Pwd; |
|
|
|
|
private String TemplateId_ZH_CN; |
|
|
|
|
private String SignId_ZH_CN; |
|
|
|
|
|
|
|
|
|
private String MSG_CODE_PLACEHOLDER = "${code}"; |
|
|
|
|
private String CHINA_CODE = "86"; |
|
|
|
|
private String BIG5_CODE = "852,853,886"; |
|
|
|
|
|
|
|
|
|
//参数
|
|
|
|
|
private String TemplateId_ZH_HK; |
|
|
|
|
private String TemplateId_EN_US; |
|
|
|
|
private String SignId_EN_US; |
|
|
|
|
private String SignId_ZH_HK; |
|
|
|
|
|
|
|
|
|
private List<String> BIG5_CODE = Arrays.asList(new String[]{"852", "853", "886"}); |
|
|
|
|
/** |
|
|
|
|
* 请求阿里云短信接口,发送短信验证码 |
|
|
|
|
* 请求短信接口,发送短信验证码 |
|
|
|
|
* |
|
|
|
|
* @param mobile 手机号 |
|
|
|
|
* @param smsCode 短信验证码 |
|
|
|
|
* @param templateId 模板ID |
|
|
|
|
* @param mobile 手机号 |
|
|
|
|
* @param smsCode 短信验证码 |
|
|
|
|
* @return |
|
|
|
|
*/ |
|
|
|
|
@Async |
|
|
|
|
public void sendSmsg(String mobile, String smsCode, String internationalCode, String templateId) { |
|
|
|
|
public void sendSmsg(String mobile, String smsCode, String internationalCode) { |
|
|
|
|
if (closed) {//如果没有开启短信,则不发送短信
|
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
DefaultProfile profile = DefaultProfile.getProfile("ap-southeast-1", ACCESS_KEY_ID, ACCESS_SECRET); |
|
|
|
|
IAcsClient client = new DefaultAcsClient(profile); |
|
|
|
|
CommonRequest request = new CommonRequest(); |
|
|
|
|
request.setMethod(MethodType.POST); |
|
|
|
|
//域名,请勿修改
|
|
|
|
|
request.setDomain("dysmsapi.ap-southeast-1.aliyuncs.com"); |
|
|
|
|
//API版本号,请勿修改
|
|
|
|
|
request.setVersion("2018-05-01"); |
|
|
|
|
//接收号码,格式为:国际码+号码,必填
|
|
|
|
|
request.putQueryParameter("To", internationalCode + mobile); |
|
|
|
|
|
|
|
|
|
//国际代码
|
|
|
|
|
MultiValueMap<String, String> requestEntity = new LinkedMultiValueMap<>(); |
|
|
|
|
requestEntity.add("Account", Account); |
|
|
|
|
requestEntity.add("Pwd", Pwd); |
|
|
|
|
requestEntity.add("Content", smsCode); |
|
|
|
|
//国内手机
|
|
|
|
|
if (CHINA_CODE.equals(internationalCode)) { |
|
|
|
|
//请求国内服务器发送短信
|
|
|
|
|
sendTelCode(mobile, smsCode, internationalCode, templateId); |
|
|
|
|
requestEntity.add("Mobile", mobile); |
|
|
|
|
requestEntity.add("TemplateId", TemplateId_ZH_CN); |
|
|
|
|
requestEntity.add("SignId", SignId_ZH_CN); |
|
|
|
|
|
|
|
|
|
// //API名称
|
|
|
|
|
// request.setAction("SendMessageWithTemplate");
|
|
|
|
|
//
|
|
|
|
|
// request.putQueryParameter("From", SginName);
|
|
|
|
|
// request.putQueryParameter("TemplateCode", TemplateCode);
|
|
|
|
|
// request.putQueryParameter("TemplateParam", "{\"code\":\"" + smsCode + "\"}");
|
|
|
|
|
try { |
|
|
|
|
ResponseEntity<String> res = restTemplate.postForEntity(URL, requestEntity, String.class); |
|
|
|
|
LOG.info("===send sms end==== : {} ", res); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
|
//API名称
|
|
|
|
|
request.setAction("SendMessageToGlobe"); |
|
|
|
|
//短信内容,必填
|
|
|
|
|
//国际代码
|
|
|
|
|
if (BIG5_CODE.contains(internationalCode)) { |
|
|
|
|
//香港,用繁体
|
|
|
|
|
request.putQueryParameter("Message", BIG5_MSG.replace(MSG_CODE_PLACEHOLDER, smsCode)); |
|
|
|
|
requestEntity.add("SignId", SignId_ZH_HK); |
|
|
|
|
requestEntity.add("TemplateId", TemplateId_ZH_HK); |
|
|
|
|
} else { |
|
|
|
|
//其他,用英文
|
|
|
|
|
request.putQueryParameter("Message", ENGLISH_MSG.replace(MSG_CODE_PLACEHOLDER, smsCode)); |
|
|
|
|
requestEntity.add("SignId", SignId_EN_US); |
|
|
|
|
requestEntity.add("TemplateId", TemplateId_EN_US); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
try { |
|
|
|
|
CommonResponse response = client.getCommonResponse(request); |
|
|
|
|
LOG.info("===send sms end==== : {} ", response.getData()); |
|
|
|
|
ResponseEntity<String> res = restTemplate.postForEntity(URL, requestEntity, String.class); |
|
|
|
|
LOG.info("===send sms end==== : {} ", res); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 请求国内服务器发送短信 |
|
|
|
|
*/ |
|
|
|
|
private void sendTelCode(String mobile, String smsCode, String internationalCode, String templateId) { |
|
|
|
|
String url = "http://47.112.194.145:9220/sendsms"; |
|
|
|
|
MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>(); |
|
|
|
|
multiValueMap.add("mobile", mobile); |
|
|
|
|
multiValueMap.add("smsCode", smsCode); |
|
|
|
|
multiValueMap.add("internationalCode", internationalCode); |
|
|
|
|
multiValueMap.add("templateId", templateId); |
|
|
|
|
try { |
|
|
|
|
String res = restTemplate.postForEntity(url, multiValueMap, String.class).getBody(); |
|
|
|
|
LOG.info("===send sms end==== : {} ", res); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
e.printStackTrace(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |