sms-ali
parent
32dc3e8284
commit
1b872a039d
7
pom.xml
7
pom.xml
|
|
@ -43,6 +43,7 @@
|
|||
<aliyun-sdk-oss.version>3.17.4</aliyun-sdk-oss.version>
|
||||
<alipay-sdk.version>4.40.54.ALL</alipay-sdk.version>
|
||||
<aliyun-imagesearch.version>4.2.2</aliyun-imagesearch.version>
|
||||
<alibabacloud-dysmsapi.version>4.0.1</alibabacloud-dysmsapi.version>
|
||||
</properties>
|
||||
|
||||
<!-- 依赖声明 -->
|
||||
|
|
@ -266,6 +267,12 @@
|
|||
<version>${aliyun-imagesearch.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
|
||||
<version>${alibabacloud-dysmsapi.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- ES相关 begin-->
|
||||
<dependency>
|
||||
<groupId>co.elastic.clients</groupId>
|
||||
|
|
|
|||
|
|
@ -50,6 +50,11 @@
|
|||
<artifactId>imagesearch20201214</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.aliyun</groupId>
|
||||
<artifactId>alibabacloud-dysmsapi20170525</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mybatis-plus -->
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,34 @@
|
|||
package com.ruoyi.framework.sms;
|
||||
|
||||
import com.ruoyi.framework.sms.ali.AliSmsServer;
|
||||
import com.ruoyi.framework.sms.ali.entity.AliSmsResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SmsClientWrapper {
|
||||
|
||||
@Autowired
|
||||
private AliSmsServer aliSmsServer;
|
||||
|
||||
@Value("${sms.send:true}")
|
||||
private Boolean doSend;
|
||||
|
||||
public boolean sendSms(String signName, String phoneNumber, String templateCode, String templateParams) {
|
||||
boolean sendResult;
|
||||
if (doSend) {
|
||||
AliSmsResponse response = aliSmsServer.sendSms(signName, phoneNumber, templateCode, templateParams);
|
||||
sendResult = response.success();
|
||||
}
|
||||
// 测试的时候不发短信
|
||||
else {
|
||||
sendResult = true;
|
||||
}
|
||||
log.info("发送短信{},{},{},{}:{},doSend:{}", signName, phoneNumber, templateCode, templateParams, sendResult, doSend);
|
||||
return sendResult;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.ruoyi.framework.sms;
|
||||
|
||||
import com.ruoyi.framework.sms.ali.AliSmsServer;
|
||||
import lombok.Getter;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Getter
|
||||
@Configuration
|
||||
public class SmsConfiguration {
|
||||
|
||||
@Value("${sms.accessKeyId:}")
|
||||
private String accessKeyId;
|
||||
|
||||
@Value("${sms.accessKeySecret:}")
|
||||
private String accessKeySecret;
|
||||
|
||||
@Value("${sms.regionId:}")
|
||||
private String regionId;
|
||||
|
||||
@Bean
|
||||
public AliSmsServer aliSmsServer() {
|
||||
return new AliSmsServer(accessKeyId, accessKeySecret, regionId);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,116 @@
|
|||
package com.ruoyi.framework.sms.ali;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
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 com.aliyuncs.profile.IClientProfile;
|
||||
import com.ruoyi.framework.sms.ali.entity.AliSmsResponse;
|
||||
import com.ruoyi.framework.sms.ali.entity.Result;
|
||||
import com.ruoyi.framework.sms.ali.entity.TemplateInfo;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 阿里短信服务
|
||||
* 官方文档地址 https://help.aliyun.com/document_detail/121206.html
|
||||
*
|
||||
* @author liangyq
|
||||
*/
|
||||
@Slf4j
|
||||
public class AliSmsServer {
|
||||
|
||||
private IAcsClient client;
|
||||
|
||||
private String accessKeyId;
|
||||
|
||||
public AliSmsServer(String accessKeyId, String accessKeySecret, String regionId) {
|
||||
IClientProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, accessKeySecret);
|
||||
client = new DefaultAcsClient(profile);
|
||||
this.accessKeyId = accessKeyId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @param signName 短信签名
|
||||
* @param phoneNumber 手机号
|
||||
* @param templateCode 模版编号
|
||||
* @param templateParams 模版参数(json)
|
||||
*/
|
||||
public AliSmsResponse sendSms(String signName, String phoneNumber, String templateCode, String templateParams) {
|
||||
try {
|
||||
CommonRequest request = newCommonRequest("SendSms");
|
||||
request.putQueryParameter("PhoneNumbers", phoneNumber);
|
||||
request.putQueryParameter("SignName", signName);
|
||||
request.putQueryParameter("TemplateCode", templateCode);
|
||||
request.putQueryParameter("TemplateParam", templateParams);
|
||||
String params = JSON.toJSONString(request);
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
return new AliSmsResponse(params, response);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量发短信 最多100个手机号
|
||||
*
|
||||
* @param signNameList 短信签名
|
||||
* @param phoneNumberList 手机号
|
||||
* @param templateCode 模版编号
|
||||
* @param templateParamList 模版参数 json
|
||||
*/
|
||||
public Result sendBatchSms(List<String> signNameList, List<String> phoneNumberList, String templateCode, List<String> templateParamList) {
|
||||
try {
|
||||
CommonRequest request = newCommonRequest("SendBatchSms");
|
||||
request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(phoneNumberList));
|
||||
request.putQueryParameter("SignNameJson", JSON.toJSONString(signNameList));
|
||||
request.putQueryParameter("TemplateCode", templateCode);
|
||||
request.putQueryParameter("TemplateParamJson", JSON.toJSONString(templateParamList));
|
||||
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
return handlerResponse(response);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public TemplateInfo querySmsTemplate(String templateCode) {
|
||||
CommonRequest request = newCommonRequest("QuerySmsTemplate");
|
||||
request.putQueryParameter("TemplateCode", templateCode);
|
||||
request.putQueryParameter("AccessKeyId", this.accessKeyId);
|
||||
try {
|
||||
CommonResponse response = client.getCommonResponse(request);
|
||||
if (response.getHttpResponse().isSuccess()) {
|
||||
return JSON.parseObject(response.getData(), TemplateInfo.class);
|
||||
}
|
||||
return null;
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private CommonRequest newCommonRequest(String action) {
|
||||
CommonRequest request = new CommonRequest();
|
||||
request.setSysMethod(MethodType.POST);
|
||||
request.setSysDomain("dysmsapi.aliyuncs.com");
|
||||
request.setSysVersion("2017-05-25");
|
||||
request.setSysAction(action);
|
||||
return request;
|
||||
}
|
||||
|
||||
private Result handlerResponse(CommonResponse response) {
|
||||
if (!response.getHttpResponse().isSuccess()) {
|
||||
return new Result("异常");
|
||||
}
|
||||
log.info(response.getData());
|
||||
return JSON.parseObject(response.getData(), Result.class);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
package com.ruoyi.framework.sms.ali.entity;
|
||||
|
||||
import com.alibaba.fastjson2.JSON;
|
||||
import com.aliyuncs.CommonResponse;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
*/
|
||||
@Slf4j
|
||||
@Data
|
||||
public class AliSmsResponse extends Response {
|
||||
|
||||
private CommonResponse response;
|
||||
|
||||
public AliSmsResponse(String params, CommonResponse response) {
|
||||
super("ALI-SMS", "ALI", params, JSON.toJSONString(response));
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean success() {
|
||||
return response.getHttpResponse().isSuccess();
|
||||
}
|
||||
|
||||
public Result get() {
|
||||
if (!response.getHttpResponse().isSuccess()) {
|
||||
return new Result("异常");
|
||||
}
|
||||
log.info(response.getData());
|
||||
return JSON.parseObject(response.getData(), Result.class);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,74 @@
|
|||
package com.ruoyi.framework.sms.ali.entity;
|
||||
|
||||
import cn.hutool.core.lang.Assert;
|
||||
import lombok.Getter;
|
||||
|
||||
/**
|
||||
* @author liangyq
|
||||
*/
|
||||
@Getter
|
||||
public abstract class Response {
|
||||
/**
|
||||
* api名称
|
||||
*/
|
||||
private final String apiName;
|
||||
/**
|
||||
* 平台信息
|
||||
*/
|
||||
private final String platform;
|
||||
/**
|
||||
* 参数
|
||||
*/
|
||||
private final String params;
|
||||
/**
|
||||
* 返回信息
|
||||
*/
|
||||
private final String responseText;
|
||||
/**
|
||||
* 结果记录数
|
||||
*/
|
||||
private int count;
|
||||
|
||||
public Response(String apiName, String platform, String params, String responseText) {
|
||||
Assert.notEmpty(apiName);
|
||||
Assert.notNull(platform);
|
||||
this.apiName = apiName;
|
||||
this.platform = platform;
|
||||
this.responseText = responseText;
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否成功
|
||||
*/
|
||||
public abstract boolean success();
|
||||
|
||||
/**
|
||||
* @return 是否调用的是本地历史结果(默认否)
|
||||
*/
|
||||
public boolean isLocalCall() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 增加结果记录数
|
||||
*/
|
||||
public void increaseCount() {
|
||||
this.count++;
|
||||
}
|
||||
|
||||
public void setCount(int count) {
|
||||
this.count = count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Response{" +
|
||||
"apiName='" + apiName + '\'' +
|
||||
", platform='" + platform + '\'' +
|
||||
", params='" + params + '\'' +
|
||||
", responseText='" + responseText + '\'' +
|
||||
", count=" + count +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,23 @@
|
|||
package com.ruoyi.framework.sms.ali.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class Result {
|
||||
|
||||
private String code;
|
||||
|
||||
private String message;
|
||||
|
||||
public Result() {
|
||||
|
||||
}
|
||||
|
||||
public Result(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public boolean isOk() {
|
||||
return "OK".equals(code);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
package com.ruoyi.framework.sms.ali.entity;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 短信模板信息
|
||||
*
|
||||
* @author liangyq
|
||||
*/
|
||||
@Data
|
||||
public class TemplateInfo {
|
||||
private String templateCode;
|
||||
private String templateContent;
|
||||
private String templateName;
|
||||
private Integer templateType;
|
||||
private Date createDate;
|
||||
private String reason;
|
||||
/**
|
||||
* 审核状态[0:审核中,1:通过,2:审核失败]
|
||||
*/
|
||||
private Integer templateStatus;
|
||||
}
|
||||
Loading…
Reference in New Issue