Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

多企业微信配置增加 http 客户端配置 #3225

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@
## ConfigStorage 配置(选填)
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
## 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-port=
wx.cp.config-storage.http-proxy-username=
@@ -57,7 +59,6 @@

```java
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.WxCpUserService;
import org.springframework.beans.factory.annotation.Autowired;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.binarywang.spring.starter.wxjava.cp.autoconfigure;

import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import com.binarywang.spring.starter.wxjava.cp.configuration.WxCpMultiServicesAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@@ -12,9 +11,6 @@
* created on 2023/10/16
*/
@Configuration
@EnableConfigurationProperties(WxCpMultiProperties.class)
@Import({
WxCpMultiServicesAutoConfiguration.class
})
@Import(WxCpMultiServicesAutoConfiguration.class)
public class WxCpMultiAutoConfiguration {
}
Original file line number Diff line number Diff line change
@@ -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.autoconfigure.services.WxCpInMemoryConfiguration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedisTemplateConfiguration;
import com.binarywang.spring.starter.wxjava.cp.autoconfigure.services.WxCpInRedissonConfiguration;
import lombok.RequiredArgsConstructor;
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInJedisConfiguration;
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInMemoryConfiguration;
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedisTemplateConfiguration;
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpInRedissonConfiguration;
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.Import;

@@ -15,7 +16,7 @@
* created on 2023/10/16
*/
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(WxCpMultiProperties.class)
@Import({
WxCpInJedisConfiguration.class,
WxCpInMemoryConfiguration.class,
Original file line number Diff line number Diff line change
@@ -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.WxCpSingleProperties;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices;
import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServicesImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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.WxCpServiceJoddHttpImpl;
import me.chanjar.weixin.cp.api.impl.WxCpServiceOkHttpImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
@@ -28,8 +31,8 @@
@Slf4j
public abstract class AbstractWxCpConfiguration {

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

Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet();
for (Map.Entry<String, CorpProperties> entry : entries) {
Set<Map.Entry<String, WxCpSingleProperties>> entries = corps.entrySet();
for (Map.Entry<String, WxCpSingleProperties> entry : entries) {
String tenantId = entry.getKey();
CorpProperties corpProperties = entry.getValue();
WxCpDefaultConfigImpl storage = this.configWxCpDefaultConfigImpl(wxCpMultiProperties);
this.configCorp(storage, corpProperties);
WxCpSingleProperties wxCpSingleProperties = entry.getValue();
WxCpDefaultConfigImpl storage = this.wxCpConfigStorage(wxCpMultiProperties);
this.configCorp(storage, wxCpSingleProperties);
this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
WxCpService wxCpService = this.configWxCpService(storage, wxCpMultiProperties.getConfigStorage());
WxCpService wxCpService = this.wxCpService(storage, wxCpMultiProperties.getConfigStorage());
services.addWxCpService(tenantId, wxCpService);
}
return services;
@@ -78,12 +81,26 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
* @param wxCpMultiProperties 参数
* @return WxCpDefaultConfigImpl
*/
protected abstract WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties);
protected abstract WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties);

private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
WxCpService wxCpService = new WxCpServiceImpl();
private WxCpService wxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) {
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);

int maxRetryTimes = storage.getMaxRetryTimes();
if (maxRetryTimes < 0) {
maxRetryTimes = 0;
@@ -97,15 +114,15 @@ private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpM
return wxCpService;
}

private void configCorp(WxCpDefaultConfigImpl config, CorpProperties corpProperties) {
String corpId = corpProperties.getCorpId();
String corpSecret = corpProperties.getCorpSecret();
Integer agentId = corpProperties.getAgentId();
String token = corpProperties.getToken();
String aesKey = corpProperties.getAesKey();
private void configCorp(WxCpDefaultConfigImpl config, WxCpSingleProperties wxCpSingleProperties) {
String corpId = wxCpSingleProperties.getCorpId();
String corpSecret = wxCpSingleProperties.getCorpSecret();
Integer agentId = wxCpSingleProperties.getAgentId();
String token = wxCpSingleProperties.getToken();
String aesKey = wxCpSingleProperties.getAesKey();
// 企业微信,私钥,会话存档路径
String msgAuditPriKey = corpProperties.getMsgAuditPriKey();
String msgAuditLibPath = corpProperties.getMsgAuditLibPath();
String msgAuditPriKey = wxCpSingleProperties.getMsgAuditPriKey();
String msgAuditLibPath = wxCpSingleProperties.getMsgAuditLibPath();

config.setCorpId(corpId);
config.setCorpSecret(corpSecret);
Original file line number Diff line number Diff line change
@@ -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.WxCpMultiRedisProperties;
@@ -31,11 +31,11 @@ public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {

@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
return this.wxCpMultiServices(wxCpMultiProperties);
}

@Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedis(wxCpMultiProperties);
}

Original file line number Diff line number Diff line change
@@ -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.service.WxCpMultiServices;
@@ -24,11 +24,11 @@ public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {

@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
return this.wxCpMultiServices(wxCpMultiProperties);
}

@Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configInMemory();
}

Original file line number Diff line number Diff line change
@@ -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.service.WxCpMultiServices;
@@ -28,11 +28,11 @@ public class WxCpInRedisTemplateConfiguration extends AbstractWxCpConfiguration

@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
return this.wxCpMultiServices(wxCpMultiProperties);
}

@Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedisTemplate(wxCpMultiProperties);
}

Original file line number Diff line number Diff line change
@@ -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.WxCpMultiRedisProperties;
@@ -33,11 +33,11 @@ public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {

@Bean
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
return this.wxCpMultiServices(wxCpMultiProperties);
}

@Override
protected WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties) {
protected WxCpDefaultConfigImpl wxCpConfigStorage(WxCpMultiProperties wxCpMultiProperties) {
return this.configRedisson(wxCpMultiProperties);
}

Original file line number Diff line number Diff line change
@@ -18,10 +18,11 @@
@Data
@NoArgsConstructor
@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";

private Map<String, CorpProperties> corps = new HashMap<>();
private Map<String, WxCpSingleProperties> corps = new HashMap<>();

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

/**
* http客户端类型.
*/
private HttpClientType httpClientType = HttpClientType.HTTP_CLIENT;

/**
* http代理主机
*/
@@ -105,4 +111,19 @@ public enum StorageType {
*/
redistemplate
}

public enum HttpClientType {
/**
* HttpClient
*/
HTTP_CLIENT,
/**
* OkHttp
*/
OK_HTTP,
/**
* JoddHttp
*/
JODD_HTTP
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.binarywang.spring.starter.wxjava.cp.properties;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

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

Original file line number Diff line number Diff line change
@@ -3,6 +3,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
* 企业微信企业相关配置属性
*
@@ -11,7 +13,8 @@
*/
@Data
@NoArgsConstructor
public class CorpProperties {
public class WxCpSingleProperties implements Serializable {
private static final long serialVersionUID = -7502823825007859418L;
/**
* 微信企业号 corpId
*/