feat:模块名称调整
This commit is contained in:
29
springboot-message/pom.xml
Normal file
29
springboot-message/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>springboot-parent</artifactId>
|
||||
<version>0.0.18</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>springboot-message</artifactId>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>8</maven.compiler.source>
|
||||
<maven.compiler.target>8</maven.compiler.target>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.tiesheng</groupId>
|
||||
<artifactId>springboot-platform</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.tiesheng.message;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
@ComponentScan({
|
||||
"com.tiesheng.message.**.*",
|
||||
})
|
||||
public class MessageAutoConfigurer {
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
package com.tiesheng.message.config.aliyun;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import cn.hutool.crypto.SecureUtil;
|
||||
import cn.hutool.http.HttpUtil;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
import com.tiesheng.message.service.TieshengMessageConfigurer;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
*/
|
||||
@Configuration
|
||||
@ConfigurationProperties(prefix = "tiesheng.aliyun")
|
||||
public class AliyunSmsConfig {
|
||||
|
||||
/**
|
||||
* 请求地址
|
||||
*/
|
||||
private static final String ENDPOINT = "https://dysmsapi.aliyuncs.com";
|
||||
|
||||
@Autowired
|
||||
TieshengMessageConfigurer tieshengMessageConfigurer;
|
||||
|
||||
private String accessKeyId;
|
||||
private String accessKeySecret;
|
||||
private String signName;
|
||||
|
||||
/**
|
||||
* 特殊字符替换
|
||||
*
|
||||
* @param value
|
||||
* @return
|
||||
*/
|
||||
private static String specialUrlEncode(String value) {
|
||||
try {
|
||||
return URLEncoder.encode(value, "UTF-8").replace("+", "%20")
|
||||
.replace("*", "%2A").replace("%7E", "~");
|
||||
} catch (Exception ignore) {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 创建发送请求
|
||||
*
|
||||
* @param phoneNumbers 手机号,多个用,隔开
|
||||
* @param templateCode 短信模板
|
||||
* @param params 模板参数
|
||||
* @return
|
||||
*/
|
||||
public MessageReqResp sendSms(String phoneNumbers, String templateCode, JSONObject params) {
|
||||
|
||||
MessageReqResp reqResp = new MessageReqResp();
|
||||
reqResp.setTarget(phoneNumbers);
|
||||
reqResp.setResult(1);
|
||||
reqResp.setContent(JSONUtil.createObj()
|
||||
.putOpt("PhoneNumbers", phoneNumbers)
|
||||
.putOpt("SignName", getSignName())
|
||||
.putOpt("TemplateCode", templateCode)
|
||||
.putOpt("TemplateParam", params)
|
||||
.toString());
|
||||
|
||||
SortedMap<String, String> queryMap = new TreeMap<>();
|
||||
|
||||
// 系统参数
|
||||
queryMap.put("AccessKeyId", getAccessKeyId());
|
||||
queryMap.put("SignatureMethod", "HMAC-SHA1");
|
||||
queryMap.put("SignatureNonce", IdUtil.randomUUID());
|
||||
queryMap.put("SignatureVersion", "1.0");
|
||||
SimpleDateFormat dateFormat = DateUtil.newSimpleFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.CHINESE, TimeZone.getTimeZone("0"));
|
||||
queryMap.put("Timestamp", DateUtil.format(DateUtil.date(), dateFormat));
|
||||
queryMap.put("Format", "json");
|
||||
|
||||
// 业务API参数
|
||||
queryMap.put("Action", "SendSms");
|
||||
queryMap.put("Version", "2017-05-25");
|
||||
queryMap.put("RegionId", "cn-hangzhou");
|
||||
queryMap.put("PhoneNumbers", phoneNumbers);
|
||||
queryMap.put("SignName", getSignName());
|
||||
queryMap.put("TemplateCode", templateCode);
|
||||
if (params != null) {
|
||||
queryMap.put("TemplateParam", params.toString());
|
||||
}
|
||||
|
||||
// 构造带签名字符串
|
||||
StringBuilder sortQueryStringTmp = new StringBuilder();
|
||||
for (String key : queryMap.keySet()) {
|
||||
sortQueryStringTmp.append("&").append(specialUrlEncode(key)).append("=").append(specialUrlEncode(queryMap.get(key)));
|
||||
}
|
||||
String sortedQueryString = sortQueryStringTmp.substring(1);
|
||||
String stringToSign = "GET" + "&" + specialUrlEncode("/") + "&" + specialUrlEncode(sortedQueryString);
|
||||
String digest = SecureUtil.hmacSha1((getAccessKeySecret() + "&").getBytes(StandardCharsets.UTF_8))
|
||||
.digestBase64(stringToSign, false);
|
||||
String signature = specialUrlEncode(digest);
|
||||
queryMap.put("Signature", signature);
|
||||
|
||||
// 发送请求
|
||||
String response = HttpUtil.get(ENDPOINT + "?Signature=" + signature + sortQueryStringTmp, 10 * 1000);
|
||||
reqResp.setRespBody(response);
|
||||
JSONObject respObj = JSONUtil.parseObj(response);
|
||||
if (!Objects.equals(respObj.getStr("Code"), "OK")) {
|
||||
reqResp.setResult(0);
|
||||
reqResp.setToast(respObj.getStr("Message"));
|
||||
}
|
||||
|
||||
tieshengMessageConfigurer.onMessageSend("阿里云短信", reqResp);
|
||||
|
||||
return reqResp;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getAccessKeyId() {
|
||||
return accessKeyId;
|
||||
}
|
||||
|
||||
public void setAccessKeyId(String accessKeyId) {
|
||||
this.accessKeyId = accessKeyId;
|
||||
}
|
||||
|
||||
public String getAccessKeySecret() {
|
||||
return accessKeySecret;
|
||||
}
|
||||
|
||||
public void setAccessKeySecret(String accessKeySecret) {
|
||||
this.accessKeySecret = accessKeySecret;
|
||||
}
|
||||
|
||||
public String getSignName() {
|
||||
return signName;
|
||||
}
|
||||
|
||||
public void setSignName(String signName) {
|
||||
this.signName = signName;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
package com.tiesheng.message.pojos;
|
||||
|
||||
public class MessageReqResp {
|
||||
|
||||
/**
|
||||
* 发送对象
|
||||
*/
|
||||
private String target;
|
||||
|
||||
/**
|
||||
* 发送内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 返回结果
|
||||
*/
|
||||
private String respBody;
|
||||
|
||||
/**
|
||||
* 结果,0-否,1-是
|
||||
*/
|
||||
private Integer result;
|
||||
|
||||
|
||||
/**
|
||||
* 提示的异常信息
|
||||
*/
|
||||
private String toast;
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// setter\getter
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
|
||||
public String getTarget() {
|
||||
return target;
|
||||
}
|
||||
|
||||
public void setTarget(String target) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public String getContent() {
|
||||
return content;
|
||||
}
|
||||
|
||||
public void setContent(String content) {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public String getRespBody() {
|
||||
return respBody;
|
||||
}
|
||||
|
||||
public void setRespBody(String respBody) {
|
||||
this.respBody = respBody;
|
||||
}
|
||||
|
||||
public Integer getResult() {
|
||||
return result;
|
||||
}
|
||||
|
||||
public void setResult(Integer result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getToast() {
|
||||
return toast;
|
||||
}
|
||||
|
||||
public void setToast(String toast) {
|
||||
this.toast = toast;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.tiesheng.message.service;
|
||||
|
||||
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import cn.hutool.log.LogFactory;
|
||||
import com.tiesheng.message.pojos.MessageReqResp;
|
||||
|
||||
/**
|
||||
* @author hao
|
||||
*/
|
||||
public interface TieshengMessageConfigurer {
|
||||
|
||||
|
||||
/**
|
||||
* 消息发送后
|
||||
*
|
||||
* @param reqResp
|
||||
*/
|
||||
default void onMessageSend(String type, MessageReqResp reqResp) {
|
||||
LogFactory.get().info(JSONUtil.toJsonStr(reqResp));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.tiesheng.message.service.impl;
|
||||
|
||||
import com.tiesheng.message.service.TieshengMessageConfigurer;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnMissingBean(value = TieshengMessageConfigurer.class, ignored = DefaultMessageConfigurer.class)
|
||||
public class DefaultMessageConfigurer implements TieshengMessageConfigurer {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user