1
0
mirror of synced 2025-12-12 09:41:35 +08:00

🆕 #3217 增加 solon-plugins 适配

This commit is contained in:
西东
2024-09-02 17:18:56 +08:00
committed by GitHub
parent 8ceca63f28
commit 41bb5e44cc
103 changed files with 5069 additions and 0 deletions

View File

@@ -0,0 +1,162 @@
package com.binarywang.solon.wxjava.cp_multi.configuration.services;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiProperties;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpSingleProperties;
import com.binarywang.solon.wxjava.cp_multi.service.WxCpMultiServices;
import com.binarywang.solon.wxjava.cp_multi.service.WxCpMultiServicesImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceApacheHttpClientImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceJoddHttpImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* WxCpConfigStorage 抽象配置类
*
* @author yl
* created on 2023/10/16
*/
@RequiredArgsConstructor
@Slf4j
public abstract class AbstractWxCpConfiguration {
protected WxCpMultiServices wxCpMultiServices(WxCpMultiProperties wxCpMultiProperties) {
Map<String, WxCpSingleProperties> corps = wxCpMultiProperties.getCorps();
if (corps == null || corps.isEmpty()) {
log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpService(\"tenantId\")获取实例将返回空");
return new WxCpMultiServicesImpl();
}
/**
* 校验同一个企业下agentId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
*
* 查看 {@link me.chanjar.weixin.cp.config.impl.AbstractWxCpInRedisConfigImpl#setAgentId(Integer)}
*/
Collection<WxCpSingleProperties> corpList = corps.values();
if (corpList.size() > 1) {
// 先按 corpId 分组统计
Map<String, List<WxCpSingleProperties>> corpsMap = corpList.stream()
.collect(Collectors.groupingBy(WxCpSingleProperties::getCorpId));
Set<Map.Entry<String, List<WxCpSingleProperties>>> entries = corpsMap.entrySet();
for (Map.Entry<String, List<WxCpSingleProperties>> entry : entries) {
String corpId = entry.getKey();
// 校验每个企业下agentId 是否唯一
boolean multi = entry.getValue().stream()
// 通讯录没有 agentId如果不判断是否为空这里会报 NPE 异常
.collect(Collectors.groupingBy(c -> c.getAgentId() == null ? 0 : c.getAgentId(), Collectors.counting()))
.entrySet().stream().anyMatch(e -> e.getValue() > 1);
if (multi) {
throw new RuntimeException("请确保企业微信配置唯一性[" + corpId + "]");
}
}
}
WxCpMultiServicesImpl services = new WxCpMultiServicesImpl();
Set<Map.Entry<String, WxCpSingleProperties>> entries = corps.entrySet();
for (Map.Entry<String, WxCpSingleProperties> entry : entries) {
String tenantId = entry.getKey();
WxCpSingleProperties wxCpSingleProperties = entry.getValue();
WxCpDefaultConfigImpl storage = this.wxCpConfigStorage(wxCpMultiProperties);
this.configCorp(storage, wxCpSingleProperties);
this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
WxCpService wxCpService = this.wxCpService(storage, wxCpMultiProperties.getConfigStorage());
services.addWxCpService(tenantId, wxCpService);
}
return services;
}
/**
* 配置 WxCpDefaultConfigImpl
*
* @param wxCpMultiProperties 参数
* @return WxCpDefaultConfigImpl
*/
protected abstract WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties);
private WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
WxCpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType();
WxCpService wxCpService;
switch (httpClientType) {
case OK_HTTP:
wxCpService = new WxCpServiceOkHttpImpl();
break;
case JODD_HTTP:
wxCpService = new WxCpServiceJoddHttpImpl();
break;
case HTTP_CLIENT:
wxCpService = new WxCpServiceApacheHttpClientImpl();
break;
default:
wxCpService = new WxCpServiceImpl();
break;
}
wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
int maxRetryTimes = storage.getMaxRetryTimes();
if (maxRetryTimes < 0) {
maxRetryTimes = 0;
}
int retrySleepMillis = storage.getRetrySleepMillis();
if (retrySleepMillis < 0) {
retrySleepMillis = 1000;
}
wxCpService.setRetrySleepMillis(retrySleepMillis);
wxCpService.setMaxRetryTimes(maxRetryTimes);
return wxCpService;
}
private void configCorp(WxCpDefaultConfigImpl config, WxCpSingleProperties wxCpSingleProperties) {
String corpId = wxCpSingleProperties.getCorpId();
String corpSecret = wxCpSingleProperties.getCorpSecret();
Integer agentId = wxCpSingleProperties.getAgentId();
String token = wxCpSingleProperties.getToken();
String aesKey = wxCpSingleProperties.getAesKey();
// 企业微信,私钥,会话存档路径
String msgAuditPriKey = wxCpSingleProperties.getMsgAuditPriKey();
String msgAuditLibPath = wxCpSingleProperties.getMsgAuditLibPath();
config.setCorpId(corpId);
config.setCorpSecret(corpSecret);
config.setAgentId(agentId);
if (StringUtils.isNotBlank(token)) {
config.setToken(token);
}
if (StringUtils.isNotBlank(aesKey)) {
config.setAesKey(aesKey);
}
if (StringUtils.isNotBlank(msgAuditPriKey)) {
config.setMsgAuditPriKey(msgAuditPriKey);
}
if (StringUtils.isNotBlank(msgAuditLibPath)) {
config.setMsgAuditLibPath(msgAuditLibPath);
}
}
private void configHttp(WxCpDefaultConfigImpl config, WxCpMultiProperties.ConfigStorage storage) {
String httpProxyHost = storage.getHttpProxyHost();
Integer httpProxyPort = storage.getHttpProxyPort();
String httpProxyUsername = storage.getHttpProxyUsername();
String httpProxyPassword = storage.getHttpProxyPassword();
if (StringUtils.isNotBlank(httpProxyHost)) {
config.setHttpProxyHost(httpProxyHost);
if (httpProxyPort != null) {
config.setHttpProxyPort(httpProxyPort);
}
if (StringUtils.isNotBlank(httpProxyUsername)) {
config.setHttpProxyUsername(httpProxyUsername);
}
if (StringUtils.isNotBlank(httpProxyPassword)) {
config.setHttpProxyPassword(httpProxyPassword);
}
}
}
}

View File

@@ -0,0 +1,77 @@
package com.binarywang.solon.wxjava.cp_multi.configuration.services;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiProperties;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiRedisProperties;
import com.binarywang.solon.wxjava.cp_multi.service.WxCpMultiServices;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.core.AppContext;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
/**
* 自动装配基于 jedis 策略配置
*
* @author yl
* created on 2023/10/16
*/
@Configuration
@Condition(
onProperty = "${"+WxCpMultiProperties.PREFIX + ".configStorage.type} = jedis",
onClass = JedisPool.class
)
@RequiredArgsConstructor
public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {
private final WxCpMultiProperties wxCpMultiProperties;
private final AppContext applicationContext;
@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.wxCpMultiServices(wxCpMultiProperties);
}
@Override
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedis(wxCpMultiProperties);
}
private WxCpDefaultConfigImpl configRedis(WxCpMultiProperties wxCpMultiProperties) {
WxCpMultiRedisProperties wxCpMultiRedisProperties = wxCpMultiProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (wxCpMultiRedisProperties != null && StringUtils.isNotEmpty(wxCpMultiRedisProperties.getHost())) {
jedisPool = getJedisPool(wxCpMultiProperties);
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
return new WxCpJedisConfigImpl(jedisPool, wxCpMultiProperties.getConfigStorage().getKeyPrefix());
}
private JedisPool getJedisPool(WxCpMultiProperties wxCpMultiProperties) {
WxCpMultiProperties.ConfigStorage storage = wxCpMultiProperties.getConfigStorage();
WxCpMultiRedisProperties redis = storage.getRedis();
JedisPoolConfig config = new JedisPoolConfig();
if (redis.getMaxActive() != null) {
config.setMaxTotal(redis.getMaxActive());
}
if (redis.getMaxIdle() != null) {
config.setMaxIdle(redis.getMaxIdle());
}
if (redis.getMaxWaitMillis() != null) {
config.setMaxWaitMillis(redis.getMaxWaitMillis());
}
if (redis.getMinIdle() != null) {
config.setMinIdle(redis.getMinIdle());
}
config.setTestOnBorrow(true);
config.setTestWhileIdle(true);
return new JedisPool(config, redis.getHost(), redis.getPort(),
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
}
}

View File

@@ -0,0 +1,38 @@
package com.binarywang.solon.wxjava.cp_multi.configuration.services;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiProperties;
import com.binarywang.solon.wxjava.cp_multi.service.WxCpMultiServices;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* 自动装配基于内存策略配置
*
* @author yl
* created on 2023/10/16
*/
@Configuration
@Condition(
onProperty = "${"+WxCpMultiProperties.PREFIX + ".configStorage.type:memory} = memory"
)
@RequiredArgsConstructor
public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {
private final WxCpMultiProperties wxCpMultiProperties;
@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.wxCpMultiServices(wxCpMultiProperties);
}
@Override
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configInMemory();
}
private WxCpDefaultConfigImpl configInMemory() {
return new WxCpDefaultConfigImpl();
}
}

View File

@@ -0,0 +1,68 @@
package com.binarywang.solon.wxjava.cp_multi.configuration.services;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiProperties;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiRedisProperties;
import com.binarywang.solon.wxjava.cp_multi.service.WxCpMultiServices;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpRedissonConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.core.AppContext;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.redisson.config.TransportMode;
/**
* 自动装配基于 redisson 策略配置
*
* @author yl
* created on 2023/10/16
*/
@Configuration
@Condition(
onProperty = "${"+WxCpMultiProperties.PREFIX + ".configStorage.type} = redisson",
onClass = Redisson.class
)
@RequiredArgsConstructor
public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {
private final WxCpMultiProperties wxCpMultiProperties;
private final AppContext applicationContext;
@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.wxCpMultiServices(wxCpMultiProperties);
}
@Override
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedisson(wxCpMultiProperties);
}
private WxCpDefaultConfigImpl configRedisson(WxCpMultiProperties wxCpMultiProperties) {
WxCpMultiRedisProperties redisProperties = wxCpMultiProperties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient(wxCpMultiProperties);
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxCpRedissonConfigImpl(redissonClient, wxCpMultiProperties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient(WxCpMultiProperties wxCpMultiProperties) {
WxCpMultiProperties.ConfigStorage storage = wxCpMultiProperties.getConfigStorage();
WxCpMultiRedisProperties redis = storage.getRedis();
Config config = new Config();
config.useSingleServer()
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
.setDatabase(redis.getDatabase())
.setPassword(redis.getPassword());
config.setTransportMode(TransportMode.NIO);
return Redisson.create(config);
}
}

View File

@@ -0,0 +1,21 @@
package com.binarywang.solon.wxjava.cp_multi.integration;
import com.binarywang.solon.wxjava.cp_multi.configuration.services.WxCpInJedisConfiguration;
import com.binarywang.solon.wxjava.cp_multi.configuration.services.WxCpInMemoryConfiguration;
import com.binarywang.solon.wxjava.cp_multi.configuration.services.WxCpInRedissonConfiguration;
import com.binarywang.solon.wxjava.cp_multi.properties.WxCpMultiProperties;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
/**
* @author noear 2024/9/2 created
*/
public class WxCpMultiPluginImpl implements Plugin {
@Override
public void start(AppContext context) throws Throwable {
context.beanMake(WxCpMultiProperties.class);
context.beanMake(WxCpInJedisConfiguration.class);
context.beanMake(WxCpInMemoryConfiguration.class);
context.beanMake(WxCpInRedissonConfiguration.class);
}
}

View File

@@ -0,0 +1,129 @@
package com.binarywang.solon.wxjava.cp_multi.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
/**
* 企业微信多企业接入相关配置属性
*
* @author yl
* created on 2023/10/16
*/
@Data
@NoArgsConstructor
@Configuration
@Inject("${" + WxCpMultiProperties.PREFIX + "}")
public class WxCpMultiProperties implements Serializable {
private static final long serialVersionUID = -1569510477055668503L;
public static final String PREFIX = "wx.cp";
private Map<String, WxCpSingleProperties> corps = new HashMap<>();
/**
* 配置存储策略,默认内存
*/
private ConfigStorage configStorage = new ConfigStorage();
@Data
@NoArgsConstructor
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;
/**
* 存储类型
*/
private StorageType type = StorageType.memory;
/**
* 指定key前缀
*/
private String keyPrefix = "wx:cp";
/**
* redis连接配置
*/
private WxCpMultiRedisProperties redis = new WxCpMultiRedisProperties();
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HTTP_CLIENT;
/**
* http代理主机
*/
private String httpProxyHost;
/**
* http代理端口
*/
private Integer httpProxyPort;
/**
* http代理用户名
*/
private String httpProxyUsername;
/**
* http代理密码
*/
private String httpProxyPassword;
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.cp.api.WxCpService#setMaxRetryTimes(int)}
* {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
private int maxRetryTimes = 5;
/**
* http 请求重试间隔
* <pre>
* {@link me.chanjar.weixin.cp.api.WxCpService#setRetrySleepMillis(int)}
* {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
}
public enum StorageType {
/**
* 内存
*/
memory,
/**
* jedis
*/
jedis,
/**
* redisson
*/
redisson,
/**
* redistemplate
*/
redistemplate
}
public enum HttpClientType {
/**
* HttpClient
*/
HTTP_CLIENT,
/**
* OkHttp
*/
OK_HTTP,
/**
* JoddHttp
*/
JODD_HTTP
}
}

View File

@@ -0,0 +1,48 @@
package com.binarywang.solon.wxjava.cp_multi.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* Redis配置.
*
* @author yl
* created on 2023/10/16
*/
@Data
@NoArgsConstructor
public class WxCpMultiRedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;
/**
* 主机地址.
*/
private String host;
/**
* 端口号.
*/
private int port = 6379;
/**
* 密码.
*/
private String password;
/**
* 超时.
*/
private int timeout = 2000;
/**
* 数据库.
*/
private int database = 0;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWaitMillis;
private Integer minIdle;
}

View File

@@ -0,0 +1,46 @@
package com.binarywang.solon.wxjava.cp_multi.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* 企业微信企业相关配置属性
*
* @author yl
* created on 2023/10/16
*/
@Data
@NoArgsConstructor
public class WxCpSingleProperties implements Serializable {
private static final long serialVersionUID = -7502823825007859418L;
/**
* 微信企业号 corpId
*/
private String corpId;
/**
* 微信企业号 corpSecret
*/
private String corpSecret;
/**
* 微信企业号应用 token
*/
private String token;
/**
* 微信企业号应用 ID
*/
private Integer agentId;
/**
* 微信企业号应用 EncodingAESKey
*/
private String aesKey;
/**
* 微信企业号应用 会话存档私钥
*/
private String msgAuditPriKey;
/**
* 微信企业号应用 会话存档类库路径
*/
private String msgAuditLibPath;
}

View File

@@ -0,0 +1,26 @@
package com.binarywang.solon.wxjava.cp_multi.service;
import me.chanjar.weixin.cp.api.WxCpService;
/**
* 企业微信 {@link WxCpService} 所有实例存放类.
*
* @author yl
* created on 2023/10/16
*/
public interface WxCpMultiServices {
/**
* 通过租户 Id 获取 WxCpService
*
* @param tenantId 租户 Id
* @return WxCpService
*/
WxCpService getWxCpService(String tenantId);
/**
* 根据租户 Id从列表中移除一个 WxCpService 实例
*
* @param tenantId 租户 Id
*/
void removeWxCpService(String tenantId);
}

View File

@@ -0,0 +1,42 @@
package com.binarywang.solon.wxjava.cp_multi.service;
import me.chanjar.weixin.cp.api.WxCpService;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 企业微信 {@link WxCpMultiServices} 默认实现
*
* @author yl
* created on 2023/10/16
*/
public class WxCpMultiServicesImpl implements WxCpMultiServices {
private final Map<String, WxCpService> services = new ConcurrentHashMap<>();
/**
* 通过租户 Id 获取 WxCpService
*
* @param tenantId 租户 Id
* @return WxCpService
*/
@Override
public WxCpService getWxCpService(String tenantId) {
return this.services.get(tenantId);
}
/**
* 根据租户 Id添加一个 WxCpService 到列表
*
* @param tenantId 租户 Id
* @param wxCpService WxCpService 实例
*/
public void addWxCpService(String tenantId, WxCpService wxCpService) {
this.services.put(tenantId, wxCpService);
}
@Override
public void removeWxCpService(String tenantId) {
this.services.remove(tenantId);
}
}

View File

@@ -0,0 +1,2 @@
solon.plugin=com.binarywang.solon.wxjava.cp_multi.integration.WxCpMultiPluginImpl
solon.plugin.priority=10