|
|
@@ -9,6 +9,7 @@ import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
|
|
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
import org.springframework.context.annotation.Bean;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.context.annotation.Scope;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -39,36 +40,63 @@ public class WxMpConfiguration {
|
|
|
private final WxMpProperties properties;
|
|
|
|
|
|
@Bean
|
|
|
+ @Scope("prototype") // 每次获取新的 WxMpService 实例,避免单例缓存导致多租户问题
|
|
|
public WxMpService wxMpService() {
|
|
|
- // 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
|
|
|
+ // 从 WxMpProperties 获取当前请求租户配置
|
|
|
final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
|
|
|
- if (configs == null) {
|
|
|
- throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
|
|
|
+
|
|
|
+ if (configs == null || configs.isEmpty()) {
|
|
|
+ throw new RuntimeException("当前租户未配置微信公众号!请检查 TenantConfigContext 或 yml 配置");
|
|
|
}
|
|
|
|
|
|
WxMpService service = new WxMpServiceImpl();
|
|
|
- service.setMultiConfigStorages(configs
|
|
|
- .stream().map(a -> {
|
|
|
- WxMpDefaultConfigImpl configStorage;
|
|
|
-// if (this.properties.isUseRedis()) {
|
|
|
-// final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
|
|
|
-// JedisPoolConfig poolConfig = new JedisPoolConfig();
|
|
|
-// JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
|
|
|
-// redisConfig.getTimeout(), redisConfig.getPassword());
|
|
|
-// configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
|
|
|
-// } else {
|
|
|
-// configStorage = new WxMpDefaultConfigImpl();
|
|
|
-// }
|
|
|
- configStorage = new WxMpDefaultConfigImpl();
|
|
|
- configStorage.setAppId(a.getAppId());
|
|
|
- configStorage.setSecret(a.getSecret());
|
|
|
- configStorage.setToken(a.getToken());
|
|
|
- configStorage.setAesKey(a.getAesKey());
|
|
|
- return configStorage;
|
|
|
- }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
|
|
|
+
|
|
|
+ // 遍历 configs 构建多公众号存储
|
|
|
+ service.setMultiConfigStorages(
|
|
|
+ configs.stream().map(a -> {
|
|
|
+ WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
|
|
|
+ configStorage.setAppId(a.getAppId());
|
|
|
+ configStorage.setSecret(a.getSecret());
|
|
|
+ configStorage.setToken(a.getToken());
|
|
|
+ configStorage.setAesKey(a.getAesKey());
|
|
|
+ return configStorage;
|
|
|
+ }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o,n) -> o)
|
|
|
+ ));
|
|
|
+
|
|
|
return service;
|
|
|
}
|
|
|
|
|
|
+// @Bean
|
|
|
+// public WxMpService wxMpService() {
|
|
|
+// // 代码里 getConfigs()处报错的同学,请注意仔细阅读项目说明,你的IDE需要引入lombok插件!!!!
|
|
|
+// final List<WxMpProperties.MpConfig> configs = this.properties.getConfigs();
|
|
|
+// if (configs == null) {
|
|
|
+// throw new RuntimeException("大哥,拜托先看下项目首页的说明(readme文件),添加下相关配置,注意别配错了!");
|
|
|
+// }
|
|
|
+//
|
|
|
+// WxMpService service = new WxMpServiceImpl();
|
|
|
+// service.setMultiConfigStorages(configs
|
|
|
+// .stream().map(a -> {
|
|
|
+// WxMpDefaultConfigImpl configStorage;
|
|
|
+//// if (this.properties.isUseRedis()) {
|
|
|
+//// final WxMpProperties.RedisConfig redisConfig = this.properties.getRedisConfig();
|
|
|
+//// JedisPoolConfig poolConfig = new JedisPoolConfig();
|
|
|
+//// JedisPool jedisPool = new JedisPool(poolConfig, redisConfig.getHost(), redisConfig.getPort(),
|
|
|
+//// redisConfig.getTimeout(), redisConfig.getPassword());
|
|
|
+//// configStorage = new WxMpRedisConfigImpl(new JedisWxRedisOps(jedisPool), a.getAppId());
|
|
|
+//// } else {
|
|
|
+//// configStorage = new WxMpDefaultConfigImpl();
|
|
|
+//// }
|
|
|
+// configStorage = new WxMpDefaultConfigImpl();
|
|
|
+// configStorage.setAppId(a.getAppId());
|
|
|
+// configStorage.setSecret(a.getSecret());
|
|
|
+// configStorage.setToken(a.getToken());
|
|
|
+// configStorage.setAesKey(a.getAesKey());
|
|
|
+// return configStorage;
|
|
|
+// }).collect(Collectors.toMap(WxMpDefaultConfigImpl::getAppId, a -> a, (o, n) -> o)));
|
|
|
+// return service;
|
|
|
+// }
|
|
|
+
|
|
|
@Bean
|
|
|
public WxMpMessageRouter messageRouter(WxMpService wxMpService) {
|
|
|
final WxMpMessageRouter newRouter = new WxMpMessageRouter(wxMpService);
|