🆕 添加 solon-plugins 下的两个新模块

This commit is contained in:
西东
2024-10-09 12:33:11 +08:00
committed by GitHub
parent 17399b59f5
commit 42621285fb
36 changed files with 1737 additions and 11 deletions

View File

@@ -0,0 +1,147 @@
package com.binarywang.solon.wxjava.miniapp.configuration.services;
import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceJoddHttpImpl;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceOkHttpImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiProperties;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaSingleProperties;
import com.binarywang.solon.wxjava.miniapp.service.WxMaMultiServices;
import com.binarywang.solon.wxjava.miniapp.service.WxMaMultiServicesImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import java.util.Collection;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
/**
* WxMaConfigStorage 抽象配置类
*
* @author monch
* created on 2024/9/6
*/
@RequiredArgsConstructor
@Slf4j
public abstract class AbstractWxMaConfiguration {
protected WxMaMultiServices wxMaMultiServices(WxMaMultiProperties wxMaMultiProperties) {
Map<String, WxMaSingleProperties> appsMap = wxMaMultiProperties.getApps();
if (appsMap == null || appsMap.isEmpty()) {
log.warn("微信公众号应用参数未配置,通过 WxMaMultiServices#getWxMaService(\"tenantId\")获取实例将返回空");
return new WxMaMultiServicesImpl();
}
/**
* 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
*
* 查看 {@link cn.binarywang.wx.miniapp.config.impl.WxMaRedisConfigImpl#setAppId(String)}
*/
Collection<WxMaSingleProperties> apps = appsMap.values();
if (apps.size() > 1) {
// 校验 appId 是否唯一
boolean multi = apps.stream()
// 没有 appId如果不判断是否为空这里会报 NPE 异常
.collect(Collectors.groupingBy(c -> c.getAppId() == null ? 0 : c.getAppId(), Collectors.counting()))
.entrySet().stream().anyMatch(e -> e.getValue() > 1);
if (multi) {
throw new RuntimeException("请确保微信公众号配置 appId 的唯一性");
}
}
WxMaMultiServicesImpl services = new WxMaMultiServicesImpl();
Set<Map.Entry<String, WxMaSingleProperties>> entries = appsMap.entrySet();
for (Map.Entry<String, WxMaSingleProperties> entry : entries) {
String tenantId = entry.getKey();
WxMaSingleProperties wxMaSingleProperties = entry.getValue();
WxMaDefaultConfigImpl storage = this.wxMaConfigStorage(wxMaMultiProperties);
this.configApp(storage, wxMaSingleProperties);
this.configHttp(storage, wxMaMultiProperties.getConfigStorage());
WxMaService wxMaService = this.wxMaService(storage, wxMaMultiProperties);
services.addWxMaService(tenantId, wxMaService);
}
return services;
}
/**
* 配置 WxMaDefaultConfigImpl
*
* @param wxMaMultiProperties 参数
* @return WxMaDefaultConfigImpl
*/
protected abstract WxMaDefaultConfigImpl wxMaConfigStorage(WxMaMultiProperties wxMaMultiProperties);
public WxMaService wxMaService(WxMaConfig wxMaConfig, WxMaMultiProperties wxMaMultiProperties) {
WxMaMultiProperties.ConfigStorage storage = wxMaMultiProperties.getConfigStorage();
WxMaMultiProperties.HttpClientType httpClientType = storage.getHttpClientType();
WxMaService wxMaService;
switch (httpClientType) {
case OK_HTTP:
wxMaService = new WxMaServiceOkHttpImpl();
break;
case JODD_HTTP:
wxMaService = new WxMaServiceJoddHttpImpl();
break;
case HTTP_CLIENT:
wxMaService = new WxMaServiceHttpClientImpl();
break;
default:
wxMaService = new WxMaServiceImpl();
break;
}
wxMaService.setWxMaConfig(wxMaConfig);
int maxRetryTimes = storage.getMaxRetryTimes();
if (maxRetryTimes < 0) {
maxRetryTimes = 0;
}
int retrySleepMillis = storage.getRetrySleepMillis();
if (retrySleepMillis < 0) {
retrySleepMillis = 1000;
}
wxMaService.setRetrySleepMillis(retrySleepMillis);
wxMaService.setMaxRetryTimes(maxRetryTimes);
return wxMaService;
}
private void configApp(WxMaDefaultConfigImpl config, WxMaSingleProperties corpProperties) {
String appId = corpProperties.getAppId();
String appSecret = corpProperties.getAppSecret();
String token = corpProperties.getToken();
String aesKey = corpProperties.getAesKey();
boolean useStableAccessToken = corpProperties.isUseStableAccessToken();
config.setAppid(appId);
config.setSecret(appSecret);
if (StringUtils.isNotBlank(token)) {
config.setToken(token);
}
if (StringUtils.isNotBlank(aesKey)) {
config.setAesKey(aesKey);
}
config.useStableAccessToken(useStableAccessToken);
}
private void configHttp(WxMaDefaultConfigImpl config, WxMaMultiProperties.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.miniapp.configuration.services;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisConfigImpl;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiProperties;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiRedisProperties;
import com.binarywang.solon.wxjava.miniapp.service.WxMaMultiServices;
import lombok.RequiredArgsConstructor;
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 monch
* created on 2024/9/6
*/
@Configuration
@Condition(
onProperty = "${"+WxMaMultiProperties.PREFIX + ".configStorage.type} = jedis",
onClass = JedisPool.class
)
@RequiredArgsConstructor
public class WxMaInJedisConfiguration extends AbstractWxMaConfiguration {
private final WxMaMultiProperties wxMaMultiProperties;
private final AppContext applicationContext;
@Bean
public WxMaMultiServices wxMaMultiServices() {
return this.wxMaMultiServices(wxMaMultiProperties);
}
@Override
protected WxMaDefaultConfigImpl wxMaConfigStorage(WxMaMultiProperties wxMaMultiProperties) {
return this.configRedis(wxMaMultiProperties);
}
private WxMaDefaultConfigImpl configRedis(WxMaMultiProperties wxMaMultiProperties) {
WxMaMultiRedisProperties wxMaMultiRedisProperties = wxMaMultiProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (wxMaMultiRedisProperties != null && StringUtils.isNotEmpty(wxMaMultiRedisProperties.getHost())) {
jedisPool = getJedisPool(wxMaMultiProperties);
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
return new WxMaRedisConfigImpl(jedisPool);
}
private JedisPool getJedisPool(WxMaMultiProperties wxMaMultiProperties) {
WxMaMultiProperties.ConfigStorage storage = wxMaMultiProperties.getConfigStorage();
WxMaMultiRedisProperties 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,39 @@
package com.binarywang.solon.wxjava.miniapp.configuration.services;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiProperties;
import com.binarywang.solon.wxjava.miniapp.service.WxMaMultiServices;
import lombok.RequiredArgsConstructor;
import org.noear.solon.annotation.Bean;
import org.noear.solon.annotation.Condition;
import org.noear.solon.annotation.Configuration;
/**
* 自动装配基于内存策略配置
*
* @author monch
* created on 2024/9/6
*/
@Configuration
@Condition(
onProperty = "${"+WxMaMultiProperties.PREFIX + ".configStorage.type:memory} = memory"
)
@RequiredArgsConstructor
public class WxMaInMemoryConfiguration extends AbstractWxMaConfiguration {
private final WxMaMultiProperties wxMaMultiProperties;
@Bean
public WxMaMultiServices wxMaMultiServices() {
return this.wxMaMultiServices(wxMaMultiProperties);
}
@Override
protected WxMaDefaultConfigImpl wxMaConfigStorage(WxMaMultiProperties wxMaMultiProperties) {
return this.configInMemory();
}
private WxMaDefaultConfigImpl configInMemory() {
return new WxMaDefaultConfigImpl();
// return new WxMaDefaultConfigImpl();
}
}

View File

@@ -0,0 +1,68 @@
package com.binarywang.solon.wxjava.miniapp.configuration.services;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiProperties;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiRedisProperties;
import com.binarywang.solon.wxjava.miniapp.service.WxMaMultiServices;
import lombok.RequiredArgsConstructor;
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 monch
* created on 2024/9/6
*/
@Configuration
@Condition(
onProperty = "${"+WxMaMultiProperties.PREFIX + ".configStorage.type} = redisson",
onClass = Redisson.class
)
@RequiredArgsConstructor
public class WxMaInRedissonConfiguration extends AbstractWxMaConfiguration {
private final WxMaMultiProperties wxMaMultiProperties;
private final AppContext applicationContext;
@Bean
public WxMaMultiServices wxMaMultiServices() {
return this.wxMaMultiServices(wxMaMultiProperties);
}
@Override
protected WxMaDefaultConfigImpl wxMaConfigStorage(WxMaMultiProperties wxMaMultiProperties) {
return this.configRedisson(wxMaMultiProperties);
}
private WxMaDefaultConfigImpl configRedisson(WxMaMultiProperties wxMaMultiProperties) {
WxMaMultiRedisProperties redisProperties = wxMaMultiProperties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient(wxMaMultiProperties);
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxMaRedissonConfigImpl(redissonClient, wxMaMultiProperties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient(WxMaMultiProperties wxMaMultiProperties) {
WxMaMultiProperties.ConfigStorage storage = wxMaMultiProperties.getConfigStorage();
WxMaMultiRedisProperties 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,22 @@
package com.binarywang.solon.wxjava.miniapp.integration;
import com.binarywang.solon.wxjava.miniapp.configuration.services.WxMaInJedisConfiguration;
import com.binarywang.solon.wxjava.miniapp.configuration.services.WxMaInMemoryConfiguration;
import com.binarywang.solon.wxjava.miniapp.configuration.services.WxMaInRedissonConfiguration;
import com.binarywang.solon.wxjava.miniapp.properties.WxMaMultiProperties;
import org.noear.solon.core.AppContext;
import org.noear.solon.core.Plugin;
/**
* @author noear 2024/10/9 created
*/
public class WxMiniappMultiPluginImpl implements Plugin {
@Override
public void start(AppContext context) throws Throwable {
context.beanMake(WxMaMultiProperties.class);
context.beanMake(WxMaInJedisConfiguration.class);
context.beanMake(WxMaInMemoryConfiguration.class);
context.beanMake(WxMaInRedissonConfiguration.class);
}
}

View File

@@ -0,0 +1,154 @@
package com.binarywang.solon.wxjava.miniapp.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 monch created on 2024/9/6
* @author noear
*/
@Data
@NoArgsConstructor
@Configuration
@Inject("${" + WxMaMultiProperties.PREFIX + "}")
public class WxMaMultiProperties implements Serializable {
private static final long serialVersionUID = -5358245184407791011L;
public static final String PREFIX = "wx.ma";
private Map<String, WxMaSingleProperties> apps = new HashMap<>();
/**
* 自定义host配置
*/
private HostConfig hosts;
/**
* 存储策略
*/
private final ConfigStorage configStorage = new ConfigStorage();
@Data
@NoArgsConstructor
public static class HostConfig implements Serializable {
private static final long serialVersionUID = -4172767630740346001L;
/**
* 对应于https://api.weixin.qq.com
*/
private String apiHost;
/**
* 对应于https://open.weixin.qq.com
*/
private String openHost;
/**
* 对应于https://mp.weixin.qq.com
*/
private String mpHost;
}
@Data
@NoArgsConstructor
public static class ConfigStorage implements Serializable {
private static final long serialVersionUID = 4815731027000065434L;
/**
* 存储类型.
*/
private StorageType type = StorageType.MEMORY;
/**
* 指定key前缀.
*/
private String keyPrefix = "wx:ma:multi";
/**
* redis连接配置.
*/
private final WxMaMultiRedisProperties redis = new WxMaMultiRedisProperties();
/**
* 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 cn.binarywang.wx.miniapp.api.WxMaService#setMaxRetryTimes(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
* </pre>
*/
private int maxRetryTimes = 5;
/**
* http 请求重试间隔
* <pre>
* {@link cn.binarywang.wx.miniapp.api.WxMaService#setRetrySleepMillis(int)}
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
}
public enum StorageType {
/**
* 内存
*/
MEMORY,
/**
* jedis
*/
JEDIS,
/**
* redisson
*/
REDISSON,
/**
* redisTemplate
*/
REDIS_TEMPLATE
}
public enum HttpClientType {
/**
* HttpClient
*/
HTTP_CLIENT,
/**
* OkHttp
*/
OK_HTTP,
/**
* JoddHttp
*/
JODD_HTTP
}
}

View File

@@ -0,0 +1,56 @@
package com.binarywang.solon.wxjava.miniapp.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author monch
* created on 2024/9/6
*/
@Data
@NoArgsConstructor
public class WxMaMultiRedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L;
/**
* 主机地址.
*/
private String host = "127.0.0.1";
/**
* 端口号.
*/
private int port = 6379;
/**
* 密码.
*/
private String password;
/**
* 超时.
*/
private int timeout = 2000;
/**
* 数据库.
*/
private int database = 0;
/**
* sentinel ips
*/
private String sentinelIps;
/**
* sentinel name
*/
private String sentinelName;
private Integer maxActive;
private Integer maxIdle;
private Integer maxWaitMillis;
private Integer minIdle;
}

View File

@@ -0,0 +1,40 @@
package com.binarywang.solon.wxjava.miniapp.properties;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
/**
* @author monch
* created on 2024/9/6
*/
@Data
@NoArgsConstructor
public class WxMaSingleProperties implements Serializable {
private static final long serialVersionUID = 1980986361098922525L;
/**
* 设置微信公众号的 appid.
*/
private String appId;
/**
* 设置微信公众号的 app secret.
*/
private String appSecret;
/**
* 设置微信公众号的 token.
*/
private String token;
/**
* 设置微信公众号的 EncodingAESKey.
*/
private String aesKey;
/**
* 是否使用稳定版 Access Token
*/
private boolean useStableAccessToken = false;
}

View File

@@ -0,0 +1,27 @@
package com.binarywang.solon.wxjava.miniapp.service;
import cn.binarywang.wx.miniapp.api.WxMaService;
/**
* 微信小程序 {@link WxMaService} 所有实例存放类.
*
* @author monch
* created on 2024/9/6
*/
public interface WxMaMultiServices {
/**
* 通过租户 Id 获取 WxMaService
*
* @param tenantId 租户 Id
* @return WxMaService
*/
WxMaService getWxMaService(String tenantId);
/**
* 根据租户 Id从列表中移除一个 WxMaService 实例
*
* @param tenantId 租户 Id
*/
void removeWxMaService(String tenantId);
}

View File

@@ -0,0 +1,36 @@
package com.binarywang.solon.wxjava.miniapp.service;
import cn.binarywang.wx.miniapp.api.WxMaService;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* 微信小程序 {@link WxMaMultiServices} 默认实现
*
* @author monch
* created on 2024/9/6
*/
public class WxMaMultiServicesImpl implements WxMaMultiServices {
private final Map<String, WxMaService> services = new ConcurrentHashMap<>();
@Override
public WxMaService getWxMaService(String tenantId) {
return this.services.get(tenantId);
}
/**
* 根据租户 Id添加一个 WxMaService 到列表
*
* @param tenantId 租户 Id
* @param wxMaService WxMaService 实例
*/
public void addWxMaService(String tenantId, WxMaService wxMaService) {
this.services.put(tenantId, wxMaService);
}
@Override
public void removeWxMaService(String tenantId) {
this.services.remove(tenantId);
}
}

View File

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

View File

@@ -0,0 +1,15 @@
package features.test;
import org.junit.jupiter.api.Test;
import org.noear.solon.test.SolonTest;
/**
* @author noear 2024/9/4 created
*/
@SolonTest
public class LoadTest {
@Test
public void load(){
}
}

View File

@@ -0,0 +1,38 @@
# 公众号配置
## 应用 1 配置(必填)
wx.ma.apps.tenantId1.app-id=appId
wx.ma.apps.tenantId1.app-secret=@secret
## 选填
wx.ma.apps.tenantId1.token=@token
wx.ma.apps.tenantId1.aes-key=@aesKey
wx.ma.apps.tenantId1.use-stable-access-token=@useStableAccessToken
## 应用 2 配置(必填)
wx.ma.apps.tenantId2.app-id=@appId
wx.ma.apps.tenantId2.app-secret =@secret
## 选填
wx.ma.apps.tenantId2.token=@token
wx.ma.apps.tenantId2.aes-key=@aesKey
wx.ma.apps.tenantId2.use-stable-access-token=@useStableAccessToken
# ConfigStorage 配置(选填)
## 配置类型: memory(默认), jedis, redisson
wx.ma.config-storage.type=memory
## 相关redis前缀配置: wx:ma:multi(默认)
wx.ma.config-storage.key-prefix=wx:ma:multi
wx.ma.config-storage.redis.host=127.0.0.1
wx.ma.config-storage.redis.port=6379
## 单机和 sentinel 同时存在时优先使用sentinel配置
# wx.ma.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379
# wx.ma.config-storage.redis.sentinel-name=mymaster
# http 客户端配置(选填)
## # http客户端类型: http_client(默认), ok_http, jodd_http
wx.ma.config-storage.http-client-type=http_client
wx.ma.config-storage.http-proxy-host=
wx.ma.config-storage.http-proxy-port=
wx.ma.config-storage.http-proxy-username=
wx.ma.config-storage.http-proxy-password=
## 最大重试次数默认5 次,如果小于 0则为 0
wx.ma.config-storage.max-retry-times=5
## 重试时间间隔步进默认1000 毫秒,如果小于 0则为 1000
wx.ma.config-storage.retry-sleep-millis=1000