mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
🆕 #3847 【开放平台】新增 wx-java-open-multi-spring-boot-starter 支持多开放平台配置
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.autoconfigure;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.WxOpenMultiServiceConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号自动配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@Import(WxOpenMultiServiceConfiguration.class)
|
||||
public class WxOpenMultiAutoConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInJedisConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInMemoryConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInRedisTemplateConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.configuration.services.WxOpenInRedissonConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 微信开放平台相关服务自动注册
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WxOpenMultiProperties.class)
|
||||
@Import({
|
||||
WxOpenInJedisConfiguration.class,
|
||||
WxOpenInMemoryConfiguration.class,
|
||||
WxOpenInRedissonConfiguration.class,
|
||||
WxOpenInRedisTemplateConfiguration.class
|
||||
})
|
||||
public class WxOpenMultiServiceConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenSingleProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServicesImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* WxOpenConfigStorage 抽象配置类
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public abstract class AbstractWxOpenConfiguration {
|
||||
|
||||
protected WxOpenMultiServices wxOpenMultiServices(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
Map<String, WxOpenSingleProperties> appsMap = wxOpenMultiProperties.getApps();
|
||||
if (appsMap == null || appsMap.isEmpty()) {
|
||||
log.warn("微信开放平台应用参数未配置,通过 WxOpenMultiServices#getWxOpenService(\"tenantId\")获取实例将返回空");
|
||||
return new WxOpenMultiServicesImpl();
|
||||
}
|
||||
/**
|
||||
* 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。
|
||||
*/
|
||||
Collection<WxOpenSingleProperties> apps = appsMap.values();
|
||||
if (apps.size() > 1) {
|
||||
// 校验 appId 是否唯一
|
||||
String nullAppIdPlaceholder = "__NULL_APP_ID__";
|
||||
boolean multi = apps.stream()
|
||||
// 没有 appId,如果不判断是否为空,这里会报 NPE 异常
|
||||
.collect(Collectors.groupingBy(c -> c.getAppId() == null ? nullAppIdPlaceholder : c.getAppId(), Collectors.counting()))
|
||||
.entrySet().stream().anyMatch(e -> e.getValue() > 1);
|
||||
if (multi) {
|
||||
throw new RuntimeException("请确保微信开放平台配置 appId 的唯一性");
|
||||
}
|
||||
}
|
||||
WxOpenMultiServicesImpl services = new WxOpenMultiServicesImpl();
|
||||
|
||||
Set<Map.Entry<String, WxOpenSingleProperties>> entries = appsMap.entrySet();
|
||||
for (Map.Entry<String, WxOpenSingleProperties> entry : entries) {
|
||||
String tenantId = entry.getKey();
|
||||
WxOpenSingleProperties wxOpenSingleProperties = entry.getValue();
|
||||
WxOpenInMemoryConfigStorage storage = this.wxOpenConfigStorage(wxOpenMultiProperties);
|
||||
this.configApp(storage, wxOpenSingleProperties);
|
||||
this.configHttp(storage, wxOpenMultiProperties.getConfigStorage());
|
||||
WxOpenService wxOpenService = this.wxOpenService(storage, wxOpenMultiProperties);
|
||||
services.addWxOpenService(tenantId, wxOpenService);
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 WxOpenInMemoryConfigStorage
|
||||
*
|
||||
* @param wxOpenMultiProperties 参数
|
||||
* @return WxOpenInMemoryConfigStorage
|
||||
*/
|
||||
protected abstract WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties);
|
||||
|
||||
public WxOpenService wxOpenService(WxOpenConfigStorage configStorage, WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenService wxOpenService = new WxOpenServiceImpl();
|
||||
wxOpenService.setWxOpenConfigStorage(configStorage);
|
||||
return wxOpenService;
|
||||
}
|
||||
|
||||
private void configApp(WxOpenInMemoryConfigStorage config, WxOpenSingleProperties appProperties) {
|
||||
String appId = appProperties.getAppId();
|
||||
String secret = appProperties.getSecret();
|
||||
String token = appProperties.getToken();
|
||||
String aesKey = appProperties.getAesKey();
|
||||
String apiHostUrl = appProperties.getApiHostUrl();
|
||||
String accessTokenUrl = appProperties.getAccessTokenUrl();
|
||||
|
||||
// appId 和 secret 是必需的
|
||||
if (StringUtils.isBlank(appId)) {
|
||||
throw new IllegalArgumentException("微信开放平台 appId 不能为空");
|
||||
}
|
||||
if (StringUtils.isBlank(secret)) {
|
||||
throw new IllegalArgumentException("微信开放平台 secret 不能为空");
|
||||
}
|
||||
|
||||
config.setComponentAppId(appId);
|
||||
config.setComponentAppSecret(secret);
|
||||
if (StringUtils.isNotBlank(token)) {
|
||||
config.setComponentToken(token);
|
||||
}
|
||||
if (StringUtils.isNotBlank(aesKey)) {
|
||||
config.setComponentAesKey(aesKey);
|
||||
}
|
||||
// 设置URL配置
|
||||
config.setApiHostUrl(StringUtils.trimToNull(apiHostUrl));
|
||||
config.setAccessTokenUrl(StringUtils.trimToNull(accessTokenUrl));
|
||||
}
|
||||
|
||||
private void configHttp(WxOpenInMemoryConfigStorage config, WxOpenMultiProperties.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);
|
||||
}
|
||||
}
|
||||
|
||||
// 设置重试配置
|
||||
int maxRetryTimes = storage.getMaxRetryTimes();
|
||||
if (maxRetryTimes < 0) {
|
||||
maxRetryTimes = 0;
|
||||
}
|
||||
int retrySleepMillis = storage.getRetrySleepMillis();
|
||||
if (retrySleepMillis < 0) {
|
||||
retrySleepMillis = 1000;
|
||||
}
|
||||
config.setRetrySleepMillis(retrySleepMillis);
|
||||
config.setMaxRetryTimes(maxRetryTimes);
|
||||
|
||||
// 设置自定义的HttpClient超时配置
|
||||
ApacheHttpClientBuilder clientBuilder = config.getApacheHttpClientBuilder();
|
||||
if (clientBuilder == null) {
|
||||
clientBuilder = DefaultApacheHttpClientBuilder.get();
|
||||
}
|
||||
if (clientBuilder instanceof DefaultApacheHttpClientBuilder) {
|
||||
DefaultApacheHttpClientBuilder defaultBuilder = (DefaultApacheHttpClientBuilder) clientBuilder;
|
||||
defaultBuilder.setConnectionTimeout(storage.getConnectionTimeout());
|
||||
defaultBuilder.setSoTimeout(storage.getSoTimeout());
|
||||
defaultBuilder.setConnectionRequestTimeout(storage.getConnectionRequestTimeout());
|
||||
config.setApacheHttpClientBuilder(defaultBuilder);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
|
||||
/**
|
||||
* 自动装配基于 jedis 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "JEDIS"
|
||||
)
|
||||
@ConditionalOnClass({JedisPool.class, JedisPoolConfig.class})
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInJedisConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configJedis(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
private WxOpenInRedisConfigStorage configJedis(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiRedisProperties redisProperties = wxOpenMultiProperties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
jedisPool = getJedisPool(wxOpenMultiProperties);
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
return new WxOpenInRedisConfigStorage(jedisPool, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiProperties.ConfigStorage storage = wxOpenMultiProperties.getConfigStorage();
|
||||
WxOpenMultiRedisProperties 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,33 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于内存策略配置
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "MEMORY", matchIfMissing = true
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInMemoryConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return new WxOpenInMemoryConfigStorage();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedisTemplateConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redis template 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate"
|
||||
)
|
||||
@ConditionalOnClass(StringRedisTemplate.class)
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInRedisTemplateConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configRedisTemplate();
|
||||
}
|
||||
|
||||
private WxOpenInRedisTemplateConfigStorage configRedisTemplate() {
|
||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
return new WxOpenInRedisTemplateConfigStorage(redisTemplate, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
|
||||
import me.chanjar.weixin.open.api.impl.WxOpenInRedissonConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisson 策略配置
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxOpenMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson"
|
||||
)
|
||||
@ConditionalOnClass({Redisson.class, RedissonClient.class})
|
||||
@RequiredArgsConstructor
|
||||
public class WxOpenInRedissonConfiguration extends AbstractWxOpenConfiguration {
|
||||
private final WxOpenMultiProperties wxOpenMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxOpenMultiServices wxOpenMultiServices() {
|
||||
return this.wxOpenMultiServices(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
return this.configRedisson(wxOpenMultiProperties);
|
||||
}
|
||||
|
||||
private WxOpenInRedissonConfigStorage configRedisson(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiRedisProperties redisProperties = wxOpenMultiProperties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient(wxOpenMultiProperties);
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxOpenInRedissonConfigStorage(redissonClient, wxOpenMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient(WxOpenMultiProperties wxOpenMultiProperties) {
|
||||
WxOpenMultiProperties.ConfigStorage storage = wxOpenMultiProperties.getConfigStorage();
|
||||
WxOpenMultiRedisProperties 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,125 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号配置属性.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@ConfigurationProperties(WxOpenMultiProperties.PREFIX)
|
||||
public class WxOpenMultiProperties implements Serializable {
|
||||
private static final long serialVersionUID = -5358245184407791011L;
|
||||
public static final String PREFIX = "wx.open";
|
||||
|
||||
private Map<String, WxOpenSingleProperties> apps = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
private final 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:open:multi";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private final WxOpenMultiRedisProperties redis = new WxOpenMultiRedisProperties();
|
||||
|
||||
/**
|
||||
* 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#setMaxRetryTimes(int)}
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* 连接超时时间,单位毫秒
|
||||
*/
|
||||
private int connectionTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 读数据超时时间,即socketTimeout,单位毫秒
|
||||
*/
|
||||
private int soTimeout = 5000;
|
||||
|
||||
/**
|
||||
* 从连接池获取链接的超时时间,单位毫秒
|
||||
*/
|
||||
private int connectionRequestTimeout = 5000;
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存
|
||||
*/
|
||||
memory,
|
||||
/**
|
||||
* jedis
|
||||
*/
|
||||
jedis,
|
||||
/**
|
||||
* redisson
|
||||
*/
|
||||
redisson,
|
||||
/**
|
||||
* redisTemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 微信开放平台多账号Redis配置.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxOpenMultiRedisProperties 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,49 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 微信开放平台单个应用配置.
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxOpenSingleProperties implements Serializable {
|
||||
private static final long serialVersionUID = 1980986361098922525L;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置微信开放平台的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 自定义API主机地址,用于替换默认的 https://api.weixin.qq.com
|
||||
* 例如:http://proxy.company.com:8080
|
||||
*/
|
||||
private String apiHostUrl;
|
||||
|
||||
/**
|
||||
* 自定义获取AccessToken地址,用于向自定义统一服务获取AccessToken
|
||||
* 例如:http://proxy.company.com:8080/oauth/token
|
||||
*/
|
||||
private String accessTokenUrl;
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.service;
|
||||
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
|
||||
/**
|
||||
* 微信开放平台 {@link WxOpenService} 所有实例存放类.
|
||||
*
|
||||
* @author binarywang
|
||||
*/
|
||||
public interface WxOpenMultiServices {
|
||||
/**
|
||||
* 通过租户 Id 获取 WxOpenService
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @return WxOpenService
|
||||
*/
|
||||
WxOpenService getWxOpenService(String tenantId);
|
||||
|
||||
/**
|
||||
* 根据租户 Id,从列表中移除一个 WxOpenService 实例
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
*/
|
||||
void removeWxOpenService(String tenantId);
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.binarywang.spring.starter.wxjava.open.service;
|
||||
|
||||
import me.chanjar.weixin.open.api.WxOpenService;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 微信开放平台 {@link WxOpenMultiServices} 默认实现
|
||||
*
|
||||
* @author Binary Wang
|
||||
*/
|
||||
public class WxOpenMultiServicesImpl implements WxOpenMultiServices {
|
||||
private final Map<String, WxOpenService> services = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public WxOpenService getWxOpenService(String tenantId) {
|
||||
return this.services.get(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据租户 Id,添加一个 WxOpenService 到列表
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @param wxOpenService WxOpenService 实例
|
||||
*/
|
||||
public void addWxOpenService(String tenantId, WxOpenService wxOpenService) {
|
||||
this.services.put(tenantId, wxOpenService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeWxOpenService(String tenantId) {
|
||||
this.services.remove(tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.binarywang.spring.starter.wxjava.open.autoconfigure.WxOpenMultiAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
com.binarywang.spring.starter.wxjava.open.autoconfigure.WxOpenMultiAutoConfiguration
|
||||
Reference in New Issue
Block a user