Skip to content

Commit

Permalink
🎨 #3156 【企业微信】优化企业微信应用参数未配置时的初始化逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
foreveryang321 authored Nov 7, 2023
1 parent 179d469 commit b569ab8
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 ...
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
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;
Expand All @@ -24,13 +25,14 @@
* 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 缓存 token、ticket 时错乱。
Expand All @@ -55,6 +57,7 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
}
}
}
WxCpMultiServicesImpl services = new WxCpMultiServicesImpl();

Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet();
for (Map.Entry<String, CorpProperties> entry : entries) {
Expand All @@ -64,9 +67,9 @@ protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProp
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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class WxCpInJedisConfiguration extends AbstractWxCpConfiguration {
private final ApplicationContext applicationContext;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class WxCpInMemoryConfiguration extends AbstractWxCpConfiguration {
private final WxCpMultiProperties wxCpMultiProperties;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class WxCpInRedisTemplateConfiguration extends AbstractWxCpConfiguration
private final ApplicationContext applicationContext;

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class WxCpInRedissonConfiguration extends AbstractWxCpConfiguration {
private final ApplicationContext applicationContext;

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

Expand Down

0 comments on commit b569ab8

Please sign in to comment.