🆕 #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,41 @@
# wx-java-cp-solon-plugin
## 快速开始
1. 引入依赖
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-cp-solon-plugin</artifactId>
<version>${version}</version>
</dependency>
```
2. 添加配置(app.properties)
```properties
# 企业微信号配置(必填)
wx.cp.corp-id = @corp-id
wx.cp.corp-secret = @corp-secret
# 选填
wx.cp.agent-id = @agent-id
wx.cp.token = @token
wx.cp.aes-key = @aes-key
wx.cp.msg-audit-priKey = @msg-audit-priKey
wx.cp.msg-audit-lib-path = @msg-audit-lib-path
# ConfigStorage 配置(选填)
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
# http 客户端配置(选填)
wx.cp.config-storage.http-proxy-host=
wx.cp.config-storage.http-proxy-port=
wx.cp.config-storage.http-proxy-username=
wx.cp.config-storage.http-proxy-password=
# 最大重试次数默认5 次,如果小于 0则为 0
wx.cp.config-storage.max-retry-times=5
# 重试时间间隔步进默认1000 毫秒,如果小于 0则为 1000
wx.cp.config-storage.retry-sleep-millis=1000
```
3. 支持自动注入的类型: `WxCpService`, `WxCpConfigStorage`
4. 覆盖自动配置: 自定义注入的bean会覆盖自动注入的
- WxCpService
- WxCpConfigStorage

View File

@@ -0,0 +1,30 @@
<?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">
<parent>
<artifactId>wx-java-solon-plugins</artifactId>
<groupId>com.github.binarywang</groupId>
<version>4.6.4.B</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>wx-java-cp-solon-plugin</artifactId>
<name>WxJava - Solon Plugin for WxCp</name>
<description>微信企业号开发的 Solon Plugin</description>
<dependencies>
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-cp</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<dependency>
<groupId>org.redisson</groupId>
<artifactId>redisson</artifactId>
</dependency>
</dependencies>
</project>

View File

@@ -0,0 +1,43 @@
package com.binarywang.solon.wxjava.cp.config;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* 企业微信平台相关服务自动注册
*
* @author yl
* created on 2021/12/6
*/
@Configuration
@RequiredArgsConstructor
public class WxCpServiceAutoConfiguration {
private final WxCpProperties wxCpProperties;
@Bean
@Condition(onMissingBean = WxCpService.class,
onBean = WxCpConfigStorage.class)
public WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage) {
WxCpService wxCpService = new WxCpServiceImpl();
wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
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;
}
}

View File

@@ -0,0 +1,25 @@
package com.binarywang.solon.wxjava.cp.integration;
import com.binarywang.solon.wxjava.cp.config.WxCpServiceAutoConfiguration;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import com.binarywang.solon.wxjava.cp.storage.WxCpInJedisConfigStorageConfiguration;
import com.binarywang.solon.wxjava.cp.storage.WxCpInMemoryConfigStorageConfiguration;
import com.binarywang.solon.wxjava.cp.storage.WxCpInRedissonConfigStorageConfiguration;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
/**
* @author noear 2024/9/2 created
*/
public class WxCpPluginImpl implements Plugin {
@Override
public void start(AppContext context) throws Throwable {
context.beanMake(WxCpProperties.class);
context.beanMake(WxCpServiceAutoConfiguration.class);
context.beanMake(WxCpInMemoryConfigStorageConfiguration.class);
context.beanMake(WxCpInJedisConfigStorageConfiguration.class);
context.beanMake(WxCpInRedissonConfigStorageConfiguration.class);
}
}

View File

@@ -0,0 +1,133 @@
package com.binarywang.solon.wxjava.cp.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.noear.solon.annotation.Configuration;
import org.noear.solon.annotation.Inject;
import java.io.Serializable;
/**
* 企业微信接入相关配置属性
*
* @author yl
* created on 2021/12/6
*/
@Data
@NoArgsConstructor
@Configuration
@Inject("${" + WxCpProperties.PREFIX + "}")
public class WxCpProperties {
public static final String PREFIX = "wx.cp";
/**
* 微信企业号 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;
/**
* 配置存储策略,默认内存
*/
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 WxCpRedisProperties redis = new WxCpRedisProperties();
/**
* 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
}
}

View File

@@ -0,0 +1,46 @@
package com.binarywang.solon.wxjava.cp.properties;
import lombok.Data;
import java.io.Serializable;
/**
* Redis配置.
*
* @author yl
* created on 2023/04/23
*/
@Data
public class WxCpRedisProperties 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,61 @@
package com.binarywang.solon.wxjava.cp.storage;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
/**
* WxCpConfigStorage 抽象配置类
*
* @author yl & Wang_Wong
* created on 2021/12/6
*/
public abstract class AbstractWxCpConfigStorageConfiguration {
protected WxCpDefaultConfigImpl config(WxCpDefaultConfigImpl config, WxCpProperties properties) {
String corpId = properties.getCorpId();
String corpSecret = properties.getCorpSecret();
Integer agentId = properties.getAgentId();
String token = properties.getToken();
String aesKey = properties.getAesKey();
// 企业微信,私钥,会话存档路径
String msgAuditPriKey = properties.getMsgAuditPriKey();
String msgAuditLibPath = properties.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);
}
WxCpProperties.ConfigStorage storage = properties.getConfigStorage();
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);
}
}
return config;
}
}

View File

@@ -0,0 +1,74 @@
package com.binarywang.solon.wxjava.cp.storage;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import com.binarywang.solon.wxjava.cp.properties.WxCpRedisProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
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/04/23
*/
@Configuration
@Condition(
onProperty = "${"+WxCpProperties.PREFIX + ".configStorage.type} = jedis",
onClass = JedisPool.class
)
@RequiredArgsConstructor
public class WxCpInJedisConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
private final WxCpProperties wxCpProperties;
private final AppContext applicationContext;
@Bean
@Condition(onMissingBean=WxCpConfigStorage.class)
public WxCpConfigStorage wxCpConfigStorage() {
WxCpDefaultConfigImpl config = getConfigStorage();
return this.config(config, wxCpProperties);
}
private WxCpJedisConfigImpl getConfigStorage() {
WxCpRedisProperties wxCpRedisProperties = wxCpProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (wxCpRedisProperties != null && StringUtils.isNotEmpty(wxCpRedisProperties.getHost())) {
jedisPool = getJedisPool();
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
return new WxCpJedisConfigImpl(jedisPool, wxCpProperties.getConfigStorage().getKeyPrefix());
}
private JedisPool getJedisPool() {
WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
WxCpRedisProperties 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,31 @@
package com.binarywang.solon.wxjava.cp.storage;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
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 2021/12/6
*/
@Configuration
@Condition(
onProperty = "${"+WxCpProperties.PREFIX + ".configStorage.type:memory} = memory"
)
@RequiredArgsConstructor
public class WxCpInMemoryConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
private final WxCpProperties wxCpProperties;
@Bean
@Condition(onMissingBean=WxCpConfigStorage.class)
public WxCpConfigStorage wxCpConfigStorage() {
WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
return this.config(config, wxCpProperties);
}
}

View File

@@ -0,0 +1,65 @@
package com.binarywang.solon.wxjava.cp.storage;
import com.binarywang.solon.wxjava.cp.properties.WxCpProperties;
import com.binarywang.solon.wxjava.cp.properties.WxCpRedisProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
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/04/23
*/
@Configuration
@Condition(
onProperty = "${"+WxCpProperties.PREFIX + ".configStorage.type} = redisson",
onClass = Redisson.class
)
@RequiredArgsConstructor
public class WxCpInRedissonConfigStorageConfiguration extends AbstractWxCpConfigStorageConfiguration {
private final WxCpProperties wxCpProperties;
private final AppContext applicationContext;
@Bean
@Condition(onMissingBean=WxCpConfigStorage.class)
public WxCpConfigStorage wxCpConfigStorage() {
WxCpDefaultConfigImpl config = getConfigStorage();
return this.config(config, wxCpProperties);
}
private WxCpRedissonConfigImpl getConfigStorage() {
WxCpRedisProperties redisProperties = wxCpProperties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient();
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxCpRedissonConfigImpl(redissonClient, wxCpProperties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient() {
WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
WxCpRedisProperties 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,2 @@
solon.plugin=com.binarywang.solon.wxjava.cp.integration.WxCpPluginImpl
solon.plugin.priority=10