🆕 #3703 【企业微信】第三方应用基础接口实现
This commit is contained in:
@@ -26,6 +26,7 @@
|
||||
<module>wx-java-open-spring-boot-starter</module>
|
||||
<module>wx-java-qidian-spring-boot-starter</module>
|
||||
<module>wx-java-cp-multi-spring-boot-starter</module>
|
||||
<module>wx-java-cp-tp-multi-spring-boot-starter</module>
|
||||
<module>wx-java-cp-spring-boot-starter</module>
|
||||
<module>wx-java-channel-spring-boot-starter</module>
|
||||
<module>wx-java-channel-multi-spring-boot-starter</module>
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
# wx-java-cp-multi-spring-boot-starter
|
||||
|
||||
企业微信多账号配置
|
||||
|
||||
- 实现多 WxCpService 初始化。
|
||||
- 未实现 WxCpTpService 初始化,需要的小伙伴可以参考多 WxCpService 配置的实现。
|
||||
- 未实现 WxCpCgService 初始化,需要的小伙伴可以参考多 WxCpService 配置的实现。
|
||||
|
||||
## 快速开始
|
||||
|
||||
1. 引入依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java-cp-multi-spring-boot-starter</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
```
|
||||
2. 添加配置(application.properties)
|
||||
```properties
|
||||
# 应用 1 配置
|
||||
wx.cp.corps.tenantId1.corp-id = @corp-id
|
||||
wx.cp.corps.tenantId1.corp-secret = @corp-secret
|
||||
## 选填
|
||||
wx.cp.corps.tenantId1.agent-id = @agent-id
|
||||
wx.cp.corps.tenantId1.token = @token
|
||||
wx.cp.corps.tenantId1.aes-key = @aes-key
|
||||
wx.cp.corps.tenantId1.msg-audit-priKey = @msg-audit-priKey
|
||||
wx.cp.corps.tenantId1.msg-audit-lib-path = @msg-audit-lib-path
|
||||
|
||||
# 应用 2 配置
|
||||
wx.cp.corps.tenantId2.corp-id = @corp-id
|
||||
wx.cp.corps.tenantId2.corp-secret = @corp-secret
|
||||
## 选填
|
||||
wx.cp.corps.tenantId2.agent-id = @agent-id
|
||||
wx.cp.corps.tenantId2.token = @token
|
||||
wx.cp.corps.tenantId2.aes-key = @aes-key
|
||||
wx.cp.corps.tenantId2.msg-audit-priKey = @msg-audit-priKey
|
||||
wx.cp.corps.tenantId2.msg-audit-lib-path = @msg-audit-lib-path
|
||||
|
||||
# 公共配置
|
||||
## ConfigStorage 配置(选填)
|
||||
wx.cp.config-storage.type=memory # 配置类型: memory(默认), jedis, redisson, redistemplate
|
||||
## http 客户端配置(选填)
|
||||
## # http客户端类型: http_client(默认), ok_http, jodd_http
|
||||
wx.cp.config-storage.http-client-type=http_client
|
||||
wx.cp.config-storage.http-proxy-host=
|
||||
wx.cp.config-storage.http-proxy-port=
|
||||
wx.cp.config-storage.http-proxy-username=
|
||||
wx.cp.config-storage.http-proxy-password=
|
||||
## 最大重试次数,默认:5 次,如果小于 0,则为 0
|
||||
wx.cp.config-storage.max-retry-times=5
|
||||
## 重试时间间隔步进,默认:1000 毫秒,如果小于 0,则为 1000
|
||||
wx.cp.config-storage.retry-sleep-millis=1000
|
||||
```
|
||||
3. 支持自动注入的类型: `WxCpMultiServices`
|
||||
|
||||
4. 使用样例
|
||||
|
||||
```java
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import me.chanjar.weixin.cp.api.WxCpService;
|
||||
import me.chanjar.weixin.cp.api.WxCpUserService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
@Service
|
||||
public class DemoService {
|
||||
@Autowired
|
||||
private WxCpTpMultiServices wxCpTpMultiServices;
|
||||
|
||||
public void test() {
|
||||
// 应用 1 的 WxCpService
|
||||
WxCpService wxCpService1 = wxCpMultiServices.getWxCpService("tenantId1");
|
||||
WxCpUserService userService1 = wxCpService1.getUserService();
|
||||
userService1.getUserId("xxx");
|
||||
// todo ...
|
||||
|
||||
// 应用 2 的 WxCpService
|
||||
WxCpService wxCpService2 = wxCpMultiServices.getWxCpService("tenantId2");
|
||||
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 ...
|
||||
}
|
||||
}
|
||||
```
|
||||
@@ -0,0 +1,60 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>wx-java-spring-boot-starters</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>4.7.7.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>wx-java-cp-tp-multi-spring-boot-starter</artifactId>
|
||||
<name>WxJava - Spring Boot Starter for WxCp::支持多账号配置</name>
|
||||
<description>微信企业号开发的 Spring Boot Starter::支持多账号配置</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>weixin-java-cp</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.redisson</groupId>
|
||||
<artifactId>redisson</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.data</groupId>
|
||||
<artifactId>spring-data-redis</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
<version>${spring.boot.version}</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>2.2.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.autoconfigure;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.configuration.WxCpTpMultiServicesAutoConfiguration;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 企业微信自动注册
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@Import(WxCpTpMultiServicesAutoConfiguration.class)
|
||||
public class WxCpTpMultiAutoConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpTpInJedisTpConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpTpInMemoryTpConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpTpInRedisTemplateTpConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.configuration.services.WxCpTpInRedissonTpConfiguration;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* 企业微信平台相关服务自动注册
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WxCpTpMultiProperties.class)
|
||||
@Import({
|
||||
WxCpTpInJedisTpConfiguration.class,
|
||||
WxCpTpInMemoryTpConfiguration.class,
|
||||
WxCpTpInRedissonTpConfiguration.class,
|
||||
WxCpTpInRedisTemplateTpConfiguration.class
|
||||
})
|
||||
public class WxCpTpMultiServicesAutoConfiguration {
|
||||
}
|
||||
@@ -0,0 +1,139 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpSingleProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServicesImpl;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceApacheHttpClientImpl;
|
||||
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceImpl;
|
||||
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceJoddHttpImpl;
|
||||
import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceOkHttpImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* WxCpConfigStorage 抽象配置类
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
public abstract class AbstractWxCpTpConfiguration {
|
||||
|
||||
/**
|
||||
*
|
||||
* @param wxCpTpMultiProperties 应用列表配置
|
||||
* @param services 用于支持,应用启动之后,可以调用这个接口添加新服务对象。主要是配置是从数据库中读取的
|
||||
* @return
|
||||
*/
|
||||
public WxCpTpMultiServices wxCpMultiServices(WxCpTpMultiProperties wxCpTpMultiProperties,WxCpTpMultiServices services) {
|
||||
Map<String, WxCpTpSingleProperties> corps = wxCpTpMultiProperties.getCorps();
|
||||
if (corps == null || corps.isEmpty()) {
|
||||
log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpTpService(\"tenantId\")获取实例将返回空");
|
||||
return new WxCpTpMultiServicesImpl();
|
||||
}
|
||||
|
||||
if (services == null) {
|
||||
services = new WxCpTpMultiServicesImpl();
|
||||
}
|
||||
|
||||
Set<Map.Entry<String, WxCpTpSingleProperties>> entries = corps.entrySet();
|
||||
for (Map.Entry<String, WxCpTpSingleProperties> entry : entries) {
|
||||
String tenantId = entry.getKey();
|
||||
WxCpTpSingleProperties wxCpTpSingleProperties = entry.getValue();
|
||||
WxCpTpDefaultConfigImpl storage = this.wxCpTpConfigStorage(wxCpTpMultiProperties);
|
||||
this.configCorp(storage, wxCpTpSingleProperties);
|
||||
this.configHttp(storage, wxCpTpMultiProperties.getConfigStorage());
|
||||
WxCpTpService wxCpTpService = this.wxCpTpService(storage, wxCpTpMultiProperties.getConfigStorage());
|
||||
if (services.getWxCpTpService(tenantId) == null) {
|
||||
// 不存在的才会添加到服务列表中
|
||||
services.addWxCpTpService(tenantId, wxCpTpService);
|
||||
}
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置 WxCpDefaultConfigImpl
|
||||
*
|
||||
* @param wxCpTpMultiProperties 参数
|
||||
* @return WxCpDefaultConfigImpl
|
||||
*/
|
||||
protected abstract WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties);
|
||||
|
||||
private WxCpTpService wxCpTpService(WxCpTpConfigStorage wxCpTpConfigStorage, WxCpTpMultiProperties.ConfigStorage storage) {
|
||||
WxCpTpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType();
|
||||
WxCpTpService cpTpService;
|
||||
switch (httpClientType) {
|
||||
case OK_HTTP:
|
||||
cpTpService = new WxCpTpServiceOkHttpImpl();
|
||||
break;
|
||||
case JODD_HTTP:
|
||||
cpTpService = new WxCpTpServiceJoddHttpImpl();
|
||||
break;
|
||||
case HTTP_CLIENT:
|
||||
cpTpService = new WxCpTpServiceApacheHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
cpTpService = new WxCpTpServiceImpl();
|
||||
break;
|
||||
}
|
||||
cpTpService.setWxCpTpConfigStorage(wxCpTpConfigStorage);
|
||||
int maxRetryTimes = storage.getMaxRetryTimes();
|
||||
if (maxRetryTimes < 0) {
|
||||
maxRetryTimes = 0;
|
||||
}
|
||||
int retrySleepMillis = storage.getRetrySleepMillis();
|
||||
if (retrySleepMillis < 0) {
|
||||
retrySleepMillis = 1000;
|
||||
}
|
||||
cpTpService.setRetrySleepMillis(retrySleepMillis);
|
||||
cpTpService.setMaxRetryTimes(maxRetryTimes);
|
||||
return cpTpService;
|
||||
}
|
||||
|
||||
private void configCorp(WxCpTpDefaultConfigImpl config, WxCpTpSingleProperties wxCpTpSingleProperties) {
|
||||
String corpId = wxCpTpSingleProperties.getCorpId();
|
||||
String providerSecret = wxCpTpSingleProperties.getProviderSecret();
|
||||
String suiteId = wxCpTpSingleProperties.getSuiteId();
|
||||
String token = wxCpTpSingleProperties.getToken();
|
||||
String suiteSecret = wxCpTpSingleProperties.getSuiteSecret();
|
||||
// 企业微信,私钥,会话存档路径
|
||||
config.setCorpId(corpId);
|
||||
config.setProviderSecret(providerSecret);
|
||||
config.setEncodingAESKey(wxCpTpSingleProperties.getEncodingAESKey());
|
||||
config.setSuiteId(suiteId);
|
||||
config.setToken(token);
|
||||
config.setSuiteSecret(suiteSecret);
|
||||
}
|
||||
|
||||
private void configHttp(WxCpTpDefaultConfigImpl config, WxCpTpMultiProperties.ConfigStorage storage) {
|
||||
String httpProxyHost = storage.getHttpProxyHost();
|
||||
Integer httpProxyPort = storage.getHttpProxyPort();
|
||||
String httpProxyUsername = storage.getHttpProxyUsername();
|
||||
String httpProxyPassword = storage.getHttpProxyPassword();
|
||||
if (StringUtils.isNotBlank(httpProxyHost)) {
|
||||
config.setHttpProxyHost(httpProxyHost);
|
||||
if (httpProxyPort != null) {
|
||||
config.setHttpProxyPort(httpProxyPort);
|
||||
}
|
||||
if (StringUtils.isNotBlank(httpProxyUsername)) {
|
||||
config.setHttpProxyUsername(httpProxyUsername);
|
||||
}
|
||||
if (StringUtils.isNotBlank(httpProxyPassword)) {
|
||||
config.setHttpProxyPassword(httpProxyPassword);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpJedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* 自动装配基于 jedis 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpTpMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "jedis"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpTpInJedisTpConfiguration extends AbstractWxCpTpConfiguration {
|
||||
private final WxCpTpMultiProperties wxCpTpMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxCpTpMultiServices wxCpMultiServices() {
|
||||
return this.wxCpMultiServices(wxCpTpMultiProperties,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
return this.configRedis(wxCpTpMultiProperties);
|
||||
}
|
||||
|
||||
private WxCpTpDefaultConfigImpl configRedis(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
WxCpTpMultiRedisProperties wxCpTpMultiRedisProperties = wxCpTpMultiProperties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (wxCpTpMultiRedisProperties != null && StringUtils.isNotEmpty(wxCpTpMultiRedisProperties.getHost())) {
|
||||
jedisPool = getJedisPool(wxCpTpMultiProperties);
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
return new WxCpTpJedisConfigImpl(jedisPool, wxCpTpMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
WxCpTpMultiProperties.ConfigStorage storage = wxCpTpMultiProperties.getConfigStorage();
|
||||
WxCpTpMultiRedisProperties redis = storage.getRedis();
|
||||
|
||||
JedisPoolConfig config = new JedisPoolConfig();
|
||||
if (redis.getMaxActive() != null) {
|
||||
config.setMaxTotal(redis.getMaxActive());
|
||||
}
|
||||
if (redis.getMaxIdle() != null) {
|
||||
config.setMaxIdle(redis.getMaxIdle());
|
||||
}
|
||||
if (redis.getMaxWaitMillis() != null) {
|
||||
config.setMaxWaitMillis(redis.getMaxWaitMillis());
|
||||
}
|
||||
if (redis.getMinIdle() != null) {
|
||||
config.setMinIdle(redis.getMinIdle());
|
||||
}
|
||||
config.setTestOnBorrow(true);
|
||||
config.setTestWhileIdle(true);
|
||||
|
||||
return new JedisPool(config, redis.getHost(), redis.getPort(),
|
||||
redis.getTimeout(), redis.getPassword(), redis.getDatabase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于内存策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpTpMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "memory", matchIfMissing = true
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpTpInMemoryTpConfiguration extends AbstractWxCpTpConfiguration {
|
||||
private final WxCpTpMultiProperties wxCpTpMultiProperties;
|
||||
|
||||
@Bean
|
||||
public WxCpTpMultiServices wxCpMultiServices() {
|
||||
return this.wxCpMultiServices(wxCpTpMultiProperties,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
return this.configInMemory();
|
||||
}
|
||||
|
||||
private WxCpTpDefaultConfigImpl configInMemory() {
|
||||
return new WxCpTpDefaultConfigImpl();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpRedisTemplateConfigImpl;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisTemplate 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpTpMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpTpInRedisTemplateTpConfiguration extends AbstractWxCpTpConfiguration {
|
||||
private final WxCpTpMultiProperties wxCpTpMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxCpTpMultiServices wxCpMultiServices() {
|
||||
return this.wxCpMultiServices(wxCpTpMultiProperties,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
return this.configRedisTemplate(wxCpTpMultiProperties);
|
||||
}
|
||||
|
||||
private WxCpTpDefaultConfigImpl configRedisTemplate(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
return new WxCpTpRedisTemplateConfigImpl(redisTemplate, wxCpTpMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.configuration.services;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiRedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.AbstractWxCpTpInRedisConfigImpl;
|
||||
import me.chanjar.weixin.cp.config.impl.WxCpTpRedissonConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 自动装配基于 redisson 策略配置
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Configuration
|
||||
@ConditionalOnProperty(
|
||||
prefix = WxCpTpMultiProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxCpTpInRedissonTpConfiguration extends AbstractWxCpTpConfiguration {
|
||||
private final WxCpTpMultiProperties wxCpTpMultiProperties;
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
@Bean
|
||||
public WxCpTpMultiServices wxCpMultiServices() {
|
||||
return this.wxCpMultiServices(wxCpTpMultiProperties,null);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
return this.configRedisson(wxCpTpMultiProperties);
|
||||
}
|
||||
|
||||
private WxCpTpDefaultConfigImpl configRedisson(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
WxCpTpMultiRedisProperties redisProperties = wxCpTpMultiProperties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient(wxCpTpMultiProperties);
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxCpTpRedissonConfigImpl(redissonClient, wxCpTpMultiProperties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient(WxCpTpMultiProperties wxCpTpMultiProperties) {
|
||||
WxCpTpMultiProperties.ConfigStorage storage = wxCpTpMultiProperties.getConfigStorage();
|
||||
WxCpTpMultiRedisProperties redis = storage.getRedis();
|
||||
|
||||
Config config = new Config();
|
||||
config.useSingleServer()
|
||||
.setAddress("redis://" + redis.getHost() + ":" + redis.getPort())
|
||||
.setDatabase(redis.getDatabase())
|
||||
.setPassword(redis.getPassword());
|
||||
config.setTransportMode(TransportMode.NIO);
|
||||
return Redisson.create(config);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,129 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.NestedConfigurationProperty;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 企业微信多企业接入相关配置属性
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@ConfigurationProperties(prefix = WxCpTpMultiProperties.PREFIX)
|
||||
public class WxCpTpMultiProperties implements Serializable {
|
||||
private static final long serialVersionUID = -1569510477055668503L;
|
||||
public static final String PREFIX = "wx.cp.tp";
|
||||
|
||||
private Map<String, WxCpTpSingleProperties> corps = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 配置存储策略,默认内存
|
||||
*/
|
||||
private ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public static class ConfigStorage implements Serializable {
|
||||
private static final long serialVersionUID = 4815731027000065434L;
|
||||
/**
|
||||
* 存储类型
|
||||
*/
|
||||
private StorageType type = StorageType.memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀
|
||||
*/
|
||||
private String keyPrefix = "wx:cp:tp";
|
||||
|
||||
/**
|
||||
* redis连接配置
|
||||
*/
|
||||
@NestedConfigurationProperty
|
||||
private WxCpTpMultiRedisProperties redis = new WxCpTpMultiRedisProperties();
|
||||
|
||||
/**
|
||||
* http客户端类型.
|
||||
*/
|
||||
private HttpClientType httpClientType = HttpClientType.HTTP_CLIENT;
|
||||
|
||||
/**
|
||||
* http代理主机
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.cp.api.WxCpService#setMaxRetryTimes(int)}
|
||||
* {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
* <pre>
|
||||
* {@link me.chanjar.weixin.cp.api.WxCpService#setRetrySleepMillis(int)}
|
||||
* {@link me.chanjar.weixin.cp.api.impl.BaseWxCpServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int retrySleepMillis = 1000;
|
||||
}
|
||||
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存
|
||||
*/
|
||||
memory,
|
||||
/**
|
||||
* jedis
|
||||
*/
|
||||
jedis,
|
||||
/**
|
||||
* redisson
|
||||
*/
|
||||
redisson,
|
||||
/**
|
||||
* redistemplate
|
||||
*/
|
||||
redistemplate
|
||||
}
|
||||
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient
|
||||
*/
|
||||
HTTP_CLIENT,
|
||||
/**
|
||||
* OkHttp
|
||||
*/
|
||||
OK_HTTP,
|
||||
/**
|
||||
* JoddHttp
|
||||
*/
|
||||
JODD_HTTP
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* Redis配置.
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxCpTpMultiRedisProperties implements Serializable {
|
||||
private static final long serialVersionUID = -5924815351660074401L;
|
||||
|
||||
/**
|
||||
* 主机地址.
|
||||
*/
|
||||
private String host;
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
*/
|
||||
private int port = 6379;
|
||||
|
||||
/**
|
||||
* 密码.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 超时.
|
||||
*/
|
||||
private int timeout = 2000;
|
||||
|
||||
/**
|
||||
* 数据库.
|
||||
*/
|
||||
private int database = 0;
|
||||
|
||||
private Integer maxActive;
|
||||
private Integer maxIdle;
|
||||
private Integer maxWaitMillis;
|
||||
private Integer minIdle;
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 企业微信企业相关配置属性
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
public class WxCpTpSingleProperties implements Serializable {
|
||||
private static final long serialVersionUID = -7502823825007859418L;
|
||||
/**
|
||||
* 微信企业号 corpId
|
||||
*/
|
||||
private String corpId;
|
||||
/**
|
||||
* 微信企业号 服务商 providerSecret
|
||||
*/
|
||||
private String providerSecret;
|
||||
/**
|
||||
* 微信企业号应用 token
|
||||
*/
|
||||
private String token;
|
||||
|
||||
private String encodingAESKey;
|
||||
|
||||
/**
|
||||
* 微信企业号 第三方 应用 ID
|
||||
*/
|
||||
private String suiteId;
|
||||
/**
|
||||
* 微信企业号应用
|
||||
*/
|
||||
private String suiteSecret;
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.service;
|
||||
|
||||
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
|
||||
/**
|
||||
* 企业微信 {@link WxCpTpService} 所有实例存放类.
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
public interface WxCpTpMultiServices {
|
||||
/**
|
||||
* 通过租户 Id 获取 WxCpTpService
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @return WxCpTpService
|
||||
*/
|
||||
WxCpTpService getWxCpTpService(String tenantId);
|
||||
|
||||
void addWxCpTpService(String tenantId, WxCpTpService wxCpService);
|
||||
|
||||
/**
|
||||
* 根据租户 Id,从列表中移除一个 WxCpTpService 实例
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
*/
|
||||
void removeWxCpTpService(String tenantId);
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.binarywang.spring.starter.wxjava.cp.service;
|
||||
|
||||
|
||||
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
/**
|
||||
* 企业微信 {@link WxCpTpMultiServices} 默认实现
|
||||
*
|
||||
* @author yl
|
||||
* created on 2023/10/16
|
||||
*/
|
||||
public class WxCpTpMultiServicesImpl implements WxCpTpMultiServices {
|
||||
private final Map<String, WxCpTpService> services = new ConcurrentHashMap<>();
|
||||
|
||||
/**
|
||||
* 通过租户 Id 获取 WxCpTpService
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @return WxCpTpService
|
||||
*/
|
||||
@Override
|
||||
public WxCpTpService getWxCpTpService(String tenantId) {
|
||||
return this.services.get(tenantId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据租户 Id,添加一个 WxCpTpService 到列表
|
||||
*
|
||||
* @param tenantId 租户 Id
|
||||
* @param wxCpService WxCpTpService 实例
|
||||
*/
|
||||
@Override
|
||||
public void addWxCpTpService(String tenantId, WxCpTpService wxCpService) {
|
||||
this.services.put(tenantId, wxCpService);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeWxCpTpService(String tenantId) {
|
||||
this.services.remove(tenantId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.binarywang.spring.starter.wxjava.cp.autoconfigure.WxCpTpMultiAutoConfiguration
|
||||
@@ -0,0 +1 @@
|
||||
com.binarywang.spring.starter.wxjava.cp.autoconfigure.WxCpTpMultiAutoConfiguration
|
||||
Reference in New Issue
Block a user