mirror of
https://gitee.com/dromara/sms4j.git
synced 2025-12-08 01:48:38 +08:00
添加UniSms合一短信的基础支持,添加发送群体延迟短信
This commit is contained in:
parent
f687ba5870
commit
084b1eda93
181
pom.xml
181
pom.xml
@ -5,7 +5,7 @@
|
|||||||
<groupId>kim.wind</groupId>
|
<groupId>kim.wind</groupId>
|
||||||
<artifactId>sms_aggregation</artifactId>
|
<artifactId>sms_aggregation</artifactId>
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
<name>sms_aggregation</name>
|
<name>sms-aggregation</name>
|
||||||
<packaging>pom</packaging>
|
<packaging>pom</packaging>
|
||||||
<description>sms_aggregation</description>
|
<description>sms_aggregation</description>
|
||||||
<url>https://gitee.com/the-wind-is-like-a-song/sms_aggregation</url>
|
<url>https://gitee.com/the-wind-is-like-a-song/sms_aggregation</url>
|
||||||
@ -52,6 +52,7 @@
|
|||||||
<aliyun.version>2.0.23</aliyun.version>
|
<aliyun.version>2.0.23</aliyun.version>
|
||||||
<json.version>2.0.15</json.version>
|
<json.version>2.0.15</json.version>
|
||||||
<okhttp.version>3.14.9</okhttp.version>
|
<okhttp.version>3.14.9</okhttp.version>
|
||||||
|
<unisms.version>0.0.4</unisms.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -101,12 +102,14 @@
|
|||||||
<version>${modules.version}</version>
|
<version>${modules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--通用模块-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>kim.wind</groupId>
|
<groupId>kim.wind</groupId>
|
||||||
<artifactId>sms-aggregation-comm</artifactId>
|
<artifactId>sms-aggregation-comm</artifactId>
|
||||||
<version>${modules.version}</version>
|
<version>${modules.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--api模块-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>kim.wind</groupId>
|
<groupId>kim.wind</groupId>
|
||||||
<artifactId>sms-aggregation-api</artifactId>
|
<artifactId>sms-aggregation-api</artifactId>
|
||||||
@ -155,6 +158,13 @@
|
|||||||
<scope>provided</scope>
|
<scope>provided</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--合一短信依赖-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.apistd.uni</groupId>
|
||||||
|
<artifactId>uni-sdk</artifactId>
|
||||||
|
<version>${unisms.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</dependencyManagement>
|
</dependencyManagement>
|
||||||
|
|
||||||
@ -165,6 +175,7 @@
|
|||||||
<optional>true</optional>
|
<optional>true</optional>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<!--配置文件提示-->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-configuration-processor</artifactId>
|
<artifactId>spring-boot-configuration-processor</artifactId>
|
||||||
@ -186,90 +197,90 @@
|
|||||||
|
|
||||||
|
|
||||||
<build>
|
<build>
|
||||||
<plugins>
|
<plugins>
|
||||||
<!-- Source -->
|
<!-- Source -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-source-plugin</artifactId>
|
<artifactId>maven-source-plugin</artifactId>
|
||||||
<version>2.2.1</version>
|
<version>2.2.1</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>jar-no-fork</goal>
|
<goal>jar-no-fork</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- Javadoc -->
|
<!-- Javadoc -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-javadoc-plugin</artifactId>
|
<artifactId>maven-javadoc-plugin</artifactId>
|
||||||
<version>2.9.1</version>
|
<version>2.9.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<show>private</show>
|
<show>private</show>
|
||||||
<nohelp>true</nohelp>
|
<nohelp>true</nohelp>
|
||||||
<charset>UTF-8</charset>
|
<charset>UTF-8</charset>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
<docencoding>UTF-8</docencoding>
|
<docencoding>UTF-8</docencoding>
|
||||||
<additionalparam>-Xdoclint:none</additionalparam>
|
<additionalparam>-Xdoclint:none</additionalparam>
|
||||||
<!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
|
<!-- TODO 临时解决不规范的javadoc生成报错,后面要规范化后把这行去掉 -->
|
||||||
</configuration>
|
</configuration>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>package</phase>
|
<phase>package</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>jar</goal>
|
<goal>jar</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!-- GPG -->
|
<!-- GPG -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-gpg-plugin</artifactId>
|
<artifactId>maven-gpg-plugin</artifactId>
|
||||||
<version>1.6</version>
|
<version>1.6</version>
|
||||||
<executions>
|
<executions>
|
||||||
<execution>
|
<execution>
|
||||||
<phase>verify</phase>
|
<phase>verify</phase>
|
||||||
<goals>
|
<goals>
|
||||||
<goal>sign</goal>
|
<goal>sign</goal>
|
||||||
</goals>
|
</goals>
|
||||||
</execution>
|
</execution>
|
||||||
</executions>
|
</executions>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!--Compiler -->
|
<!--Compiler -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.0</version>
|
<version>3.0</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>1.8</source>
|
||||||
<target>1.8</target>
|
<target>1.8</target>
|
||||||
<fork>true</fork>
|
<fork>true</fork>
|
||||||
<verbose>true</verbose>
|
<verbose>true</verbose>
|
||||||
<encoding>UTF-8</encoding>
|
<encoding>UTF-8</encoding>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
<!--Release -->
|
<!--Release -->
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-release-plugin</artifactId>
|
<artifactId>maven-release-plugin</artifactId>
|
||||||
<version>2.5.1</version>
|
<version>2.5.1</version>
|
||||||
</plugin>
|
</plugin>
|
||||||
<plugin>
|
<plugin>
|
||||||
<groupId>org.apache.maven.plugins</groupId>
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
<artifactId>maven-jar-plugin</artifactId>
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
<version>3.2.2</version>
|
<version>3.2.2</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<archive>
|
<archive>
|
||||||
<manifest>
|
<manifest>
|
||||||
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
|
||||||
</manifest>
|
</manifest>
|
||||||
</archive>
|
</archive>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
|
|
||||||
</plugins>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@ -26,6 +26,13 @@ import java.util.List;
|
|||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>类名: AlibabaSmsImpl
|
||||||
|
* <p>说明: 阿里云短信实现
|
||||||
|
*
|
||||||
|
* @author :Wind
|
||||||
|
* 2023/3/26 17:16
|
||||||
|
**/
|
||||||
@EnableConfigurationProperties({AlibabaSmsConfig.class})
|
@EnableConfigurationProperties({AlibabaSmsConfig.class})
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public class AlibabaSmsImpl implements SmsBlend {
|
public class AlibabaSmsImpl implements SmsBlend {
|
||||||
@ -132,6 +139,13 @@ public class AlibabaSmsImpl implements SmsBlend {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessageAsync(String phone, String message) {
|
||||||
|
pool.execute(() -> {
|
||||||
|
sendMessage(phone, message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Restricted
|
@Restricted
|
||||||
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||||
@ -141,6 +155,13 @@ public class AlibabaSmsImpl implements SmsBlend {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||||
|
pool.execute(()->{
|
||||||
|
sendMessage(phone,templateId,messages);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Restricted
|
@Restricted
|
||||||
public void delayedMessage(String phone, String message, Long delayedTime) {
|
public void delayedMessage(String phone, String message, Long delayedTime) {
|
||||||
@ -162,4 +183,24 @@ public class AlibabaSmsImpl implements SmsBlend {
|
|||||||
}
|
}
|
||||||
},delayedTime);
|
},delayedTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delayMassTexting(List<String> phones, String message, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
massTexting(phones,message);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delayMassTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
massTexting(phones,templateId,messages);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -58,6 +58,15 @@ public interface SmsBlend {
|
|||||||
|
|
||||||
void sendMessageAsync(String phone, String message, CallBack callBack);
|
void sendMessageAsync(String phone, String message, CallBack callBack);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>说明:异步发送短信,不关注发送结果
|
||||||
|
* sendMessageAsync
|
||||||
|
* @param phone 要发送的号码
|
||||||
|
* @param message 发送内容
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendMessageAsync(String phone, String message);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>说明:异步短信发送,使用自定义模板发送短信
|
* <p>说明:异步短信发送,使用自定义模板发送短信
|
||||||
* sendMessage
|
* sendMessage
|
||||||
@ -70,7 +79,16 @@ public interface SmsBlend {
|
|||||||
void sendMessageAsync(String phone, String templateId, LinkedHashMap<String,String> messages, CallBack callBack);
|
void sendMessageAsync(String phone, String templateId, LinkedHashMap<String,String> messages, CallBack callBack);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>说明:
|
* <p>说明:异步短信发送,使用自定义模板发送短信,不关注发送结果
|
||||||
|
* sendMessageAsync
|
||||||
|
* @param templateId 模板id
|
||||||
|
* @param messages key为模板变量名称 value为模板变量值
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void sendMessageAsync(String phone, String templateId, LinkedHashMap<String,String> messages);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>说明:使用固定模板发送延时短信
|
||||||
* delayedMessage
|
* delayedMessage
|
||||||
* @param phone 接收短信的手机号
|
* @param phone 接收短信的手机号
|
||||||
* @param message 要发送的短信
|
* @param message 要发送的短信
|
||||||
@ -89,4 +107,23 @@ public interface SmsBlend {
|
|||||||
* @author :Wind
|
* @author :Wind
|
||||||
*/
|
*/
|
||||||
void delayedMessage(String phone ,String templateId, LinkedHashMap<String,String> messages,Long delayedTime);
|
void delayedMessage(String phone ,String templateId, LinkedHashMap<String,String> messages,Long delayedTime);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>说明:群发延迟短信
|
||||||
|
* delayMassTexting
|
||||||
|
* @param phones 要群体发送的手机号码
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void delayMassTexting(List<String> phones, String message,Long delayedTime);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>说明:使用自定义模板发送群体延迟短信
|
||||||
|
* delayMassTexting
|
||||||
|
* @param phones 要群体发送的手机号码
|
||||||
|
* @param templateId 模板id
|
||||||
|
* @param messages key为模板变量名称 value为模板变量值
|
||||||
|
* @param delayedTime 延迟的时间
|
||||||
|
* @author :Wind
|
||||||
|
*/
|
||||||
|
void delayMassTexting(List<String> phones,String templateId, LinkedHashMap<String, String> messages,Long delayedTime);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,6 +6,13 @@ import java.lang.annotation.Retention;
|
|||||||
import java.lang.annotation.RetentionPolicy;
|
import java.lang.annotation.RetentionPolicy;
|
||||||
import java.lang.annotation.Target;
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>类名: Restricted
|
||||||
|
* <p>说明: 发送短信限制
|
||||||
|
*
|
||||||
|
* @author :Wind
|
||||||
|
* 2023/3/26 17:12
|
||||||
|
**/
|
||||||
@Target(ElementType.METHOD)
|
@Target(ElementType.METHOD)
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
public @interface Restricted {
|
public @interface Restricted {
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
package kim.wind.sms.starter.config;
|
package kim.wind.sms.starter.config;
|
||||||
|
|
||||||
|
import com.example.sms.unisms.service.UniSmsImpl;
|
||||||
import kim.wind.sms.aliyun.service.AlibabaSmsImpl;
|
import kim.wind.sms.aliyun.service.AlibabaSmsImpl;
|
||||||
import kim.wind.sms.api.SmsBlend;
|
import kim.wind.sms.api.SmsBlend;
|
||||||
import kim.wind.sms.comm.delayedTime.DelayedTime;
|
import kim.wind.sms.comm.delayedTime.DelayedTime;
|
||||||
@ -69,6 +70,10 @@ public class SmsMainConfig {
|
|||||||
switch (supplier){
|
switch (supplier){
|
||||||
case "alibaba":
|
case "alibaba":
|
||||||
smsBlend = new AlibabaSmsImpl();
|
smsBlend = new AlibabaSmsImpl();
|
||||||
|
break;
|
||||||
|
case "uniSms":
|
||||||
|
smsBlend = new UniSmsImpl();
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
return smsBlend;
|
return smsBlend;
|
||||||
}
|
}
|
||||||
@ -79,8 +84,8 @@ public class SmsMainConfig {
|
|||||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
|
||||||
executor.setCorePoolSize(corePoolSize);
|
executor.setCorePoolSize(corePoolSize);
|
||||||
executor.setMaxPoolSize(maxPoolSize);
|
executor.setMaxPoolSize(maxPoolSize);
|
||||||
executor.setQueueCapacity(100);
|
executor.setQueueCapacity(queueCapacity);
|
||||||
executor.setKeepAliveSeconds(60);
|
executor.setKeepAliveSeconds(keepAliveSeconds);
|
||||||
executor.setThreadNamePrefix(threadNamePrefix);
|
executor.setThreadNamePrefix(threadNamePrefix);
|
||||||
executor.setWaitForTasksToCompleteOnShutdown(true);
|
executor.setWaitForTasksToCompleteOnShutdown(true);
|
||||||
// 线程池对拒绝任务的处理策略,当线程池没有处理能力的时候,该策略会直接在 execute 方法的调用线程中运行被拒绝的任务;如果执行程序已关闭,则会丢弃该任务
|
// 线程池对拒绝任务的处理策略,当线程池没有处理能力的时候,该策略会直接在 execute 方法的调用线程中运行被拒绝的任务;如果执行程序已关闭,则会丢弃该任务
|
||||||
@ -103,6 +108,7 @@ public class SmsMainConfig {
|
|||||||
return new RedisUtils(redisTemplate);
|
return new RedisUtils(redisTemplate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** 注入一个定时器*/
|
||||||
@Bean
|
@Bean
|
||||||
public DelayedTime delayedTime(){
|
public DelayedTime delayedTime(){
|
||||||
return new DelayedTime();
|
return new DelayedTime();
|
||||||
|
|||||||
@ -18,6 +18,20 @@
|
|||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.apistd.uni</groupId>
|
||||||
|
<artifactId>uni-sdk</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>kim.wind</groupId>
|
||||||
|
<artifactId>sms-aggregation-comm</artifactId>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>kim.wind</groupId>
|
||||||
|
<artifactId>sms-aggregation-api</artifactId>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@ -0,0 +1,38 @@
|
|||||||
|
package com.example.sms.unisms.config;
|
||||||
|
|
||||||
|
import com.apistd.uni.Uni;
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ConfigurationProperties(prefix = "sms.uni-sms") //指定配置文件注入属性前缀
|
||||||
|
@Data
|
||||||
|
@ConditionalOnProperty(prefix = "sms", name = "supplier", havingValue = "uni-sms")
|
||||||
|
public class UniSmsConfig {
|
||||||
|
|
||||||
|
/** 访问键标识*/
|
||||||
|
private String accessKeyId;
|
||||||
|
/** 访问键秘钥 简易模式不需要配置*/
|
||||||
|
private String accessKeySecret;
|
||||||
|
/** 是否为简易模式*/
|
||||||
|
private String isSimple = "true";
|
||||||
|
/** 短信签名*/
|
||||||
|
private String signature;
|
||||||
|
/** 模板Id*/
|
||||||
|
private String templateId;
|
||||||
|
/** 模板变量名称*/
|
||||||
|
private String templateName;
|
||||||
|
|
||||||
|
/** 自动注入短信配置*/
|
||||||
|
@Bean
|
||||||
|
public void buildSms(){
|
||||||
|
if ("true".equals(isSimple)){
|
||||||
|
Uni.init(accessKeyId);
|
||||||
|
}else {
|
||||||
|
Uni.init(accessKeyId,accessKeySecret);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -0,0 +1,179 @@
|
|||||||
|
package com.example.sms.unisms.service;
|
||||||
|
|
||||||
|
import com.apistd.uni.UniResponse;
|
||||||
|
import com.apistd.uni.sms.UniMessage;
|
||||||
|
import com.apistd.uni.sms.UniSMS;
|
||||||
|
import com.example.sms.unisms.config.UniSmsConfig;
|
||||||
|
import kim.wind.sms.api.SmsBlend;
|
||||||
|
import kim.wind.sms.api.callback.CallBack;
|
||||||
|
import kim.wind.sms.comm.annotation.Restricted;
|
||||||
|
import kim.wind.sms.comm.delayedTime.DelayedTime;
|
||||||
|
import kim.wind.sms.comm.entity.SmsResponse;
|
||||||
|
import kim.wind.sms.comm.exception.SmsBlendException;
|
||||||
|
import kim.wind.sms.comm.utils.HTTPUtils;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.beans.factory.annotation.Qualifier;
|
||||||
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||||
|
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.TimerTask;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>类名: UniSmsImpl
|
||||||
|
* <p>说明: uniSms短信实现
|
||||||
|
*
|
||||||
|
* @author :Wind
|
||||||
|
* 2023/3/26 17:10
|
||||||
|
**/
|
||||||
|
@EnableConfigurationProperties({UniSmsConfig.class})
|
||||||
|
@Slf4j
|
||||||
|
public class UniSmsImpl implements SmsBlend {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private UniSmsConfig config;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
@Qualifier("smsExecutor")
|
||||||
|
private Executor pool;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private DelayedTime delayed;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public SmsResponse sendMessage(String phone, String message) {
|
||||||
|
if ("".equals(config.getTemplateId()) && "".equals(config.getTemplateName())){
|
||||||
|
throw new SmsBlendException("配置文件模板id和模板变量不能为空!");
|
||||||
|
}
|
||||||
|
LinkedHashMap<String, String>map = new LinkedHashMap<>();
|
||||||
|
map.put(config.getTemplateName(),message);
|
||||||
|
return sendMessage(phone, config.getTemplateId(),map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public SmsResponse sendMessage(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||||
|
UniMessage uniMes = UniSMS.buildMessage().setSignature(config.getSignature()).setTo(phone)
|
||||||
|
.setTemplateId(templateId)
|
||||||
|
.setTemplateData(messages);
|
||||||
|
return getSmsResponse(uniMes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public SmsResponse massTexting(List<String> phones, String message) {
|
||||||
|
if ("".equals(config.getTemplateId()) && "".equals(config.getTemplateName())){
|
||||||
|
throw new SmsBlendException("配置文件模板id和模板变量不能为空!");
|
||||||
|
}
|
||||||
|
LinkedHashMap<String, String>map = new LinkedHashMap<>();
|
||||||
|
map.put(config.getTemplateName(),message);
|
||||||
|
return massTexting(phones, config.getTemplateId(),map);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public SmsResponse massTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages) {
|
||||||
|
if (phones.size()>1000){
|
||||||
|
throw new SmsBlendException("单次发送超过最大发送上限,建议每次群发短信人数低于1000");
|
||||||
|
}
|
||||||
|
String[] s = new String[phones.size()];
|
||||||
|
UniMessage uniMes = UniSMS.buildMessage().setSignature(config.getSignature()).setTo(phones.toArray(s))
|
||||||
|
.setTemplateId(templateId)
|
||||||
|
.setTemplateData(messages);
|
||||||
|
return getSmsResponse(uniMes);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public void sendMessageAsync(String phone, String message, CallBack callBack) {
|
||||||
|
pool.execute(()->{
|
||||||
|
SmsResponse smsResponse = sendMessage(phone, message);
|
||||||
|
callBack.callBack(smsResponse);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessageAsync(String phone, String message) {
|
||||||
|
pool.execute(()->{
|
||||||
|
sendMessage(phone, message);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages, CallBack callBack) {
|
||||||
|
pool.execute(()->{
|
||||||
|
SmsResponse smsResponse = sendMessage(phone,templateId,messages);
|
||||||
|
callBack.callBack(smsResponse);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendMessageAsync(String phone, String templateId, LinkedHashMap<String, String> messages) {
|
||||||
|
pool.execute(()->{
|
||||||
|
sendMessage(phone,templateId,messages);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public void delayedMessage(String phone, String message, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
sendMessage(phone,message);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Restricted
|
||||||
|
public void delayedMessage(String phone, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
sendMessage(phone,templateId,messages);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delayMassTexting(List<String> phones, String message, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
massTexting(phones,message);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void delayMassTexting(List<String> phones, String templateId, LinkedHashMap<String, String> messages, Long delayedTime) {
|
||||||
|
this.delayed.schedule(new TimerTask() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
massTexting(phones,templateId,messages);
|
||||||
|
}
|
||||||
|
},delayedTime);
|
||||||
|
}
|
||||||
|
|
||||||
|
private SmsResponse getSmsResponse(UniMessage uniMes) {
|
||||||
|
SmsResponse smsResponse = new SmsResponse();
|
||||||
|
try {
|
||||||
|
UniResponse send = uniMes.send();
|
||||||
|
smsResponse.setCode(send.status);
|
||||||
|
smsResponse.setErrorCode(send.code);
|
||||||
|
smsResponse.setMessage(send.message);
|
||||||
|
smsResponse.setBizId(send.requestId);
|
||||||
|
smsResponse.setData(HTTPUtils.getJSONObject(send));
|
||||||
|
}catch(Exception e){
|
||||||
|
smsResponse.setErrMessage(e.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
return smsResponse;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user