🆕 #3360 【小程序】增加支持多小程序账号的spring-boot-starter组件

This commit is contained in:
monch
2024-09-08 16:06:54 +08:00
committed by GitHub
parent 939f3eb2ff
commit 43ad965cee
21 changed files with 886 additions and 33 deletions

View File

@@ -61,7 +61,7 @@
4. 使用样例
```java
import com.binarywang.spring.starter.wxjava.mp.service.WxMpMultiServices;
import com.binarywang.spring.starter.wxjava.mp.service.WxMaMultiServices;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpUserService;
import org.springframework.beans.factory.annotation.Autowired;

View File

@@ -31,8 +31,8 @@ import java.util.stream.Collectors;
@Slf4j
public abstract class AbstractWxMpConfiguration {
protected WxMpMultiServices wxMpMultiServices(WxMpMultiProperties wxCpMultiProperties) {
Map<String, WxMpSingleProperties> appsMap = wxCpMultiProperties.getApps();
protected WxMpMultiServices wxMpMultiServices(WxMpMultiProperties wxMpMultiProperties) {
Map<String, WxMpSingleProperties> appsMap = wxMpMultiProperties.getApps();
if (appsMap == null || appsMap.isEmpty()) {
log.warn("微信公众号应用参数未配置,通过 WxMpMultiServices#getWxMpService(\"tenantId\")获取实例将返回空");
return new WxMpMultiServicesImpl();
@@ -59,12 +59,12 @@ public abstract class AbstractWxMpConfiguration {
for (Map.Entry<String, WxMpSingleProperties> entry : entries) {
String tenantId = entry.getKey();
WxMpSingleProperties wxMpSingleProperties = entry.getValue();
WxMpDefaultConfigImpl storage = this.wxMpConfigStorage(wxCpMultiProperties);
WxMpDefaultConfigImpl storage = this.wxMpConfigStorage(wxMpMultiProperties);
this.configApp(storage, wxMpSingleProperties);
this.configHttp(storage, wxCpMultiProperties.getConfigStorage());
this.configHost(storage, wxCpMultiProperties.getHosts());
WxMpService wxCpService = this.wxMpService(storage, wxCpMultiProperties);
services.addWxMpService(tenantId, wxCpService);
this.configHttp(storage, wxMpMultiProperties.getConfigStorage());
this.configHost(storage, wxMpMultiProperties.getHosts());
WxMpService wxMpService = this.wxMpService(storage, wxMpMultiProperties);
services.addWxMpService(tenantId, wxMpService);
}
return services;
}

View File

@@ -27,32 +27,32 @@ import redis.clients.jedis.JedisPoolConfig;
)
@RequiredArgsConstructor
public class WxMpInJedisConfiguration extends AbstractWxMpConfiguration {
private final WxMpMultiProperties wxCpMultiProperties;
private final WxMpMultiProperties wxMpMultiProperties;
private final ApplicationContext applicationContext;
@Bean
public WxMpMultiServices wxMpMultiServices() {
return this.wxMpMultiServices(wxCpMultiProperties);
return this.wxMpMultiServices(wxMpMultiProperties);
}
@Override
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxCpMultiProperties) {
return this.configRedis(wxCpMultiProperties);
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxMpMultiProperties) {
return this.configRedis(wxMpMultiProperties);
}
private WxMpDefaultConfigImpl configRedis(WxMpMultiProperties wxCpMultiProperties) {
WxMpMultiRedisProperties wxCpMultiRedisProperties = wxCpMultiProperties.getConfigStorage().getRedis();
private WxMpDefaultConfigImpl configRedis(WxMpMultiProperties wxMpMultiProperties) {
WxMpMultiRedisProperties wxMpMultiRedisProperties = wxMpMultiProperties.getConfigStorage().getRedis();
JedisPool jedisPool;
if (wxCpMultiRedisProperties != null && StringUtils.isNotEmpty(wxCpMultiRedisProperties.getHost())) {
jedisPool = getJedisPool(wxCpMultiProperties);
if (wxMpMultiRedisProperties != null && StringUtils.isNotEmpty(wxMpMultiRedisProperties.getHost())) {
jedisPool = getJedisPool(wxMpMultiProperties);
} else {
jedisPool = applicationContext.getBean(JedisPool.class);
}
return new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), wxCpMultiProperties.getConfigStorage().getKeyPrefix());
return new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), wxMpMultiProperties.getConfigStorage().getKeyPrefix());
}
private JedisPool getJedisPool(WxMpMultiProperties wxCpMultiProperties) {
WxMpMultiProperties.ConfigStorage storage = wxCpMultiProperties.getConfigStorage();
private JedisPool getJedisPool(WxMpMultiProperties wxMpMultiProperties) {
WxMpMultiProperties.ConfigStorage storage = wxMpMultiProperties.getConfigStorage();
WxMpMultiRedisProperties redis = storage.getRedis();
JedisPoolConfig config = new JedisPoolConfig();

View File

@@ -21,15 +21,15 @@ import org.springframework.context.annotation.Configuration;
)
@RequiredArgsConstructor
public class WxMpInMemoryConfiguration extends AbstractWxMpConfiguration {
private final WxMpMultiProperties wxCpMultiProperties;
private final WxMpMultiProperties wxMpMultiProperties;
@Bean
public WxMpMultiServices wxCpMultiServices() {
return this.wxMpMultiServices(wxCpMultiProperties);
public WxMpMultiServices wxMpMultiServices() {
return this.wxMpMultiServices(wxMpMultiProperties);
}
@Override
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxCpMultiProperties) {
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxMpMultiProperties) {
return this.configInMemory();
}

View File

@@ -28,32 +28,32 @@ import org.springframework.context.annotation.Configuration;
)
@RequiredArgsConstructor
public class WxMpInRedissonConfiguration extends AbstractWxMpConfiguration {
private final WxMpMultiProperties wxCpMultiProperties;
private final WxMpMultiProperties wxMpMultiProperties;
private final ApplicationContext applicationContext;
@Bean
public WxMpMultiServices wxMpMultiServices() {
return this.wxMpMultiServices(wxCpMultiProperties);
return this.wxMpMultiServices(wxMpMultiProperties);
}
@Override
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxCpMultiProperties) {
return this.configRedisson(wxCpMultiProperties);
protected WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxMpMultiProperties) {
return this.configRedisson(wxMpMultiProperties);
}
private WxMpDefaultConfigImpl configRedisson(WxMpMultiProperties wxCpMultiProperties) {
WxMpMultiRedisProperties redisProperties = wxCpMultiProperties.getConfigStorage().getRedis();
private WxMpDefaultConfigImpl configRedisson(WxMpMultiProperties wxMpMultiProperties) {
WxMpMultiRedisProperties redisProperties = wxMpMultiProperties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient(wxCpMultiProperties);
redissonClient = getRedissonClient(wxMpMultiProperties);
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxMpRedissonConfigImpl(redissonClient, wxCpMultiProperties.getConfigStorage().getKeyPrefix());
return new WxMpRedissonConfigImpl(redissonClient, wxMpMultiProperties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient(WxMpMultiProperties wxCpMultiProperties) {
WxMpMultiProperties.ConfigStorage storage = wxCpMultiProperties.getConfigStorage();
private RedissonClient getRedissonClient(WxMpMultiProperties wxMpMultiProperties) {
WxMpMultiProperties.ConfigStorage storage = wxMpMultiProperties.getConfigStorage();
WxMpMultiRedisProperties redis = storage.getRedis();
Config config = new Config();