8000 :art: #3225【企业微信】多企业微信配置starter增加 http 客户端配置支持 · binarywang/WxJava@2ec6a0f · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ec6a0f

Browse files
foreveryang321binarywang
authored andcommitted
🎨 #3225【企业微信】多企业微信配置starter增加 http 客户端配置支持
1 parent 3590de1 commit 2ec6a0f

File tree

11 files changed

+97
-56
lines changed

11 files changed

+97
-56
lines changed

spring-boot-starters/wx-java-cp-multi-spring-boot-starter/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
## ConfigStorage 配置(选填)
4343
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
4444
## http 客户端配置(选填)
45+
## # http客户端类型: http_client(默认), ok_http, jodd_http
46+
wx.cp.config-storage.http-client-type=http_client
4547
wx.cp.config-storage.http-proxy-host=
4648
wx.cp.config-storage.http-proxy-port=
4749
wx.cp.config-storage.http-proxy-username=
@@ -57,7 +59,6 @@
5759

5860
```java
5961
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
60-
import com.binarywang.spring.starter.wxjava.cp.service.WxCpServices;
6162
import me.chanjar.weixin.cp.api.WxCpService;
6263
import me.chanjar.weixin.cp.api.WxCpUserService;
6364
import org.springframework.beans.factory.annotation.Autowired;
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.binarywang.spring.starter.wxjava.cp.autoconfigure;
22

3-
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
4-
import org.springframework.boot.context.properties.EnableConfigurationProperties;
3+
import com.binarywang.spring.starter.wxjava.cp.configuration.WxCpMultiServicesAutoConfiguration;
54
import org.springframework.context.annotation.Configuration;
65
import org.springframework.context.annotation.Import;
76

@@ -12,9 +11,6 @@
1211
* created on 2023/10/16
1312
*/
1413
@Configuration
15-
@EnableConfigurationProperties(WxCpMultiProperties.class)
16-
@Import({
17-
WxCpMultiServicesAutoConfiguration.class
18-
})
14+
@Import(WxCpMultiServicesAutoConfiguration.class)
1915
public class WxCpMultiAutoConfiguration {
2016
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration;
22

3-
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInJedisConfiguration;
4-
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInMemoryConfiguration;
5-
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedisTemplateConfiguration;
6-
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedissonConfiguration;
7-
import lombok.RequiredArgsConstructor;
3+
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInJedisConfiguration;
4+
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInMemoryConfiguration;
5+
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedisTemplateConfiguration;
6+
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedissonConfiguration;
7+
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
8+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
89
import org.springframework.context.annotation.Configuration;
910
import org.springframework.context.annotation.Import;
1011

@@ -15,7 +16,7 @@
1516
* created on 2023/10/16
1617
*/
1718
@Configuration
18-
@RequiredArgsConstructor
19+
@EnableConfigurationProperties(WxCpMultiProperties.class)
1920
@Import({
2021
WxCpInJedisConfiguration.class,
2122
WxCpInMemoryConfiguration.class,
Lines changed: 44 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
22

3-
import com.binarywang.spring.starter.wxjava.cp.properties.CorpProperties;
43
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
4+
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpSingleProperties;
55
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
66
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServicesImpl;
77
import lombok.RequiredArgsConstructor;
88
import lombok.extern.slf4j.Slf4j;
99
import me.chanjar.weixin.cp.api.WxCpService;
10+
import me.chanjar.weixin.cp.api.impl.WxCpServiceApacheHttpClientImpl;
1011
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
12+
import me.chanjar.weixin.cp.api.impl.WxCpServiceJoddHttpImpl;
13+
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
1114
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
1215
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
1316
import org.apache.commons.lang3.StringUtils;
@@ -28,8 +31,8 @@
2831
@Slf4j
2932
public abstract class AbstractWxCpConfiguration {
3033

31-
protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProperties) {
32-
Map<String, CorpProperties> corps = wxCpMultiProperties.getCorps();
34+
protected WxCpMultiServices wxCpMultiServices(WxCpMultiProperties wxCpMultiProperties) {
35+
Map<String, WxCpSingleProperties> corps = wxCpMultiProperties.getCorps();
3336
if (corps == null || corps.isEmpty()) {
3437
log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpService(\"tenantId\")获取实例将返回空");
3538
return new WxCpMultiServicesImpl();
@@ -39,13 +42,13 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
3942
*
4043
* 查看 {@link me.chanjar.weixin.cp.config.impl.AbstractWxCpInRedisConfigImpl#setAgentId(Integer)}
4144
*/
42-
Collection<CorpProperties> corpList = corps.values();
45+
Collection<WxCpSingleProperties> corpList = corps.values();
4346
if (corpList.size() > 1) {
4447
// 先按 corpId 分组统计
45-
Map<String, List<CorpProperties>> corpsMap = corpList.stream()
46-
.collect(Collectors.groupingBy(CorpProperties::getCorpId));
47-
Set<Map.Entry<String, List<CorpProperties>>> entries = corpsMap.entrySet();
48-
for (Map.Entry<String, List<CorpProperties>> entry : entries) {
48+
Map<String, List<WxCpSingleProperties>> corpsMap = corpList.stream()
49+
.collect(Collectors.groupingBy(WxCpSingleProperties::getCorpId));
50+
Set<Map.Entry<String, List<WxCpSingleProperties>>> entries = corpsMap.entrySet();
51+
for (Map.Entry<String, List<WxCpSingleProperties>> entry : entries) {
4952
String corpId = entry.getKey();
5053
// 校验每个企业下,agentId 是否唯一
5154
boolean multi = entry.getValue().stream()
@@ -59,14 +62,14 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
5962
}
6063
WxCpMultiServicesImpl services = new WxCpMultiServicesImpl();
6164

62-
Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet();
63-
for (Map.Entry<String, CorpProperties> entry : entries) {
65+
Set<Map.Entry<String, WxCpSingleProperties>> entries = corps.entrySet();
66+
for (Map.Entry<String, WxCpSingleProperties> entry : entries) {
6467
String tenantId = entry.getKey();
65-
CorpProperties corpProperties = entry.getValue();
66-
WxCpDefaultConfigImpl storage = this.configWxCpDefaultConfigImpl(wxCpMultiProperties);
67-
this.configCorp(storage, corpProperties);
68+
WxCpSingleProperties wxCpSingleProperties = entry.getValue();
69+
WxCpDefaultConfigImpl storage = this.wxCpConfigStorage(wxCpMultiProperties);
70+
this.configCorp(storage, wxCpSingleProperties);
6871
this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
69-
WxCpService wxCpService = this.configWxCpService(storage, wxCpMultiProperties.getConfigStorage());
72+
WxCpService wxCpService = this.wxCpService(storage, wxCpMultiProperties.getConfigStorage());
7073
services.addWxCpService(tenantId, wxCpService);
7174
}
7275
return services;
@@ -78,12 +81,26 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
7881
* @param wxCpMultiProperties 参数
7982
* @return WxCpDefaultConfigImpl
8083
*/
81-
protected abstract WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties);
84+
protected abstract WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties);
8285

83-
private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
84-
WxCpService wxCpService = new WxCpServiceImpl();
86+
private WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
87+
WxCpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType();
88+
WxCpService wxCpService;
89+
switch (httpClientType) {
90+
case OK_HTTP:
91+
wxCpService = new WxCpServiceOkHttpImpl();
92+
break;
93+
case JODD_HTTP:
94+
wxCpService = new WxCpServiceJoddHttpImpl();
95+
break;
96+
case HTTP_CLIENT:
97+
wxCpService = new WxCpServiceApacheHttpClientImpl();
98+
break;
99+
default:
100+
wxCpService = new WxCpServiceImpl();
101+
break;
102+
}
85103
wxCpService.setWxCpConfigStorage(wxCpConfigStorage);
86-
87104
int maxRetryTimes = storage.getMaxRetryTimes();
88105
if (maxRetryTimes < 0) {
89106
maxRetryTimes = 0;
@@ -97,15 +114,15 @@ private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpM
97114
return wxCpService;
98115
}
99116

100-
private void configCorp(WxCpDefaultConfigImpl config, CorpProperties corpProperties) {
101-
String corpId = corpProperties.getCorpId();
102-
String corpSecret = corpProperties.getCorpSecret();
103-
Integer agentId = corpProperties.getAgentId();
104-
String token = corpProperties.getToken();
105-
String aesKey = corpProperties.getAesKey();
117+
private void configCorp(WxCpDefaultConfigImpl config, WxCpSingleProperties wxCpSingleProperties) {
118+
String corpId = wxCpSingleProperties.getCorpId();
119+
String corpSecret = wxCpSingleProperties.getCorpSecret();
120+
Integer agentId = wxCpSingleProperties.getAgentId();
121+
String token = wxCpSingleProperties.getToken();
122+
String aesKey = wxCpSingleProperties.getAesKey();
106123
// 企业微信,私钥,会话存档路径
107-
String msgAuditPriKey = corpProperties.getMsgAuditPriKey();
108-
String msgAuditLibPath = corpProperties.getMsgAuditLibPath();
124+
String msgAuditPriKey = wxCpSingleProperties.getMsgAuditPriKey();
125+
String msgAuditLibPath = wxCpSingleProperties.getMsgAuditLibPath();
109126

110127
config.setCorpId(corpId);
111128
config.setCorpSecret(corpSecret);
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
22

33
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
44
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties;
@@ -31,11 +31,11 @@ public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {
3131

3232
@Bean
3333
public WxCpMultiServices wxCpMultiServices() {
34-
return this.configWxCpServices(wxCpMultiProperties);
34+
return this.wxCpMultiServices(wxCpMultiProperties);
3535
}
3636

3737
@Override
38-
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
38+
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
3939
return this.configRedis(wxCpMultiProperties);
4040
}
4141

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
22

33
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
44
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
@@ -24,11 +24,11 @@ public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {
2424

2525
@Bean
2626
public WxCpMultiServices wxCpMultiServices() {
27-
return this.configWxCpServices(wxCpMultiProperties);
27+
return this.wxCpMultiServices(wxCpMultiProperties);
2828
}
2929

3030
@Override
31-
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
31+
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
3232
return this.configInMemory();
3333
}
3434

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
22

33
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
44
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
@@ -28,11 +28,11 @@ public class WxCpInRedisTemplateConfiguration extends AbstractWxCpConfiguration
2828

2929
@Bean
3030
public WxCpMultiServices wxCpMultiServices() {
31-
return this.configWxCpServices(wxCpMultiProperties);
31+
return this.wxCpMultiServices(wxCpMultiProperties);
3232
}
3333

3434
@Override
35-
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
35+
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
3636
return this.configRedisTemplate(wxCpMultiProperties);
3737
}
3838

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services;
1+
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
22

33
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
44
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiRedisProperties;
@@ -33,11 +33,11 @@ public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {
3333

3434
@Bean
3535
public WxCpMultiServices wxCpMultiServices() {
36-
return this.configWxCpServices(wxCpMultiProperties);
36+
return this.wxCpMultiServices(wxCpMultiProperties);
3737
}
3838

3939
@Override
40-
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
40+
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
4141
return this.configRedisson(wxCpMultiProperties);
4242
}
4343

spring-boot-starters/wx-java-cp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpMultiProperties.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818
@Data
1919
@NoArgsConstructor
2020
@ConfigurationProperties(prefix = WxCpMultiProperties.PREFIX)
21-
public class WxCpMultiProperties {
21+
public class WxCpMultiProperties implements Serializable {
22+
private static final long serialVersionUID = -1569510477055668503L;
2223
public static final String PREFIX = "wx.cp";
2324

24-
private Map<String, CorpProperties> corps = new HashMap<>();
25+
private Map<String, WxCpSingleProperties> corps = new HashMap<>();
2526

2627
/**
2728
* 配置存储策略,默认内存
@@ -48,6 +49,11 @@ public static class ConfigStorage implements Serializable {
4849
@NestedConfigurationProperty
4950
private WxCpMultiRedisProperties redis = new WxCpMultiRedisProperties();
5051

52+
/**
53+
* http客户端类型.
54+
*/
55+
private HttpClientType httpClientType = HttpClientType.HTTP_CLIENT;
56+
5157
/**
5258
* http代理主机
5359
*/
@@ -105,4 +111,19 @@ public enum StorageType {
105111
*/
106112
redistemplate
107113
}
114+
115+
public enum HttpClientType {
116+
/**
117+
* HttpClient
118+
*/
119+
HTTP_CLIENT,
120+
/**
121+
* OkHttp
122+
*/
123+
OK_HTTP,
124+
/**
125+
* JoddHttp
126+
*/
127+
JODD_HTTP
128+
}
108129
}

spring-boot-starters/wx-java-cp-multi-spring-boot-starter/src/main/java/com/binarywang/spring/starter/wxjava/cp/properties/WxCpMultiRedisProperties.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.binarywang.spring.starter.wxjava.cp.properties;
22

33
import lombok.Data;
4+
import lombok.NoArgsConstructor;
45

56
import java.io.Serializable;
67

@@ -11,6 +12,7 @@
1112
* created on 2023/10/16
1213
*/
1314
@Data
15+
@NoArgsConstructor
1416
public class WxCpMultiRedisProperties implements Serializable {
1517
private static final long serialVersionUID = -5924815351660074401L;
1618

0 commit comments

Comments
 (0)
0