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

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

Merged
Show file tree
Hide file tree
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
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