🎨 #3225【企业微信】多企业微信配置starter增加 http 客户端配置支持

This commit is contained in:
foreveryang321 2024-01-30 10:20:31 +08:00 committed by Binary Wang
parent 3590de139c
commit 2ec6a0fe11
11 changed files with 97 additions and 56 deletions

View File

@ -42,6 +42,8 @@
## ConfigStorage 配置(选填) ## ConfigStorage 配置(选填)
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
## http 客户端配置(选填) ## http 客户端配置(选填)
## # http客户端类型: http_client(默认), ok_http, jodd_http
wx.cp.config-storage.http-client-type=http_client
wx.cp.config-storage.http-proxy-host= wx.cp.config-storage.http-proxy-host=
wx.cp.config-storage.http-proxy-port= wx.cp.config-storage.http-proxy-port=
wx.cp.config-storage.http-proxy-username= wx.cp.config-storage.http-proxy-username=
@ -57,7 +59,6 @@
```java ```java
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices; import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpServices;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.WxCpUserService; import me.chanjar.weixin.cp.api.WxCpUserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;

View File

@ -1,7 +1,6 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure; package com.binarywang.spring.starter.wxjava.cp.autoconfigure;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.configuration.WxCpMultiServicesAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -12,9 +11,6 @@ import org.springframework.context.annotation.Import;
* created on 2023/10/16 * created on 2023/10/16
*/ */
@Configuration @Configuration
@EnableConfigurationProperties(WxCpMultiProperties.class) @Import(WxCpMultiServicesAutoConfiguration.class)
@Import({
WxCpMultiServicesAutoConfiguration.class
})
public class WxCpMultiAutoConfiguration { public class WxCpMultiAutoConfiguration {
} }

View File

@ -1,10 +1,11 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure; package com.binarywang.spring.starter.wxjava.cp.configuration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInJedisConfiguration; import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInJedisConfiguration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInMemoryConfiguration; import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInMemoryConfiguration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedisTemplateConfiguration; import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedisTemplateConfiguration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedissonConfiguration; import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedissonConfiguration;
import lombok.RequiredArgsConstructor; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
@ -15,7 +16,7 @@ import org.springframework.context.annotation.Import;
* created on 2023/10/16 * created on 2023/10/16
*/ */
@Configuration @Configuration
@RequiredArgsConstructor @EnableConfigurationProperties(WxCpMultiProperties.class)
@Import({ @Import({
WxCpInJedisConfiguration.class, WxCpInJedisConfiguration.class,
WxCpInMemoryConfiguration.class, WxCpInMemoryConfiguration.class,

View File

@ -1,13 +1,16 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; package com.binarywang.spring.starter.wxjava.cp.configuration.services;
import com.binarywang.spring.starter.wxjava.cp.properties.CorpProperties;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpSingleProperties;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices; import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServicesImpl; import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServicesImpl;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService; import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceApacheHttpClientImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceJoddHttpImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage; import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
@ -28,8 +31,8 @@ import java.util.stream.Collectors;
@Slf4j @Slf4j
public abstract class AbstractWxCpConfiguration { public abstract class AbstractWxCpConfiguration {
protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProperties) { protected WxCpMultiServices wxCpMultiServices(WxCpMultiProperties wxCpMultiProperties) {
Map<String, CorpProperties> corps = wxCpMultiProperties.getCorps(); Map<String, WxCpSingleProperties> corps = wxCpMultiProperties.getCorps();
if (corps == null || corps.isEmpty()) { if (corps == null || corps.isEmpty()) {
log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpService(\"tenantId\")获取实例将返回空"); log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpService(\"tenantId\")获取实例将返回空");
return new WxCpMultiServicesImpl(); return new WxCpMultiServicesImpl();
@ -39,13 +42,13 @@ public abstract class AbstractWxCpConfiguration {
* *
* 查看 {@link me.chanjar.weixin.cp.config.impl.AbstractWxCpInRedisConfigImpl#setAgentId(Integer)} * 查看 {@link me.chanjar.weixin.cp.config.impl.AbstractWxCpInRedisConfigImpl#setAgentId(Integer)}
*/ */
Collection<CorpProperties> corpList = corps.values(); Collection<WxCpSingleProperties> corpList = corps.values();
if (corpList.size() > 1) { if (corpList.size() > 1) {
// 先按 corpId 分组统计 // 先按 corpId 分组统计
Map<String, List<CorpProperties>> corpsMap = corpList.stream() Map<String, List<WxCpSingleProperties>> corpsMap = corpList.stream()
.collect(Collectors.groupingBy(CorpProperties::getCorpId)); .collect(Collectors.groupingBy(WxCpSingleProperties::getCorpId));
Set<Map.Entry<String, List<CorpProperties>>> entries = corpsMap.entrySet(); Set<Map.Entry<String, List<WxCpSingleProperties>>> entries = corpsMap.entrySet();
for (Map.Entry<String, List<CorpProperties>> entry : entries) { for (Map.Entry<String, List<WxCpSingleProperties>> entry : entries) {
String corpId = entry.getKey(); String corpId = entry.getKey();
// 校验每个企业下agentId 是否唯一 // 校验每个企业下agentId 是否唯一
boolean multi = entry.getValue().stream() boolean multi = entry.getValue().stream()
@ -59,14 +62,14 @@ public abstract class AbstractWxCpConfiguration {
} }
WxCpMultiServicesImpl services = new WxCpMultiServicesImpl(); WxCpMultiServicesImpl services = new WxCpMultiServicesImpl();
Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet(); Set<Map.Entry<String, WxCpSingleProperties>> entries = corps.entrySet();
for (Map.Entry<String, CorpProperties> entry : entries) { for (Map.Entry<String, WxCpSingleProperties> entry : entries) {
String tenantId = entry.getKey(); String tenantId = entry.getKey();
CorpProperties corpProperties = entry.getValue(); WxCpSingleProperties wxCpSingleProperties = entry.getValue();
WxCpDefaultConfigImpl storage = this.configWxCpDefaultConfigImpl(wxCpMultiProperties); WxCpDefaultConfigImpl storage = this.wxCpConfigStorage(wxCpMultiProperties);
this.configCorp(storage, corpProperties); this.configCorp(storage, wxCpSingleProperties);
this.configHttp(storage, wxCpMultiProperties.getConfigStorage()); this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
WxCpService wxCpService = this.configWxCpService(storage, wxCpMultiProperties.getConfigStorage()); WxCpService wxCpService = this.wxCpService(storage, wxCpMultiProperties.getConfigStorage());
services.addWxCpService(tenantId, wxCpService); services.addWxCpService(tenantId, wxCpService);
} }
return services; return services;
@ -78,12 +81,26 @@ public abstract class AbstractWxCpConfiguration {
* @param wxCpMultiProperties 参数 * @param wxCpMultiProperties 参数
* @return WxCpDefaultConfigImpl * @return WxCpDefaultConfigImpl
*/ */
protected abstract WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties); protected abstract WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties);
private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) { private WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
WxCpService wxCpService = new WxCpServiceImpl(); WxCpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType();
WxCpService wxCpService;
switch (httpClientType) {
case OK_HTTP:
wxCpService = new WxCpServiceOkHttpImpl();
break;
case JODD_HTTP:
wxCpService = new WxCpServiceJoddHttpImpl();
break;
case HTTP_CLIENT:
wxCpService = new WxCpServiceApacheHttpClientImpl();
break;
default:
wxCpService = new WxCpServiceImpl();
break;
}
wxCpService.setWxCpConfigStorage(wxCpConfigStorage); wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
int maxRetryTimes = storage.getMaxRetryTimes(); int maxRetryTimes = storage.getMaxRetryTimes();
if (maxRetryTimes < 0) { if (maxRetryTimes < 0) {
maxRetryTimes = 0; maxRetryTimes = 0;
@ -97,15 +114,15 @@ public abstract class AbstractWxCpConfiguration {
return wxCpService; return wxCpService;
} }
private void configCorp(WxCpDefaultConfigImpl config, CorpProperties corpProperties) { private void configCorp(WxCpDefaultConfigImpl config, WxCpSingleProperties wxCpSingleProperties) {
String corpId = corpProperties.getCorpId(); String corpId = wxCpSingleProperties.getCorpId();
String corpSecret = corpProperties.getCorpSecret(); String corpSecret = wxCpSingleProperties.getCorpSecret();
Integer agentId = corpProperties.getAgentId(); Integer agentId = wxCpSingleProperties.getAgentId();
String token = corpProperties.getToken(); String token = wxCpSingleProperties.getToken();
String aesKey = corpProperties.getAesKey(); String aesKey = wxCpSingleProperties.getAesKey();
// 企业微信私钥会话存档路径 // 企业微信私钥会话存档路径
String msgAuditPriKey = corpProperties.getMsgAuditPriKey(); String msgAuditPriKey = wxCpSingleProperties.getMsgAuditPriKey();
String msgAuditLibPath = corpProperties.getMsgAuditLibPath(); String msgAuditLibPath = wxCpSingleProperties.getMsgAuditLibPath();
config.setCorpId(corpId); config.setCorpId(corpId);
config.setCorpSecret(corpSecret); config.setCorpSecret(corpSecret);

View File

@ -1,4 +1,4 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; package com.binarywang.spring.starter.wxjava.cp.configuration.services;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties;
@ -31,11 +31,11 @@ public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {
@Bean @Bean
public WxCpMultiServices wxCpMultiServices() { public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties); return this.wxCpMultiServices(wxCpMultiProperties);
} }
@Override @Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) { protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedis(wxCpMultiProperties); return this.configRedis(wxCpMultiProperties);
} }

View File

@ -1,4 +1,4 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; package com.binarywang.spring.starter.wxjava.cp.configuration.services;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices; import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
@ -24,11 +24,11 @@ public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {
@Bean @Bean
public WxCpMultiServices wxCpMultiServices() { public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties); return this.wxCpMultiServices(wxCpMultiProperties);
} }
@Override @Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) { protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configInMemory(); return this.configInMemory();
} }

View File

@ -1,4 +1,4 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; package com.binarywang.spring.starter.wxjava.cp.configuration.services;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices; import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
@ -28,11 +28,11 @@ public class WxCpInRedisTemplateConfiguration extends AbstractWxCpConfiguration
@Bean @Bean
public WxCpMultiServices wxCpMultiServices() { public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties); return this.wxCpMultiServices(wxCpMultiProperties);
} }
@Override @Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) { protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedisTemplate(wxCpMultiProperties); return this.configRedisTemplate(wxCpMultiProperties);
} }

View File

@ -1,4 +1,4 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; package com.binarywang.spring.starter.wxjava.cp.configuration.services;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties; import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties;
@ -33,11 +33,11 @@ public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {
@Bean @Bean
public WxCpMultiServices wxCpMultiServices() { public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties); return this.wxCpMultiServices(wxCpMultiProperties);
} }
@Override @Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) { protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedisson(wxCpMultiProperties); return this.configRedisson(wxCpMultiProperties);
} }

View File

@ -18,10 +18,11 @@ import java.util.Map;
@Data @Data
@NoArgsConstructor @NoArgsConstructor
@ConfigurationProperties(prefix = WxCpMultiProperties.PREFIX) @ConfigurationProperties(prefix = WxCpMultiProperties.PREFIX)
public class WxCpMultiProperties { public class WxCpMultiProperties implements Serializable {
private static final long serialVersionUID = -1569510477055668503L;
public static final String PREFIX = "wx.cp"; public static final String PREFIX = "wx.cp";
private Map<String, CorpProperties> corps = new HashMap<>(); private Map<String, WxCpSingleProperties> corps = new HashMap<>();
/** /**
* 配置存储策略默认内存 * 配置存储策略默认内存
@ -48,6 +49,11 @@ public class WxCpMultiProperties {
@NestedConfigurationProperty @NestedConfigurationProperty
private WxCpMultiRedisProperties redis = new WxCpMultiRedisProperties(); private WxCpMultiRedisProperties redis = new WxCpMultiRedisProperties();
/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HTTP_CLIENT;
/** /**
* http代理主机 * http代理主机
*/ */
@ -105,4 +111,19 @@ public class WxCpMultiProperties {
*/ */
redistemplate redistemplate
} }
public enum HttpClientType {
/**
* HttpClient
*/
HTTP_CLIENT,
/**
* OkHttp
*/
OK_HTTP,
/**
* JoddHttp
*/
JODD_HTTP
}
} }

View File

@ -1,6 +1,7 @@
package com.binarywang.spring.starter.wxjava.cp.properties; package com.binarywang.spring.starter.wxjava.cp.properties;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable; import java.io.Serializable;
@ -11,6 +12,7 @@ import java.io.Serializable;
* created on 2023/10/16 * created on 2023/10/16
*/ */
@Data @Data
@NoArgsConstructor
public class WxCpMultiRedisProperties implements Serializable { public class WxCpMultiRedisProperties implements Serializable {
private static final long serialVersionUID = -5924815351660074401L; private static final long serialVersionUID = -5924815351660074401L;

View File

@ -3,6 +3,8 @@ package com.binarywang.spring.starter.wxjava.cp.properties;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import java.io.Serializable;
/** /**
* 企业微信企业相关配置属性 * 企业微信企业相关配置属性
* *
@ -11,7 +13,8 @@ import lombok.NoArgsConstructor;
*/ */
@Data @Data
@NoArgsConstructor @NoArgsConstructor
public class CorpProperties { public class WxCpSingleProperties implements Serializable {
private static final long serialVersionUID = -7502823825007859418L;
/** /**
* 微信企业号 corpId * 微信企业号 corpId
*/ */