oa更新:1.支持text,markdown,link消息格式。2.支持异步发送,优先级消息。3.yaml配置新增属性--该配置是否开启。

This commit is contained in:
东风 2023-11-27 22:03:45 +08:00
parent 9c219eaa50
commit 51fc622761
26 changed files with 552 additions and 162 deletions

View File

@ -0,0 +1,12 @@
package org.dromara.oa.api;
import org.dromara.oa.comm.entity.Response;
/**
* @author dongfeng
* @date 2023-10-28 14:26
*/
@FunctionalInterface
public interface OaCallBack {
void callBack(Response smsResponse);
}

View File

@ -20,4 +20,25 @@ public interface OaSender {
* 发送消息
*/
Response sender(Request request, MessageType messageType);
/**
* 异步(回调)
*/
void senderAsync(Request request, MessageType messageType);
/**
* 异步(不回调)
* @param request oa请求体
* @param messageType 消息类型
* @param callBack 回调方法
*/
void senderAsync(Request request, MessageType messageType, OaCallBack callBack);
/**
* 发送带优先级的消息
* @param request oa请求体
* @param messageType 消息类型
*/
void senderAsyncByPriority(Request request, MessageType messageType);
}

View File

@ -2,17 +2,25 @@ package org.dromara.oa.comm.entity;
import lombok.Data;
import org.dromara.oa.comm.enums.MessageType;
import java.util.Collection;
import java.util.List;
@Data
public class Request {
public class Request implements Comparable<Request> {
// 标题
private String title;
// 消息内容
private String content;
// link类型的参数
private String picUrl;
// link类型的参数
private String messageUrl;
private List<String> phoneList;
private List<String> userIdList;
@ -24,4 +32,14 @@ public class Request {
// oa类型
private String oaType;
// 优先级
private Integer priority;
// 消息类型,用于优先级队列
private MessageType messageType;
@Override
public int compareTo(Request other) {
return Integer.compare(other.priority,this.priority);
}
}

View File

@ -9,4 +9,8 @@ public class OaException extends RuntimeException {
public OaException(String message) {
super(message);
}
public OaException(String message,String configId) {
super("configId为{"+configId+"}抛出异常:"+message);
}
}

View File

@ -0,0 +1,18 @@
package org.dromara.oa.comm.task.delayed;
import java.util.Timer;
import java.util.TimerTask;
public class DelayedTime {
private final Timer timer = new Timer(true);
/**
* 延迟队列添加新任务
*/
public void schedule(TimerTask task, long delay) {
timer.schedule(task,delay);
}
}

View File

@ -26,19 +26,6 @@
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.activation</groupId>
<artifactId>javax.activation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.activation</groupId>
<artifactId>jakarta.activation-api</artifactId>
</dependency>
<dependency>
<groupId>com.sun.mail</groupId>
<artifactId>javax.mail</artifactId>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-json</artifactId>

View File

@ -4,7 +4,7 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.oa.comm.content.OaContent;
import org.dromara.oa.core.byteTalk.service.ByteTalkOaImpl;
import org.dromara.oa.core.provider.factory.AbstractProviderFactory;
import org.dromara.oa.core.provider.factory.OaAbstractProviderFactory;
/**
* @author dongfeng
@ -12,7 +12,7 @@ import org.dromara.oa.core.provider.factory.AbstractProviderFactory;
* @date 2023-10-22 21:00
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ByteTalkFactory extends AbstractProviderFactory<ByteTalkOaImpl, ByteTalkConfig> {
public class ByteTalkFactory extends OaAbstractProviderFactory<ByteTalkOaImpl, ByteTalkConfig> {
private static final ByteTalkFactory INSTANCE = new ByteTalkFactory();
/**

View File

@ -9,7 +9,8 @@ import org.dromara.oa.comm.config.OaSupplierConfig;
import org.dromara.oa.comm.content.OaContent;
import org.dromara.oa.core.byteTalk.config.ByteTalkFactory;
import org.dromara.oa.core.dingTalk.config.DingTalkFactory;
import org.dromara.oa.core.provider.factory.BaseProviderFactory;
import org.dromara.oa.core.provider.config.OaConfig;
import org.dromara.oa.core.provider.factory.OaBaseProviderFactory;
import org.dromara.oa.core.provider.factory.OaFactory;
import org.dromara.oa.core.provider.factory.ProviderFactoryHolder;
import org.dromara.oa.core.weTalk.config.WeTalkFactory;
@ -25,12 +26,18 @@ import java.util.Map;
@Slf4j
public class OaBlendsInitializer {
private List<BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factoryList;
private List<OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factoryList;
private final OaConfig oaConfig;
private final Map<String, Map<String, Object>> blends;
public OaBlendsInitializer(Map<String, Map<String, Object>> oas
public OaBlendsInitializer(
List<OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factoryList,
OaConfig oaConfig,
Map<String, Map<String, Object>> oas
) {
this.factoryList=factoryList;
this.oaConfig = oaConfig;
this.blends = oas;
onApplicationEvent();
}
@ -41,12 +48,13 @@ public class OaBlendsInitializer {
for (String configId : blends.keySet()) {
Map<String, Object> configMap = blends.get(configId);
if (Boolean.FALSE.equals(configMap.get("isEnable"))) {
log.warn("configId为"+configId+"的配置未启用,请注意是否需要开启");
continue;
}
Object supplierObj = configMap.get(OaContent.SUPPLIER_KEY);
String supplier = supplierObj == null ? "" : String.valueOf(supplierObj);
supplier = StrUtil.isEmpty(supplier) ? configId : supplier;
BaseProviderFactory<OaSender, OaSupplierConfig> providerFactory = (BaseProviderFactory<OaSender, OaSupplierConfig>) ProviderFactoryHolder.requireForSupplier(supplier);
OaBaseProviderFactory<OaSender, OaSupplierConfig> providerFactory = (OaBaseProviderFactory<OaSender, OaSupplierConfig>) ProviderFactoryHolder.requireForSupplier(supplier);
if (providerFactory == null) {
log.warn("创建\"{}\"的通知webhook服务失败未找到供应商为\"{}\"的服务", configId, supplier);
continue;

View File

@ -0,0 +1,46 @@
package org.dromara.oa.core.config;
import lombok.Data;
import org.dromara.oa.comm.task.delayed.DelayedTime;
import org.dromara.oa.core.provider.config.OaConfig;
import org.dromara.oa.core.provider.factory.OaBeanFactory;
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.Lazy;
import java.util.concurrent.Executor;
/**
* @author dongfeng
* @description TODO
* @date 2023-11-01 18:05
*/
@Data
public class OaMainConfig {
@Bean
@ConfigurationProperties(prefix = "sms-oa") //指定配置文件注入属性前缀
@ConditionalOnProperty(prefix = "sms-oa", name = "config-type", havingValue = "yaml")
protected OaConfig oaConfig() {
return OaBeanFactory.getSmsConfig();
}
/**
* 注入一个定时器
*/
@Bean("oaDelayedTime")
@Lazy
protected DelayedTime delayedTime() {
return OaBeanFactory.getDelayedTime();
}
/**
* 注入线程池
*/
@Bean("oaExecutor")
protected Executor taskExecutor(OaConfig config) {
return OaBeanFactory.setExecutor(config);
}
}

View File

@ -1,10 +1,14 @@
package org.dromara.oa.core.config;
import org.dromara.oa.api.OaSender;
import org.dromara.oa.core.provider.config.OaConfig;
import org.dromara.oa.core.provider.factory.OaBaseProviderFactory;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
/**
@ -16,8 +20,8 @@ public class OaSupplierConfig {
* 注入配置
*/
@Bean
@ConfigurationProperties(prefix = "sms.oas")
@ConditionalOnProperty(prefix = "sms", name = "config-type", havingValue = "yaml")
@ConfigurationProperties(prefix = "sms-oa.oas")
@ConditionalOnProperty(prefix = "sms-oa", name = "config-type", havingValue = "yaml")
protected Map<String, Map<String, Object>> oas() {
return new LinkedHashMap<>();
}
@ -25,7 +29,9 @@ public class OaSupplierConfig {
@Bean
protected OaBlendsInitializer smsOasInitializer(
List<OaBaseProviderFactory<? extends OaSender, ? extends org.dromara.oa.comm.config.OaSupplierConfig>> factoryList,
OaConfig oaConfig,
Map<String, Map<String, Object>> oas) {
return new OaBlendsInitializer(oas);
return new OaBlendsInitializer(factoryList,oaConfig,oas);
}
}

View File

@ -4,7 +4,9 @@ import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.oa.comm.content.OaContent;
import org.dromara.oa.core.dingTalk.service.DingTalkOaImpl;
import org.dromara.oa.core.provider.factory.AbstractProviderFactory;
import org.dromara.oa.core.provider.factory.OaAbstractProviderFactory;
import java.util.concurrent.Executor;
/**
* @author dongfeng
@ -12,7 +14,7 @@ import org.dromara.oa.core.provider.factory.AbstractProviderFactory;
* @date 2023-10-22 21:00
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class DingTalkFactory extends AbstractProviderFactory<DingTalkOaImpl, DingTalkConfig> {
public class DingTalkFactory extends OaAbstractProviderFactory<DingTalkOaImpl, DingTalkConfig> {
private static final DingTalkFactory INSTANCE = new DingTalkFactory();
/**
@ -23,6 +25,7 @@ public class DingTalkFactory extends AbstractProviderFactory<DingTalkOaImpl, Din
return new DingTalkOaImpl(dingTalkConfig);
}
@Override
public String getSupplier() {
return OaContent.DINGTALK;

View File

@ -15,6 +15,7 @@ import org.dromara.oa.core.support.HttpClientImpl;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.Executor;
import static org.dromara.oa.comm.enums.OaType.DINGTALK;
@ -34,16 +35,27 @@ public class DingTalkOaImpl extends AbstractOaBlend<DingTalkConfig> {
super(config);
}
/**
* 建造一个微信通知对象服务
*/
public DingTalkOaImpl(DingTalkConfig config, Executor pool) {
super(config,pool);
}
@Override
public String getSupplier() {
return OaType.DINGTALK.getType();
return getConfig().getSupplier();
}
@Override
public Response sender(Request request, MessageType messageType) {
try {
Thread.sleep(10000L);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
if (Objects.isNull(request.getContent())) {
throw new OaException("消息体content不能为空");
throw new OaException("消息体content不能为空",getConfig().getConfigId());
}
StringBuilder webhook = new StringBuilder();
JSONObject message = null;
@ -62,7 +74,7 @@ public class DingTalkOaImpl extends AbstractOaBlend<DingTalkConfig> {
log.info("请求返回结果:" + post);
} catch (Exception e) {
log.warn("请求失败问题:" + e.getMessage());
throw new OaException(e.getMessage());
throw new OaException(e.getMessage(),getConfig().getConfigId());
}
// 后续解析响应体提取errorCode判断是否成功
return new Response(true, post, config.getConfigId());

View File

@ -1,5 +1,6 @@
package org.dromara.oa.core.dingTalk.utils;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import org.dromara.oa.comm.entity.Request;
import org.dromara.oa.comm.enums.MessageType;
@ -12,9 +13,10 @@ import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import java.util.List;
import java.util.Objects;
import static org.dromara.oa.comm.enums.MessageType.MARKDOWN;
import static org.dromara.oa.comm.enums.MessageType.TEXT;
import static org.dromara.oa.comm.enums.MessageType.*;
/**
* @author dongfeng
@ -45,26 +47,47 @@ public class DingTalkBuilder {
public static JSONObject createMessage(Request request, MessageType messageType) {
JSONObject message = new JSONObject();
if (messageType == TEXT) {
message.set("msgtype", "text");
JSONObject text = new JSONObject();
text.set("content", request.getContent());
JSONObject at = new JSONObject();
at.set("atMobiles", request.getPhoneList());
at.set("isAtAll", request.getIsNoticeAll());
message.set("at", at);
message.set("text", text);
} else if (messageType == MARKDOWN) {
message.set("msgtype", "markdown");
JSONObject markdown = new JSONObject();
markdown.set("text", request.getContent());
markdown.set("title", request.getTitle());
JSONObject at = new JSONObject();
at.set("atMobiles", request.getPhoneList());
message.set("at", at);
message.set("markdown", markdown);
} else if (messageType == LINK) {
message.set("msgtype", "link");
JSONObject link = new JSONObject();
link.set("text", request.getContent());
link.set("title", request.getTitle());
link.set("picUrl", request.getPicUrl());
link.set("messageUrl", request.getMessageUrl());
message.set("link", link);
}
// 处理提到的人
JSONObject at = new JSONObject();
List<String> phoneList = request.getPhoneList();
List<String> userIdList = request.getUserIdList();
if(!Objects.isNull(phoneList)){
JSONArray phoneArray = new JSONArray();
phoneList.forEach(phoneArray::set);
at.set("atMobiles", phoneArray);
}
if(!Objects.isNull(userIdList)){
JSONArray userIdArray = new JSONArray();
userIdList.forEach(userIdArray::set);
at.set("atUserIds", userIdArray);
}
at.set("isAtAll", request.getIsNoticeAll());
message.set("at", at);
return message;
}
}

View File

@ -19,6 +19,9 @@ public abstract class OaBaseConfig implements OaSupplierConfig {
private String tokenId;
private String sign;
/**
* 默认开启
*/
private Boolean isEnable = true;

View File

@ -0,0 +1,31 @@
package org.dromara.oa.core.provider.config;
import lombok.Data;
/**
* @author dongfeng
* @description TODO
* @date 2023-11-01 17:55
*/
@Data
public class OaConfig {
/**
* 核心线程池大小
*/
private Integer corePoolSize = 10;
/**
* 最大线程数
*/
private Integer maxPoolSize = 30;
/**
* 队列容量
*/
private Integer queueCapacity = 50;
/**
* 设置线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
*/
private Boolean shutdownStrategy = true;
}

View File

@ -6,11 +6,11 @@ import org.dromara.oa.comm.config.OaSupplierConfig;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
public abstract class AbstractProviderFactory<S extends OaSender, C extends OaSupplierConfig> implements BaseProviderFactory<S, C> {
public abstract class OaAbstractProviderFactory<S extends OaSender, C extends OaSupplierConfig> implements OaBaseProviderFactory<S, C> {
private Class<C> configClass;
public AbstractProviderFactory() {
public OaAbstractProviderFactory() {
Type genericSuperclass = getClass().getGenericSuperclass();
if (genericSuperclass instanceof ParameterizedType) {
ParameterizedType paramType = (ParameterizedType) genericSuperclass;

View File

@ -3,7 +3,9 @@ package org.dromara.oa.core.provider.factory;
import org.dromara.oa.api.OaSender;
import org.dromara.oa.comm.config.OaSupplierConfig;
public interface BaseProviderFactory<S extends OaSender, C extends OaSupplierConfig> {
import java.util.concurrent.Executor;
public interface OaBaseProviderFactory<S extends OaSender, C extends OaSupplierConfig> {
/**
* 创建通知webhook实现对象

View File

@ -0,0 +1,85 @@
package org.dromara.oa.core.provider.factory;
import org.dromara.oa.comm.task.delayed.DelayedTime;
import org.dromara.oa.comm.entity.Request;
import org.dromara.oa.core.provider.config.OaConfig;
import java.util.concurrent.*;
public class OaBeanFactory {
/** 定时器*/
private static DelayedTime delayedTime;
/** 线程池*/
private static Executor executor;
/** 核心配置信息*/
private static OaConfig oaConfig;
/** 优先级队列*/
private static PriorityBlockingQueue<Request> priorityBlockingQueue;
/** 优先级队列*/
private static Boolean priorityExecutorThreadStatus = false;
private OaBeanFactory() {
}
public static DelayedTime getDelayedTime() {
if (delayedTime == null){
delayedTime = new DelayedTime();
}
return delayedTime;
}
public static Executor setExecutor(OaConfig config) {
if (executor == null){
// 创建一个线程池对象
ThreadPoolExecutor ex = new ThreadPoolExecutor(
config.getCorePoolSize(),
config.getMaxPoolSize(),
config.getQueueCapacity(),
TimeUnit.SECONDS,
new ArrayBlockingQueue<>(config.getMaxPoolSize())
);
// 线程池对拒绝任务的处理策略,当线程池没有处理能力的时候该策略会直接在 execute 方法的调用线程中运行被拒绝的任务如果执行程序已关闭则会丢弃该任务
ex.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor = ex;
}
return executor;
}
public static PriorityBlockingQueue<Request> initPriorityBlockingQueue() {
if (priorityBlockingQueue == null){
// 创建一个线程池对象
priorityBlockingQueue=new PriorityBlockingQueue<>();
}
return priorityBlockingQueue;
}
public static Executor getExecutor() {
return executor;
}
public static OaConfig getSmsConfig(){
if (oaConfig == null){
oaConfig = new OaConfig();
}
return oaConfig;
}
public static PriorityBlockingQueue<Request> getPriorityBlockingQueue(){
return priorityBlockingQueue;
}
public static Boolean getPriorityExecutorThreadStatus() {
return priorityExecutorThreadStatus;
}
public static Boolean setPriorityExecutorThreadStatus(Boolean bo) {
priorityExecutorThreadStatus=bo;
return priorityExecutorThreadStatus;
}
}

View File

@ -33,9 +33,9 @@ public class OaFactory {
}
public static OaSender createAndGetOa(OaSupplierConfig config) {
BaseProviderFactory factory = ProviderFactoryHolder.requireForSupplier(config.getSupplier());
OaBaseProviderFactory factory = ProviderFactoryHolder.requireForSupplier(config.getSupplier());
if (factory == null) {
throw new OaException("不支持当前供应商配置");
throw new OaException("不支持"+config.getSupplier()+"供应商配置");
}
return factory.createSmsOa(config);
}

View File

@ -16,20 +16,20 @@ import java.util.concurrent.ConcurrentHashMap;
public class ProviderFactoryHolder {
private static final Map<String, BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factories = new ConcurrentHashMap<>();
private static final Map<String, OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factories = new ConcurrentHashMap<>();
public static void registerFactory(BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> factory) {
public static void registerFactory(OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> factory) {
if (factory == null) {
throw new OaException("注册供应商工厂失败,工厂实例不能为空");
}
factories.put(factory.getSupplier(), factory);
}
public static void registerFactory(List<BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factoryList) {
public static void registerFactory(List<OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig>> factoryList) {
if (CollUtil.isEmpty(factoryList)) {
return;
}
for (BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> factory : factoryList) {
for (OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> factory : factoryList) {
if (factory == null) {
continue;
}
@ -37,7 +37,7 @@ public class ProviderFactoryHolder {
}
}
public static BaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> requireForSupplier(String supplier) {
public static OaBaseProviderFactory<? extends OaSender, ? extends OaSupplierConfig> requireForSupplier(String supplier) {
return factories.getOrDefault(supplier, null);
}
}

View File

@ -2,8 +2,18 @@ package org.dromara.oa.core.provider.service;
import cn.hutool.core.util.StrUtil;
import lombok.Getter;
import org.dromara.oa.api.OaCallBack;
import org.dromara.oa.api.OaSender;
import org.dromara.oa.comm.config.OaSupplierConfig;
import org.dromara.oa.comm.entity.Request;
import org.dromara.oa.comm.entity.Response;
import org.dromara.oa.comm.enums.MessageType;
import org.dromara.oa.core.provider.factory.OaBeanFactory;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.concurrent.PriorityBlockingQueue;
/**
* @author dongfeng
@ -16,12 +26,63 @@ public abstract class AbstractOaBlend<C extends OaSupplierConfig> implements OaS
private final C config;
protected final Executor pool;
protected final PriorityBlockingQueue<Request> priorityQueueMap;
protected AbstractOaBlend(C config, Executor pool) {
this.configId = StrUtil.isEmpty(config.getConfigId()) ? getSupplier() : config.getConfigId();
this.config = config;
this.pool = pool;
this.priorityQueueMap = OaBeanFactory.initPriorityBlockingQueue();
priorityQueueMapThreadInit();
}
protected AbstractOaBlend(C config) {
this.configId = StrUtil.isEmpty(config.getConfigId()) ? getSupplier() : config.getConfigId();
this.config = config;
this.pool = OaBeanFactory.getExecutor();
this.priorityQueueMap = OaBeanFactory.initPriorityBlockingQueue();
priorityQueueMapThreadInit();
}
protected C getConfig() {
return config;
}
protected void priorityQueueMapThreadInit() {
Boolean status = OaBeanFactory.getPriorityExecutorThreadStatus();
if(Boolean.FALSE.equals(status)){
OaBeanFactory.setPriorityExecutorThreadStatus(true);
pool.execute(() -> {
Thread.currentThread().setName("oa-priorityQueueMap-thread");
while (!Thread.currentThread().isInterrupted()) {
Request request = priorityQueueMap.poll();
if (!Objects.isNull(request)) {
pool.execute(() -> {
System.out.println("优先级为"+request.getPriority()+"已发送");
sender(request, request.getMessageType());
});
}
}
});
}
}
public final void senderAsync(Request request, MessageType messageType) {
pool.execute(() -> {
sender(request, messageType);
});
}
public final void senderAsync(Request request, MessageType messageType, OaCallBack callBack) {
CompletableFuture<Response> future = CompletableFuture.supplyAsync(() -> sender(request, messageType));
future.thenAcceptAsync(callBack::callBack);
}
public final void senderAsyncByPriority(Request request, MessageType messageType) {
request.setMessageType(messageType);
priorityQueueMap.offer(request);
}
}

View File

@ -3,7 +3,7 @@ package org.dromara.oa.core.weTalk.config;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.dromara.oa.comm.content.OaContent;
import org.dromara.oa.core.provider.factory.AbstractProviderFactory;
import org.dromara.oa.core.provider.factory.OaAbstractProviderFactory;
import org.dromara.oa.core.weTalk.service.WeTalkOaImpl;
/**
@ -12,7 +12,7 @@ import org.dromara.oa.core.weTalk.service.WeTalkOaImpl;
* @date 2023-10-22 21:00
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class WeTalkFactory extends AbstractProviderFactory<WeTalkOaImpl, WeTalkConfig> {
public class WeTalkFactory extends OaAbstractProviderFactory<WeTalkOaImpl, WeTalkConfig> {
private static final WeTalkFactory INSTANCE = new WeTalkFactory();
/**

View File

@ -1,2 +1,3 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
org.dromara.oa.core.config.OaMainConfig,\
org.dromara.oa.core.config.OaSupplierConfig

View File

@ -1 +1,2 @@
org.dromara.oa.core.config.OaSupplierConfig
org.dromara.oa.core.config.OaMainConfig
org.dromara.oa.core.config.OaSupplierConfig

View File

@ -60,10 +60,11 @@ sms:
template-id: pub_verif_short
# 模版名称
templateName: code
sms-oa:
config-type: yaml
oas:
oaDingTalkByYaml: # configId
isEnable: true # 表示该配置是否生效(默认生效,false表示不生效)
isEnable: false # 表示该配置是否生效(默认生效,false表示不生效)
supplier: dingding # 厂商标识
tokenId: 您的accessKey
sign: 您的sign
@ -73,4 +74,7 @@ sms:
sign: 您的sign
oaWeTalkByYaml:
supplier: wetalk # 厂商标识
tokenId: 您的sign
tokenId: 您的sign
core-pool-size: 20
queue-capacity: 20
max-pool-size: 20

View File

@ -4,160 +4,204 @@ import lombok.extern.slf4j.Slf4j;
import org.dromara.oa.api.OaSender;
import org.dromara.oa.comm.entity.Request;
import org.dromara.oa.comm.enums.MessageType;
import org.dromara.oa.comm.enums.OaType;
import org.dromara.oa.core.byteTalk.config.ByteTalkConfig;
import org.dromara.oa.core.dingTalk.config.DingTalkConfig;
import org.dromara.oa.core.provider.factory.OaFactory;
import org.dromara.oa.core.weTalk.config.WeTalkConfig;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.ArrayList;
import java.util.Random;
@Slf4j
@SpringBootTest
public class SmsOaTest {
//***********************DingTalk-Test************************//
/**
* 填测试手机号
*/
private static final String PHONE = "";
private static final String DingTalkPHONE = "";
/**
* 填access_token
*/
private static final String TOKENID = "";
private static final String DingTalkTOKENID = "";
/**
* 填secret
*/
private static final String SIGN = "";
private static final String DingTalkSIGN = "";
/**
* DingTalk的Text测试
*/
@Test
public void oaDingTalkTest() {
public void oaDingTalkText() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(SIGN);
dingTalkConfig.setTokenId(TOKENID);
// OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.createAndGetOa(dingTalkConfig);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(PHONE);
phones.add(DingTalkPHONE);
//测试text
request.setPhoneList(phones);
request.setIsNoticeAll(true);
request.setIsNoticeAll(false);
request.setContent("测试消息");
request.setTitle("测试消息标题");
// 异步发送方式
alarm.senderAsync(request, MessageType.TEXT);
alarm.senderAsync(request, MessageType.TEXT, smsResponse -> System.out.println("ConfigId为" + smsResponse.getOaConfigId() + "的异步任务发送成功"));
System.out.println("异步任务已全部提交");
System.out.println("下面是同步任务");
alarm.sender(request, MessageType.TEXT);
// 测试markdown,无法@
// request.setContent("#### 杭州天气 @150XXXXXXXX \n > 9度西北风1级空气良89相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
// request.setTitle("杭州天气");
// alarm.sender(request, MessageType.MARKDOWN);
System.out.println("同步任务已执行完");
}
/**
* DingTalk的Markdown测试
*/
@Test
public void oaDingTalkMarkdown() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
request.setPhoneList(phones);
request.setIsNoticeAll(true);
request.setContent("#### 杭州天气 @150XXXXXXXX \n > 9度西北风1级空气良89相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
request.setTitle("测试消息标题");
alarm.senderAsync(request, MessageType.MARKDOWN);
alarm.senderAsync(request, MessageType.MARKDOWN, smsResponse -> System.out.println("ConfigId为" + smsResponse.getOaConfigId() + "的异步任务发送成功"));
System.out.println("异步任务已全部提交");
System.out.println("下面是同步任务");
alarm.sender(request, MessageType.MARKDOWN);
System.out.println("同步任务已执行完");
}
/**
* DingTalk的Link测试
*/
@Test
public void oaDingTalkLink() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
// 测试link
request.setPhoneList(phones);
request.setIsNoticeAll(true);
request.setContent("这个即将发布的新版本创始人xx称它为红树林。而在此之前每当面临重大升级产品经理们都会取一个应景的代号这一次为什么是红树林");
request.setTitle("点击跳转到钉钉");
request.setMessageUrl("https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
request.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
alarm.senderAsync(request, MessageType.LINK);
alarm.senderAsync(request, MessageType.LINK, smsResponse -> System.out.println("ConfigId为" + smsResponse.getOaConfigId() + "的异步任务发送成功"));
System.out.println("异步任务已全部提交");
System.out.println("下面是同步任务");
alarm.sender(request, MessageType.LINK);
System.out.println("同步任务已执行完");
}
/**
* 异步优先级尽可能优先级高的消息先发送,但是获取响应会受网络影响
*/
@Test
public void oaDingTalkAsyncByPriority() {
String key = "oaDingTalk";
DingTalkConfig dingTalkConfig = new DingTalkConfig();
dingTalkConfig.setConfigId(key);
dingTalkConfig.setSign(DingTalkSIGN);
dingTalkConfig.setTokenId(DingTalkTOKENID);
// 根据配置创建服务实例并注册
OaFactory.createAndRegisterOaSender(dingTalkConfig);
OaSender alarm = OaFactory.getSmsOaBlend(key);
// 模拟10条不同优先级的消息
for (int i = 0; i < 10; i++) {
Random random = new Random();
int priority = random.nextInt(10);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(DingTalkPHONE);
request.setPhoneList(phones);
request.setIsNoticeAll(false);
request.setPriority(priority);
request.setTitle("优先级为" + priority);
//测试-1-TEXT
// request.setContent("该消息优先级为"+ priority);
// alarm.senderAsyncByPriority(request, MessageType.TEXT);
// System.out.println("优先级为"+priority+"的异步任务已提交");
// 测试-2-MARKDOWN
// request.setContent("该消息优先级为"+ priority+"\n#### ++杭州天气 @150XXXXXXXX \n > 9度西北风1级空气良89相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
// alarm.senderAsyncByPriority(request, MessageType.MARKDOWN);
// 测试-3-LINK
request.setContent("该消息优先级为" + priority + "这个即将发布的新版本创始人xx称它为红树林。而在此之前每当面临重大升级产品经理们都会取一个应景的代号这一次为什么是红树林");
request.setMessageUrl("https://www.dingtalk.com/s?__biz=MzA4NjMwMTA2Ng==&mid=2650316842&idx=1&sn=60da3ea2b29f1dcc43a7c8e4a7c97a16&scene=2&srcid=09189AnRJEdIiWVaKltFzNTw&from=timeline&isappinstalled=0&key=&ascene=2&uin=&devicetype=android-23&version=26031933&nettype=WIFI");
request.setPicUrl("https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png");
// 发送
alarm.senderAsyncByPriority(request, MessageType.LINK);
System.out.println("优先级为" + priority + "的异步任务已提交");
}
while (true){
}
}
@Test
public void oaDingTalkByYamlTest() {
String configId = "oaDingTalkByYaml";
OaSender alarm = OaFactory.getSmsOaBlend(configId);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(PHONE);
phones.add(DingTalkPHONE);
request.setPhoneList(phones);
request.setIsNoticeAll(false);
request.setContent("HertzBeat");
request.setTitle("HertzBeat");
alarm.sender(request, MessageType.TEXT);
}
@Test
public void oaByteTalkTest() {
String configId = "oaByteTalk";
ByteTalkConfig dingTalkConfig = new ByteTalkConfig();
dingTalkConfig.setConfigId(configId);
dingTalkConfig.setSign(SIGN);
dingTalkConfig.setTokenId(TOKENID);
OaSender alarm = OaFactory.createAndGetOa(dingTalkConfig);
Request request = new Request();
ArrayList<String> userNameList = new ArrayList<>();
userNameList.add("user1");
userNameList.add("user2");
request.setUserNamesList(userNameList);
request.setContent("测试消息");
request.setIsNoticeAll(true);
// request.setTitle("测试消息标题");
alarm.sender(request, MessageType.TEXT);
}
@Test
public void oaByteTalkByYamlTest() {
String configId = "oaByteTalkByYaml";
OaSender alarm = OaFactory.getSmsOaBlend(configId);
Request request = new Request();
request.setOaType(OaType.BYTETALK.getType());
ArrayList<String> userNameList = new ArrayList<>();
userNameList.add("user1");
userNameList.add("user2");
request.setUserNamesList(userNameList);
request.setContent("测试消息");
request.setIsNoticeAll(true);
// request.setTitle("测试消息标题");
alarm.sender(request, MessageType.TEXT);
}
@Test
public void oaWeTalkTest() {
String configId = "oaWeTalk";
WeTalkConfig weTalkConfig = new WeTalkConfig();
weTalkConfig.setConfigId(configId);
weTalkConfig.setTokenId(TOKENID);
OaSender alarm = OaFactory.createAndGetOa(weTalkConfig);
Request request = new Request();
ArrayList<String> phones = new ArrayList<>();
phones.add(PHONE);
phones.add("131");
request.setPhoneList(phones);
ArrayList<String> userIds = new ArrayList<>();
userIds.add("123");
request.setUserIdList(userIds);
// request.setIsNoticeAll(true);
request.setContent("测试消息");
request.setTitle("测试消息标题");
alarm.sender(request, MessageType.TEXT);
// 测试markdown,无法@
// 企业微信的markdown直接是content没有title
// request.setContent("#### 杭州天气 @150XXXXXXXX \n > 9度西北风1级空气良89相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
// request.setTitle("杭州天气");
// alarm.sender(request, MessageType.MARKDOWN);
}
@Test
public void oaWeTalkByYamlTest() {
String configId = "oaWeTalkByYaml";
OaSender alarm = OaFactory.getSmsOaBlend(configId);
Request request = new Request();
request.setOaType(OaType.WETALK.getType());
ArrayList<String> phones = new ArrayList<>();
phones.add(PHONE);
phones.add("131");
request.setPhoneList(phones);
ArrayList<String> userIds = new ArrayList<>();
userIds.add("123");
request.setUserIdList(userIds);
// request.setIsNoticeAll(true);
request.setContent("测试消息");
request.setTitle("测试消息标题");
alarm.sender(request, MessageType.TEXT);
// 测试markdown,无法@
// 企业微信的markdown直接是content没有title
// request.setContent("#### 杭州天气 @150XXXXXXXX \n > 9度西北风1级空气良89相对温度73%\n > ![screenshot](https://img.alicdn.com/tfs/TB1NwmBEL9TBuNjy1zbXXXpepXa-2400-1218.png)\n > ###### 10点20分发布 [天气](https://www.dingtalk.com) \n");
// request.setTitle("杭州天气");
// alarm.sender(request, MessageType.MARKDOWN);
}
}