Bladeren bron

增加core-sms组建,提供统一的短信服务调用接口与配置方法

Signed-off-by: luoshi <luoshi@qmth.com.cn>
luoshi 2 jaren geleden
bovenliggende
commit
82215dfd8b

+ 34 - 0
core-sms/pom.xml

@@ -0,0 +1,34 @@
+<?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>
+        <artifactId>qmth-boot</artifactId>
+        <groupId>com.qmth.boot</groupId>
+        <version>1.0.3</version>
+    </parent>
+    <artifactId>core-sms</artifactId>
+    <name>core-sms</name>
+
+    <dependencies>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-models</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-logging</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>com.qmth.boot</groupId>
+            <artifactId>core-solar</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>junit</groupId>
+            <artifactId>junit</artifactId>
+            <scope>test</scope>
+        </dependency>
+    </dependencies>
+
+</project>

+ 21 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/api/SmsApiClient.java

@@ -0,0 +1,21 @@
+package com.qmth.boot.core.sms.api;
+
+import com.qmth.boot.core.retrofit.annotatioin.RetrofitClient;
+import com.qmth.boot.core.sms.config.SmsApiConfiguration;
+import com.qmth.boot.core.sms.model.SmsConstants;
+import com.qmth.boot.core.sms.model.SmsSendRequest;
+import com.qmth.boot.core.sms.model.SmsSendResponse;
+import retrofit2.http.Body;
+import retrofit2.http.POST;
+
+@RetrofitClient(configuration = SmsApiConfiguration.class)
+public interface SmsApiClient {
+
+    /**
+     * 发送普通短信
+     *
+     * @return
+     */
+    @POST(SmsConstants.API_PATH_SEND_SMS)
+    SmsSendResponse sendSms(@Body SmsSendRequest request);
+}

+ 42 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/config/SmsApiConfiguration.java

@@ -0,0 +1,42 @@
+package com.qmth.boot.core.sms.config;
+
+import com.qmth.boot.core.retrofit.interfaces.CustomizeRetrofitConfiguration;
+import com.qmth.boot.core.retrofit.interfaces.SignatureProvider;
+import com.qmth.boot.core.solar.config.SolarProperties;
+import com.qmth.boot.tools.signature.SignatureType;
+
+public class SmsApiConfiguration implements CustomizeRetrofitConfiguration {
+
+    private SmsProperties smsProperties;
+
+    private SolarProperties solarProperties;
+
+    public SmsApiConfiguration(SmsProperties smsProperties, SolarProperties solarProperties) {
+        this.smsProperties = smsProperties;
+        this.solarProperties = solarProperties;
+    }
+
+    public String getBaseUrl() {
+        return smsProperties.getServer();
+    }
+
+    public SignatureProvider getSignature() {
+        return new SignatureProvider() {
+
+            @Override
+            public SignatureType getType() {
+                return SignatureType.SECRET;
+            }
+
+            @Override
+            public String getIdentity() {
+                return solarProperties.getAccessKey();
+            }
+
+            @Override
+            public String getSecret() {
+                return solarProperties.getAccessSecret();
+            }
+        };
+    }
+}

+ 18 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/config/SmsAutoConfiguration.java

@@ -0,0 +1,18 @@
+package com.qmth.boot.core.sms.config;
+
+import com.qmth.boot.core.retrofit.annotatioin.RetrofitScan;
+import com.qmth.boot.core.solar.config.SolarProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+@ComponentScan("com.qmth.boot.core.sms")
+@RetrofitScan("com.qmth.boot.core.sms.api")
+public class SmsAutoConfiguration {
+
+    @Bean
+    public SmsApiConfiguration smsApiConfiguration(SmsProperties smsProperties, SolarProperties solarProperties) {
+        return new SmsApiConfiguration(smsProperties, solarProperties);
+    }
+}

+ 28 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/config/SmsProperties.java

@@ -0,0 +1,28 @@
+package com.qmth.boot.core.sms.config;
+
+import com.qmth.boot.core.constant.CoreConstant;
+import com.qmth.boot.core.solar.model.SolarConstants;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+import javax.validation.constraints.NotNull;
+
+@Component
+@ConfigurationProperties(prefix = CoreConstant.CONFIG_PREFIX + ".sms")
+public class SmsProperties {
+
+    /**
+     * 云服务模式下,默认直接访问solar地址
+     */
+    @NotNull
+    private String server = SolarConstants.DEFAULT_SERVER;
+
+    public String getServer() {
+        return server;
+    }
+
+    public void setServer(String server) {
+        this.server = server;
+    }
+
+}

+ 8 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/model/SmsConstants.java

@@ -0,0 +1,8 @@
+package com.qmth.boot.core.sms.model;
+
+public class SmsConstants {
+
+    public static final String API_PATH_PREFIX = "/api/sms";
+
+    public static final String API_PATH_SEND_SMS = API_PATH_PREFIX + "/send";
+}

+ 62 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/model/SmsSendRequest.java

@@ -0,0 +1,62 @@
+package com.qmth.boot.core.sms.model;
+
+import org.springframework.validation.annotation.Validated;
+
+import javax.validation.constraints.NotNull;
+import java.util.HashMap;
+import java.util.Map;
+
+@Validated
+public class SmsSendRequest {
+
+    @NotNull(message = "phoneNumber不能为空")
+    private String phoneNumber;
+
+    @NotNull(message = "signName不能为空")
+    private String signName;
+
+    @NotNull(message = "templateCode不能为空")
+    private String templateCode;
+
+    private Map<String, Object> templateParam;
+
+    public SmsSendRequest() {
+        this.templateParam = new HashMap<>();
+    }
+
+    public void addParam(String name, Object value) {
+        this.templateParam.put(name, value);
+    }
+
+    public String getPhoneNumber() {
+        return phoneNumber;
+    }
+
+    public void setPhoneNumber(String phoneNumber) {
+        this.phoneNumber = phoneNumber;
+    }
+
+    public String getSignName() {
+        return signName;
+    }
+
+    public void setSignName(String signName) {
+        this.signName = signName;
+    }
+
+    public String getTemplateCode() {
+        return templateCode;
+    }
+
+    public void setTemplateCode(String templateCode) {
+        this.templateCode = templateCode;
+    }
+
+    public Map<String, Object> getTemplateParam() {
+        return templateParam;
+    }
+
+    public void setTemplateParam(Map<String, Object> templateParam) {
+        this.templateParam = templateParam;
+    }
+}

+ 17 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/model/SmsSendResponse.java

@@ -0,0 +1,17 @@
+package com.qmth.boot.core.sms.model;
+
+public class SmsSendResponse {
+
+    /**
+     * 业务操作唯一标识
+     */
+    private String bizId;
+
+    public String getBizId() {
+        return bizId;
+    }
+
+    public void setBizId(String bizId) {
+        this.bizId = bizId;
+    }
+}

+ 27 - 0
core-sms/src/main/java/com/qmth/boot/core/sms/service/SmsService.java

@@ -0,0 +1,27 @@
+package com.qmth.boot.core.sms.service;
+
+import com.qmth.boot.core.sms.api.SmsApiClient;
+import com.qmth.boot.core.sms.model.SmsSendRequest;
+import com.qmth.boot.core.sms.model.SmsSendResponse;
+import org.springframework.stereotype.Service;
+import org.springframework.validation.annotation.Validated;
+
+@Service
+public class SmsService {
+
+    private SmsApiClient smsApiClient;
+
+    public SmsService(SmsApiClient smsApiClient) {
+        this.smsApiClient = smsApiClient;
+    }
+
+    /**
+     * 发送普通短信
+     *
+     * @param request
+     * @return
+     */
+    public SmsSendResponse sendSms(@Validated SmsSendRequest request) {
+        return smsApiClient.sendSms(request);
+    }
+}

+ 2 - 0
core-sms/src/main/resources/META-INF/spring.factories

@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+  com.qmth.boot.core.sms.config.SmsAutoConfiguration

+ 2 - 1
core-solar/src/main/java/com/qmth/boot/core/solar/config/SolarProperties.java

@@ -1,6 +1,7 @@
 package com.qmth.boot.core.solar.config;
 
 import com.qmth.boot.core.constant.CoreConstant;
+import com.qmth.boot.core.solar.model.SolarConstants;
 import org.springframework.boot.context.properties.ConfigurationProperties;
 import org.springframework.stereotype.Component;
 
@@ -11,7 +12,7 @@ import javax.validation.constraints.NotNull;
 public class SolarProperties {
 
     @NotNull
-    private String server = "https://solar.qmth.com.cn";
+    private String server = SolarConstants.DEFAULT_SERVER;
 
     private String accessKey;
 

+ 6 - 0
core-solar/src/main/java/com/qmth/boot/core/solar/model/SolarConstants.java

@@ -0,0 +1,6 @@
+package com.qmth.boot.core.solar.model;
+
+public interface SolarConstants {
+
+    String DEFAULT_SERVER = "https://solar.qmth.com.cn";
+}

+ 6 - 0
pom.xml

@@ -29,6 +29,7 @@
         <module>core-fss</module>
         <module>core-solar</module>
         <module>core-retrofit</module>
+        <module>core-sms</module>
     </modules>
 
     <name>qmth-boot</name>
@@ -129,6 +130,11 @@
                 <artifactId>core-retrofit</artifactId>
                 <version>${project.version}</version>
             </dependency>
+            <dependency>
+                <groupId>com.qmth.boot</groupId>
+                <artifactId>core-sms</artifactId>
+                <version>${project.version}</version>
+            </dependency>
             <dependency>
                 <groupId>com.qmth.boot</groupId>
                 <artifactId>data-redis</artifactId>