mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-01-22 02:52:03 +08:00
🆕 #3217 增加 solon-plugins 适配
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
package com.binarywang.solon.wxjava.qidian.config;
|
||||
|
||||
import com.binarywang.solon.wxjava.qidian.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.qidian.properties.WxQidianProperties;
|
||||
import me.chanjar.weixin.qidian.api.WxQidianService;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceHttpClientImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceJoddHttpImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceOkHttpImpl;
|
||||
import me.chanjar.weixin.qidian.config.WxQidianConfigStorage;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 腾讯企点相关服务自动注册.
|
||||
*
|
||||
* @author alegria
|
||||
*/
|
||||
@Configuration
|
||||
public class WxQidianServiceAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxQidianService.class)
|
||||
public WxQidianService wxQidianService(WxQidianConfigStorage configStorage, WxQidianProperties wxQidianProperties) {
|
||||
HttpClientType httpClientType = wxQidianProperties.getConfigStorage().getHttpClientType();
|
||||
WxQidianService wxQidianService;
|
||||
switch (httpClientType) {
|
||||
case OkHttp:
|
||||
wxQidianService = newWxQidianServiceOkHttpImpl();
|
||||
break;
|
||||
case JoddHttp:
|
||||
wxQidianService = newWxQidianServiceJoddHttpImpl();
|
||||
break;
|
||||
case HttpClient:
|
||||
wxQidianService = newWxQidianServiceHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
wxQidianService = newWxQidianServiceImpl();
|
||||
break;
|
||||
}
|
||||
|
||||
wxQidianService.setWxMpConfigStorage(configStorage);
|
||||
return wxQidianService;
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceImpl() {
|
||||
return new WxQidianServiceImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceHttpClientImpl() {
|
||||
return new WxQidianServiceHttpClientImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceOkHttpImpl() {
|
||||
return new WxQidianServiceOkHttpImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceJoddHttpImpl() {
|
||||
return new WxQidianServiceJoddHttpImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,135 @@
|
||||
package com.binarywang.solon.wxjava.qidian.config;
|
||||
|
||||
import com.binarywang.solon.wxjava.qidian.enums.StorageType;
|
||||
import com.binarywang.solon.wxjava.qidian.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.qidian.properties.WxQidianProperties;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
import me.chanjar.weixin.qidian.bean.WxQidianHostConfig;
|
||||
import me.chanjar.weixin.qidian.config.WxQidianConfigStorage;
|
||||
import me.chanjar.weixin.qidian.config.impl.WxQidianDefaultConfigImpl;
|
||||
import me.chanjar.weixin.qidian.config.impl.WxQidianRedisConfigImpl;
|
||||
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.annotation.Inject;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
import redis.clients.jedis.util.Pool;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 腾讯企点存储策略自动配置.
|
||||
*
|
||||
* @author alegria
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WxQidianStorageAutoConfiguration {
|
||||
private final AppContext applicationContext;
|
||||
|
||||
private final WxQidianProperties wxQidianProperties;
|
||||
|
||||
@Inject("${wx.mp.config-storage.redis.host:")
|
||||
private String redisHost;
|
||||
|
||||
@Inject("${wx.mp.configStorage.redis.host:")
|
||||
private String redisHost2;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxQidianConfigStorage.class)
|
||||
public WxQidianConfigStorage wxQidianConfigStorage() {
|
||||
StorageType type = wxQidianProperties.getConfigStorage().getType();
|
||||
WxQidianConfigStorage config;
|
||||
switch (type) {
|
||||
case Jedis:
|
||||
config = jedisConfigStorage();
|
||||
break;
|
||||
default:
|
||||
config = defaultConfigStorage();
|
||||
break;
|
||||
}
|
||||
// wx host config
|
||||
if (null != wxQidianProperties.getHosts() && StringUtils.isNotEmpty(wxQidianProperties.getHosts().getApiHost())) {
|
||||
WxQidianHostConfig hostConfig = new WxQidianHostConfig();
|
||||
hostConfig.setApiHost(wxQidianProperties.getHosts().getApiHost());
|
||||
hostConfig.setQidianHost(wxQidianProperties.getHosts().getQidianHost());
|
||||
hostConfig.setOpenHost(wxQidianProperties.getHosts().getOpenHost());
|
||||
config.setHostConfig(hostConfig);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxQidianConfigStorage defaultConfigStorage() {
|
||||
WxQidianDefaultConfigImpl config = new WxQidianDefaultConfigImpl();
|
||||
setWxMpInfo(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxQidianConfigStorage jedisConfigStorage() {
|
||||
Pool jedisPool;
|
||||
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
WxQidianRedisConfigImpl wxQidianRedisConfig = new WxQidianRedisConfigImpl(redisOps,
|
||||
wxQidianProperties.getConfigStorage().getKeyPrefix());
|
||||
setWxMpInfo(wxQidianRedisConfig);
|
||||
return wxQidianRedisConfig;
|
||||
}
|
||||
|
||||
private void setWxMpInfo(WxQidianDefaultConfigImpl config) {
|
||||
WxQidianProperties properties = wxQidianProperties;
|
||||
WxQidianProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setAppId(properties.getAppId());
|
||||
config.setSecret(properties.getSecret());
|
||||
config.setToken(properties.getToken());
|
||||
config.setAesKey(properties.getAesKey());
|
||||
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
}
|
||||
|
||||
private Pool getJedisPool() {
|
||||
WxQidianProperties.ConfigStorage storage = wxQidianProperties.getConfigStorage();
|
||||
RedisProperties 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);
|
||||
if (StringUtils.isNotEmpty(redis.getSentinelIps())) {
|
||||
|
||||
Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(","));
|
||||
return new JedisSentinelPool(redis.getSentinelName(), sentinels,config);
|
||||
}
|
||||
|
||||
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
|
||||
redis.getDatabase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.binarywang.solon.wxjava.qidian.enums;
|
||||
|
||||
/**
|
||||
* httpclient类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
HttpClient,
|
||||
/**
|
||||
* OkHttp.
|
||||
*/
|
||||
OkHttp,
|
||||
/**
|
||||
* JoddHttp.
|
||||
*/
|
||||
JoddHttp,
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.solon.wxjava.qidian.enums;
|
||||
|
||||
/**
|
||||
* storage类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存.
|
||||
*/
|
||||
Memory,
|
||||
/**
|
||||
* redis(JedisClient).
|
||||
*/
|
||||
Jedis,
|
||||
/**
|
||||
* redis(Redisson).
|
||||
*/
|
||||
Redisson,
|
||||
/**
|
||||
* redis(RedisTemplate).
|
||||
*/
|
||||
RedisTemplate
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.binarywang.solon.wxjava.qidian.integration;
|
||||
|
||||
import com.binarywang.solon.wxjava.qidian.config.WxQidianServiceAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.qidian.config.WxQidianStorageAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.qidian.properties.WxQidianProperties;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
|
||||
/**
|
||||
* @author noear 2024/9/2 created
|
||||
*/
|
||||
public class WxQidianPluginImpl implements Plugin{
|
||||
@Override
|
||||
public void start(AppContext context) throws Throwable {
|
||||
context.beanMake(WxQidianProperties.class);
|
||||
|
||||
context.beanMake(WxQidianStorageAutoConfiguration.class);
|
||||
context.beanMake(WxQidianServiceAutoConfiguration.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.binarywang.solon.wxjava.qidian.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class HostConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -4172767630740346001L;
|
||||
|
||||
private String apiHost;
|
||||
|
||||
private String openHost;
|
||||
|
||||
private String qidianHost;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.binarywang.solon.wxjava.qidian.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* redis 配置属性.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
@Data
|
||||
public class RedisProperties 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;
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
package com.binarywang.solon.wxjava.qidian.properties;
|
||||
|
||||
import com.binarywang.solon.wxjava.qidian.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.qidian.enums.StorageType;
|
||||
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.qidian.enums.StorageType.Memory;
|
||||
import static com.binarywang.solon.wxjava.qidian.properties.WxQidianProperties.PREFIX;
|
||||
|
||||
/**
|
||||
* 企点接入相关配置属性.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@Inject("${" + PREFIX + "}")
|
||||
public class WxQidianProperties {
|
||||
public static final String PREFIX = "wx.qidian";
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 自定义host配置
|
||||
*/
|
||||
private HostConfig hosts;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
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";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
private RedisProperties redis = new RedisProperties();
|
||||
|
||||
/**
|
||||
* http客户端类型.
|
||||
*/
|
||||
private HttpClientType httpClientType = HttpClientType.HttpClient;
|
||||
|
||||
/**
|
||||
* http代理主机.
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口.
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名.
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码.
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
solon.plugin=com.binarywang.solon.wxjava.qidian.integration.WxQidianPluginImpl
|
||||
solon.plugin.priority=10
|
||||
Reference in New Issue
Block a user