|
|
|
@@ -2,12 +2,23 @@ package com.iformall.sms.aliyun; |
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSON; |
|
|
|
import com.alibaba.fastjson.JSONObject; |
|
|
|
import com.aliyun.auth.credentials.provider.DefaultCredentialProvider; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.AsyncClient; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.CreateSmsSignRequest; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.CreateSmsSignResponse; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.CreateSmsTemplateRequest; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.CreateSmsTemplateResponse; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.GetSmsSignRequest; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.GetSmsSignResponse; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.GetSmsTemplateRequest; |
|
|
|
import com.aliyun.sdk.service.dysmsapi20170525.models.GetSmsTemplateResponse; |
|
|
|
import com.aliyuncs.DefaultAcsClient; |
|
|
|
import com.aliyuncs.IAcsClient; |
|
|
|
import com.aliyuncs.dysmsapi.model.v20170525.*; |
|
|
|
import com.aliyuncs.exceptions.ClientException; |
|
|
|
import com.aliyuncs.profile.DefaultProfile; |
|
|
|
import com.aliyuncs.profile.IClientProfile; |
|
|
|
import com.google.gson.Gson; |
|
|
|
import com.iformall.common.ErrorCode; |
|
|
|
import com.iformall.domain.po.WxMsgConfig; |
|
|
|
import com.iformall.exception.MallinkException; |
|
|
|
@@ -15,6 +26,8 @@ import com.iformall.sms.EnumSMSChannel; |
|
|
|
import com.iformall.sms.SMSExcutor; |
|
|
|
import com.iformall.sms.SMSResult; |
|
|
|
import com.iformall.sms.aliyun.bean.*; |
|
|
|
|
|
|
|
import darabonba.core.client.ClientOverrideConfiguration; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.StringUtils; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
@@ -23,6 +36,8 @@ import java.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.concurrent.CompletableFuture; |
|
|
|
import java.util.concurrent.ExecutionException; |
|
|
|
|
|
|
|
@Component |
|
|
|
@Slf4j |
|
|
|
@@ -57,6 +72,7 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
|
|
|
|
@Override |
|
|
|
public String addTemplate(String secret, String bid, String publickey, String signature, String content, String notifyUrl, String verifysms, String templateName, Integer templateType, String remark) { |
|
|
|
/** |
|
|
|
SMSResult SMSResult = new SMSResult(); |
|
|
|
SMSTemplate smsTemplate = new SMSTemplate(); |
|
|
|
smsTemplate.setTemplateName(templateName); |
|
|
|
@@ -69,10 +85,70 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
SMSResult.setAliyunModelCode(addSmsTemplateResponse.getTemplateCode()); |
|
|
|
} |
|
|
|
return JSON.toJSONString(SMSResult); |
|
|
|
*/ |
|
|
|
SMSResult SMSResult = new SMSResult(); |
|
|
|
DefaultCredentialProvider provider = DefaultCredentialProvider.builder().build(); |
|
|
|
AsyncClient client = AsyncClient.builder() |
|
|
|
.region(smsConfig.getRegionId()) // Region ID |
|
|
|
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient) |
|
|
|
.credentialsProvider(provider) |
|
|
|
//.serviceConfiguration(Configuration.create()) // Service-level configuration |
|
|
|
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc. |
|
|
|
.overrideConfiguration( |
|
|
|
ClientOverrideConfiguration.create() |
|
|
|
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi |
|
|
|
.setEndpointOverride(smsConfig.getDomain()) |
|
|
|
//.setConnectTimeout(Duration.ofSeconds(30)) |
|
|
|
) |
|
|
|
.build(); |
|
|
|
|
|
|
|
CreateSmsTemplateRequest createSmsTemplateRequest = CreateSmsTemplateRequest.builder() |
|
|
|
// Request-level configuration rewrite, can set Http request parameters, etc. |
|
|
|
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())) |
|
|
|
.templateName(templateName) |
|
|
|
.templateContent(content) |
|
|
|
.remark(remark) |
|
|
|
.templateType(templateType) |
|
|
|
.build(); |
|
|
|
|
|
|
|
// Asynchronously get the return value of the API request |
|
|
|
CompletableFuture<CreateSmsTemplateResponse> response = client.createSmsTemplate(createSmsTemplateRequest); |
|
|
|
// Synchronously get the return value of the API request |
|
|
|
CreateSmsTemplateResponse resp = null; |
|
|
|
try { |
|
|
|
resp = response.get(); |
|
|
|
Map<String, Object> retMap = resp.toMap(); |
|
|
|
if(retMap != null){ |
|
|
|
String code = (String) retMap.get("Code"); |
|
|
|
if (StringUtils.isNotBlank(code) && code.equalsIgnoreCase("ok")) { |
|
|
|
SMSResult.setRet("1"); |
|
|
|
SMSResult.setAliyunModelCode((String)retMap.get("TemplateCode")); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
return JSON.toJSONString(SMSResult); |
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
log.error("addTemplate error.",e); |
|
|
|
return JSON.toJSONString(SMSResult); |
|
|
|
}finally { |
|
|
|
client.close(); |
|
|
|
} |
|
|
|
|
|
|
|
// Asynchronous processing of return values |
|
|
|
/*response.thenAccept(resp -> { |
|
|
|
System.out.println(new Gson().toJson(resp)); |
|
|
|
}).exceptionally(throwable -> { // Handling exceptions |
|
|
|
System.out.println(throwable.getMessage()); |
|
|
|
return null; |
|
|
|
});*/ |
|
|
|
|
|
|
|
// Finally, close the client |
|
|
|
//client.close(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String querySmsTemplate(String templateCode) { |
|
|
|
/** |
|
|
|
SMSTemplate smsTemplate = new SMSTemplate(); |
|
|
|
smsTemplate.setTemplateCode(templateCode); |
|
|
|
QuerySmsTemplateResponse querySmsTemplateResponse = this.querySmsTemplate(smsTemplate); |
|
|
|
@@ -84,6 +160,72 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
return obj.toJSONString(); |
|
|
|
} |
|
|
|
return null; |
|
|
|
*/ |
|
|
|
DefaultCredentialProvider provider = DefaultCredentialProvider.builder().build(); |
|
|
|
|
|
|
|
// Configure the Client |
|
|
|
AsyncClient client = AsyncClient.builder() |
|
|
|
.region(smsConfig.getRegionId()) // Region ID |
|
|
|
//.httpClient(httpClient) // Use the configured HttpClient, otherwise use the default HttpClient (Apache HttpClient) |
|
|
|
.credentialsProvider(provider) |
|
|
|
//.serviceConfiguration(Configuration.create()) // Service-level configuration |
|
|
|
// Client-level configuration rewrite, can set Endpoint, Http request parameters, etc. |
|
|
|
.overrideConfiguration( |
|
|
|
ClientOverrideConfiguration.create() |
|
|
|
// Endpoint 请参考 https://api.aliyun.com/product/Dysmsapi |
|
|
|
.setEndpointOverride(smsConfig.getDomain()) |
|
|
|
//.setConnectTimeout(Duration.ofSeconds(30)) |
|
|
|
) |
|
|
|
.build(); |
|
|
|
|
|
|
|
GetSmsTemplateRequest getSmsTemplateRequest = GetSmsTemplateRequest.builder() |
|
|
|
// Request-level configuration rewrite, can set Http request parameters, etc. |
|
|
|
// .requestConfiguration(RequestConfiguration.create().setHttpHeaders(new HttpHeaders())) |
|
|
|
.build(); |
|
|
|
|
|
|
|
// Asynchronously get the return value of the API request |
|
|
|
CompletableFuture<GetSmsTemplateResponse> response = client.getSmsTemplate(getSmsTemplateRequest); |
|
|
|
// Synchronously get the return value of the API request |
|
|
|
GetSmsTemplateResponse resp = null; |
|
|
|
try { |
|
|
|
resp = response.get(); |
|
|
|
Map<String, Object> retMap = resp.toMap(); |
|
|
|
if(retMap != null){ |
|
|
|
String code = (String) retMap.get("Code"); |
|
|
|
if (StringUtils.isNotBlank(code) && code.equalsIgnoreCase("ok")) { |
|
|
|
JSONObject obj = new JSONObject(); |
|
|
|
obj.put("templateCode",retMap.get("TemplateCode")); |
|
|
|
String templateStauts = (String) retMap.get("TemplateStatus"); |
|
|
|
obj.put("templateStatus",templateStauts);//0:审核中。1:审核通过。2:审核失败 |
|
|
|
String reason = ""; |
|
|
|
if (StringUtils.isNotBlank(templateStauts) && templateStauts.equals("2")) { |
|
|
|
Object ajo = retMap.get("AuditInfo"); |
|
|
|
if (null != ajo) { |
|
|
|
JSONObject ajoo = JSON.parseObject(JSON.toJSONString(ajo)); |
|
|
|
reason = ajoo.getString("RejectInfo"); |
|
|
|
} |
|
|
|
} |
|
|
|
obj.put("reason",reason); |
|
|
|
return obj.toJSONString(); |
|
|
|
} |
|
|
|
} |
|
|
|
return null; |
|
|
|
} catch (InterruptedException | ExecutionException e) { |
|
|
|
log.error("querySmsTemplate error.",e); |
|
|
|
return null; |
|
|
|
}finally { |
|
|
|
client.close(); |
|
|
|
} |
|
|
|
// Asynchronous processing of return values |
|
|
|
/*response.thenAccept(resp -> { |
|
|
|
System.out.println(new Gson().toJson(resp)); |
|
|
|
}).exceptionally(throwable -> { // Handling exceptions |
|
|
|
System.out.println(throwable.getMessage()); |
|
|
|
return null; |
|
|
|
});*/ |
|
|
|
|
|
|
|
// Finally, close the client |
|
|
|
//client.close(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@@ -167,9 +309,12 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
|
|
|
|
/** |
|
|
|
* 添加签名 |
|
|
|
* 新版本需要对接 |
|
|
|
* https://api.aliyun.com/document/Dysmsapi/2017-05-25/CreateSmsSign?spm=5176.25163407.overview-index-9c3d4_4cfbe_0.12.51a72ec8oDkBre |
|
|
|
* @param smsSign |
|
|
|
*/ |
|
|
|
public AddSmsSignResponse addSmsSign(SMSSign smsSign){ |
|
|
|
/** |
|
|
|
IAcsClient acsClient = getClient(); |
|
|
|
AddSmsSignRequest request = new AddSmsSignRequest(); |
|
|
|
request.setSignName(smsSign.getSignName()); |
|
|
|
@@ -184,6 +329,8 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
} |
|
|
|
log.info("添加签名-----" + response.getCode() + "----" + response.getMessage()); |
|
|
|
return response; |
|
|
|
*/ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -191,6 +338,7 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
* @param smsSign |
|
|
|
*/ |
|
|
|
public DeleteSmsSignResponse deleteSmsSign(SMSSign smsSign){ |
|
|
|
/** |
|
|
|
IAcsClient acsClient = getClient(); |
|
|
|
DeleteSmsSignRequest request = new DeleteSmsSignRequest(); |
|
|
|
request.setSignName(smsSign.getSignName()); |
|
|
|
@@ -203,6 +351,8 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
} |
|
|
|
log.info("删除签名-----" + response.getCode() + "----" + response.getMessage()); |
|
|
|
return response; |
|
|
|
*/ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -210,6 +360,7 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
* @param smsSign |
|
|
|
*/ |
|
|
|
public ModifySmsSignResponse modifySmsSign(SMSSign smsSign){ |
|
|
|
/** |
|
|
|
IAcsClient acsClient = getClient(); |
|
|
|
ModifySmsSignRequest request = new ModifySmsSignRequest(); |
|
|
|
request.setSignName(smsSign.getSignName()); |
|
|
|
@@ -224,6 +375,8 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
} |
|
|
|
log.info("修改签名----" + response.getCode() + "----" + response.getMessage()); |
|
|
|
return response; |
|
|
|
*/ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -231,6 +384,7 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
* @param smsSign |
|
|
|
*/ |
|
|
|
public QuerySmsSignResponse querySmsSign(SMSSign smsSign){ |
|
|
|
/** |
|
|
|
IAcsClient acsClient = getClient(); |
|
|
|
QuerySmsSignRequest request = new QuerySmsSignRequest(); |
|
|
|
request.setSignName(smsSign.getSignName()); |
|
|
|
@@ -243,6 +397,8 @@ public class AliyunSMS implements SMSExcutor { |
|
|
|
} |
|
|
|
log.info("查询签名----" + response.getCode() + "----" + response.getMessage()); |
|
|
|
return response; |
|
|
|
*/ |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
|