🆕 #3217 增加 solon-plugins 适配
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
package com.binarywang.solon.wxjava.open.config;
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenComponentService;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenMessageRouter;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 微信开放平台相关服务自动注册.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Configuration
|
||||
public class WxOpenServiceAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxOpenService.class, onBean = WxOpenConfigStorage.class)
|
||||
public WxOpenService wxOpenService(WxOpenConfigStorage wxOpenConfigStorage) {
|
||||
WxOpenService wxOpenService = new WxOpenServiceImpl();
|
||||
wxOpenService.setWxOpenConfigStorage(wxOpenConfigStorage);
|
||||
return wxOpenService;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WxOpenMessageRouter wxOpenMessageRouter(WxOpenService wxOpenService) {
|
||||
return new WxOpenMessageRouter(wxOpenService);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WxOpenComponentService wxOpenComponentService(WxOpenService wxOpenService) {
|
||||
return wxOpenService.getWxOpenComponentService();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.binarywang.solon.wxjava.open.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
|
||||
/**
|
||||
* @author yl
|
||||
*/
|
||||
public abstract class AbstractWxOpenConfigStorageConfiguration {
|
||||
|
||||
protected WxOpenInMemoryConfigStorage config(WxOpenInMemoryConfigStorage config, WxOpenProperties properties) {
|
||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
config.setWxOpenInfo(properties.getAppId(), properties.getSecret(), properties.getToken(), properties.getAesKey());
|
||||
config.setHttpProxyHost(storage.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(storage.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(storage.getHttpProxyPassword());
|
||||
Integer httpProxyPort = storage.getHttpProxyPort();
|
||||
if (httpProxyPort != null) {
|
||||
config.setHttpProxyPort(httpProxyPort);
|
||||
}
|
||||
int maxRetryTimes = storage.getMaxRetryTimes();
|
||||
if (maxRetryTimes < 0) {
|
||||
maxRetryTimes = 0;
|
||||
}
|
||||
int retrySleepMillis = storage.getRetrySleepMillis();
|
||||
if (retrySleepMillis < 0) {
|
||||
retrySleepMillis = 1000;
|
||||
}
|
||||
config.setRetrySleepMillis(retrySleepMillis);
|
||||
config.setMaxRetryTimes(maxRetryTimes);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
package com.binarywang.solon.wxjava.open.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenRedisProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author yl
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type} = jedis",
|
||||
onClass = JedisPool.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInJedisConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
|
||||
private final WxOpenProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxOpenConfigStorage.class)
|
||||
public WxOpenConfigStorage wxOpenConfigStorage() {
|
||||
WxOpenInMemoryConfigStorage config = getWxOpenInRedisConfigStorage();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxOpenInRedisConfigStorage getWxOpenInRedisConfigStorage() {
|
||||
WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
return new WxOpenInRedisConfigStorage(jedisPool, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool() {
|
||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
WxOpenRedisProperties 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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.binarywang.solon.wxjava.open.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author yl
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type:memory} = memory"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInMemoryConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
|
||||
private final WxOpenProperties properties;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxOpenConfigStorage.class)
|
||||
public WxOpenConfigStorage wxOpenConfigStorage() {
|
||||
WxOpenInMemoryConfigStorage config = new WxOpenInMemoryConfigStorage();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package com.binarywang.solon.wxjava.open.config.storage;
|
||||
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenRedisProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
|
||||
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;
|
||||
|
||||
/**
|
||||
* @author yl
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxOpenProperties.PREFIX + ".configStorage.type} = redisson",
|
||||
onClass = Redisson.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInRedissonConfigStorageConfiguration extends AbstractWxOpenConfigStorageConfiguration {
|
||||
private final WxOpenProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxOpenConfigStorage.class)
|
||||
public WxOpenConfigStorage wxOpenConfigStorage() {
|
||||
WxOpenInMemoryConfigStorage config = getWxOpenInRedissonConfigStorage();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxOpenInRedissonConfigStorage getWxOpenInRedissonConfigStorage() {
|
||||
WxOpenRedisProperties wxOpenRedisProperties = properties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (wxOpenRedisProperties != null && StringUtils.isNotEmpty(wxOpenRedisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient();
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxOpenInRedissonConfigStorage(redissonClient, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient() {
|
||||
WxOpenProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
WxOpenRedisProperties 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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.binarywang.solon.wxjava.open.integration;
|
||||
|
||||
import com.binarywang.solon.wxjava.open.config.WxOpenServiceAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInJedisConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInMemoryConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.open.config.storage.WxOpenInRedissonConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.open.properties.WxOpenProperties;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
|
||||
/**
|
||||
* @author noear 2024/9/2 created
|
||||
*/
|
||||
public class WxOpenPluginImpl implements Plugin {
|
||||
@Override
|
||||
public void start(AppContext context) throws Throwable {
|
||||
context.beanMake(WxOpenProperties.class);
|
||||
|
||||
context.beanMake(WxOpenServiceAutoConfiguration.class);
|
||||
|
||||
context.beanMake(WxOpenInMemoryConfigStorageConfiguration.class);
|
||||
context.beanMake(WxOpenInJedisConfigStorageConfiguration.class);
|
||||
context.beanMake(WxOpenInRedissonConfigStorageConfiguration.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,138 @@
|
||||
package com.binarywang.solon.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.annotation.Inject;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import static com.binarywang.solon.wxjava.open.properties.WxOpenProperties.PREFIX;
|
||||
import static com.binarywang.solon.wxjava.open.properties.WxOpenProperties.StorageType.memory;
|
||||
|
||||
|
||||
/**
|
||||
* 微信接入相关配置属性.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@Inject("${"+PREFIX+"}")
|
||||
public class WxOpenProperties {
|
||||
public static final String PREFIX = "wx.open";
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 存储策略.
|
||||
*/
|
||||
private ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
|
||||
@Data
|
||||
public static class ConfigStorage implements Serializable {
|
||||
private static final long serialVersionUID = 4815731027000065434L;
|
||||
|
||||
/**
|
||||
* 存储类型.
|
||||
*/
|
||||
private StorageType type = memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀.
|
||||
*/
|
||||
private String keyPrefix = "wx:open";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
private WxOpenRedisProperties redis = new WxOpenRedisProperties();
|
||||
|
||||
/**
|
||||
* http客户端类型.
|
||||
*/
|
||||
private HttpClientType httpClientType = HttpClientType.httpclient;
|
||||
|
||||
/**
|
||||
* http代理主机.
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口.
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名.
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码.
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setRetrySleepMillis(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int retrySleepMillis = 1000;
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.mp.api.impl.BaseWxMpServiceImpl#setMaxRetryTimes(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存.
|
||||
*/
|
||||
memory,
|
||||
/**
|
||||
* jedis.
|
||||
*/
|
||||
jedis,
|
||||
/**
|
||||
* redisson.
|
||||
*/
|
||||
redisson,
|
||||
/**
|
||||
* redistemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
httpclient
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.binarywang.solon.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Redis配置.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Data
|
||||
public class WxOpenRedisProperties 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;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
solon.plugin=com.binarywang.solon.wxjava.open.integration.WxOpenPluginImpl
|
||||
solon.plugin.priority=10
|
||||
Reference in New Issue
Block a user