🎨 #3156 【企业微信】优化企业微信应用参数未配置时的初始化逻辑

This commit is contained in:
foreveryang321 2023-11-07 16:23:26 +08:00 committed by GitHub
parent 179d469d35
commit b569ab8cbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 8 deletions

View File

@ -80,6 +80,17 @@ public class DemoService {
WxCpUserService userService2 = wxCpService2.getUserService();
userService2.getUserId("xxx");
// todo ...
// 应用 3 的 WxCpService
WxCpService wxCpService3 = wxCpMultiServices.getWxCpService("tenantId3");
// 判断是否为空
if (wxCpService3 == null) {
// todo wxCpService3 为空,请先配置 tenantId3 企业微信应用参数
return;
}
WxCpUserService userService3 = wxCpService3.getUserService();
userService3.getUserId("xxx");
// todo ...
}
}
```

View File

@ -5,6 +5,7 @@ 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.WxCpMultiServicesImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
@ -24,13 +25,14 @@ import java.util.stream.Collectors;
* created on 2023/10/16
*/
@RequiredArgsConstructor
@Slf4j
public abstract class AbstractWxCpConfiguration {
protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProperties) {
WxCpMultiServicesImpl wxCpServices = new WxCpMultiServicesImpl();
Map<String, CorpProperties> corps = wxCpMultiProperties.getCorps();
if (corps == null || corps.isEmpty()) {
throw new RuntimeException("企业微信配置为null");
log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpService(\"tenantId\")获取实例将返回空");
return new WxCpMultiServicesImpl();
}
/**
* 校验同一个企业下agentId 是否唯一避免使用 redis 缓存 tokenticket 时错乱
@ -55,6 +57,7 @@ public abstract class AbstractWxCpConfiguration {
}
}
}
WxCpMultiServicesImpl services = new WxCpMultiServicesImpl();
Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet();
for (Map.Entry<String, CorpProperties> entry : entries) {
@ -64,9 +67,9 @@ public abstract class AbstractWxCpConfiguration {
this.configCorp(storage, corpProperties);
this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
WxCpService wxCpService = this.configWxCpService(storage, wxCpMultiProperties.getConfigStorage());
wxCpServices.addWxCpService(tenantId, wxCpService);
services.addWxCpService(tenantId, wxCpService);
}
return wxCpServices;
return services;
}
/**

View File

@ -30,7 +30,7 @@ public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {
private final ApplicationContext applicationContext;
@Bean
public WxCpMultiServices wxCpServices() {
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
}

View File

@ -23,7 +23,7 @@ public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {
private final WxCpMultiProperties wxCpMultiProperties;
@Bean
public WxCpMultiServices wxCpServices() {
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
}

View File

@ -27,7 +27,7 @@ public class WxCpInRedisTemplateConfiguration extends AbstractWxCpConfiguration
private final ApplicationContext applicationContext;
@Bean
public WxCpMultiServices wxCpServices() {
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
}

View File

@ -32,7 +32,7 @@ public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {
private final ApplicationContext applicationContext;
@Bean
public WxCpMultiServices wxCpServices() {
public WxCpMultiServices wxCpMultiServices() {
return this.configWxCpServices(wxCpMultiProperties);
}