🆕 #3217 增加 solon-plugins 适配
This commit is contained in:
46
solon-plugins/wx-java-mp-solon-plugin/README.md
Normal file
46
solon-plugins/wx-java-mp-solon-plugin/README.md
Normal file
@@ -0,0 +1,46 @@
|
||||
# wx-java-mp-solon-plugin
|
||||
|
||||
## 快速开始
|
||||
|
||||
1. 引入依赖
|
||||
```xml
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>wx-java-mp-solon-plugin</artifactId>
|
||||
<version>${version}</version>
|
||||
</dependency>
|
||||
```
|
||||
2. 添加配置(app.properties)
|
||||
```properties
|
||||
# 公众号配置(必填)
|
||||
wx.mp.app-id=appId
|
||||
wx.mp.secret=@secret
|
||||
wx.mp.token=@token
|
||||
wx.mp.aes-key=@aesKey
|
||||
wx.mp.use-stable-access-token=@useStableAccessToken
|
||||
# 存储配置redis(可选)
|
||||
wx.mp.config-storage.type= edis # 配置类型: Memory(默认), Jedis, RedisTemplate
|
||||
wx.mp.config-storage.key-prefix=wx # 相关redis前缀配置: wx(默认)
|
||||
wx.mp.config-storage.redis.host=127.0.0.1
|
||||
wx.mp.config-storage.redis.port=6379
|
||||
#单机和sentinel同时存在时,优先使用sentinel配置
|
||||
#wx.mp.config-storage.redis.sentinel-ips=127.0.0.1:16379,127.0.0.1:26379
|
||||
#wx.mp.config-storage.redis.sentinel-name=mymaster
|
||||
# http客户端配置
|
||||
wx.mp.config-storage.http-client-type=httpclient # http客户端类型: HttpClient(默认), OkHttp, JoddHttp
|
||||
wx.mp.config-storage.http-proxy-host=
|
||||
wx.mp.config-storage.http-proxy-port=
|
||||
wx.mp.config-storage.http-proxy-username=
|
||||
wx.mp.config-storage.http-proxy-password=
|
||||
# 公众号地址host配置
|
||||
#wx.mp.hosts.api-host=http://proxy.com/
|
||||
#wx.mp.hosts.open-host=http://proxy.com/
|
||||
#wx.mp.hosts.mp-host=http://proxy.com/
|
||||
```
|
||||
3. 自动注入的类型
|
||||
|
||||
- `WxMpService`
|
||||
- `WxMpConfigStorage`
|
||||
|
||||
4、参考demo:
|
||||
https://github.com/binarywang/wx-java-mp-demo
|
||||
39
solon-plugins/wx-java-mp-solon-plugin/pom.xml
Normal file
39
solon-plugins/wx-java-mp-solon-plugin/pom.xml
Normal file
@@ -0,0 +1,39 @@
|
||||
<?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-solon-plugins</artifactId>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<version>4.6.4.B</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>wx-java-mp-solon-plugin</artifactId>
|
||||
<name>WxJava - Solon Plugin for MP</name>
|
||||
<description>微信公众号开发的 Solon Plugin</description>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>com.github.binarywang</groupId>
|
||||
<artifactId>weixin-java-mp</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>redis.clients</groupId>
|
||||
<artifactId>jedis</artifactId>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.jodd</groupId>
|
||||
<artifactId>jodd-http</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.squareup.okhttp3</groupId>
|
||||
<artifactId>okhttp</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.binarywang.solon.wxjava.mp.config;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import me.chanjar.weixin.mp.api.WxMpService;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl;
|
||||
import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 微信公众号相关服务自动注册.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Configuration
|
||||
public class WxMpServiceAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean = WxMpService.class)
|
||||
public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpProperties wxMpProperties) {
|
||||
HttpClientType httpClientType = wxMpProperties.getConfigStorage().getHttpClientType();
|
||||
WxMpService wxMpService;
|
||||
switch (httpClientType) {
|
||||
case OkHttp:
|
||||
wxMpService = newWxMpServiceOkHttpImpl();
|
||||
break;
|
||||
case JoddHttp:
|
||||
wxMpService = newWxMpServiceJoddHttpImpl();
|
||||
break;
|
||||
case HttpClient:
|
||||
wxMpService = newWxMpServiceHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
wxMpService = newWxMpServiceImpl();
|
||||
break;
|
||||
}
|
||||
|
||||
wxMpService.setWxMpConfigStorage(configStorage);
|
||||
return wxMpService;
|
||||
}
|
||||
|
||||
private WxMpService newWxMpServiceImpl() {
|
||||
return new WxMpServiceImpl();
|
||||
}
|
||||
|
||||
private WxMpService newWxMpServiceHttpClientImpl() {
|
||||
return new WxMpServiceHttpClientImpl();
|
||||
}
|
||||
|
||||
private WxMpService newWxMpServiceOkHttpImpl() {
|
||||
return new WxMpServiceOkHttpImpl();
|
||||
}
|
||||
|
||||
private WxMpService newWxMpServiceJoddHttpImpl() {
|
||||
return new WxMpServiceJoddHttpImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package com.binarywang.solon.wxjava.mp.config;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.enums.StorageType;
|
||||
import com.binarywang.solon.wxjava.mp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import com.google.common.collect.Sets;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import me.chanjar.weixin.mp.config.WxMpHostConfig;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
|
||||
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import redis.clients.jedis.Jedis;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
import redis.clients.jedis.util.Pool;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 微信公众号存储策略自动配置.
|
||||
*
|
||||
* @author Luo
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WxMpStorageAutoConfiguration {
|
||||
private final AppContext applicationContext;
|
||||
|
||||
private final WxMpProperties wxMpProperties;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMpConfigStorage.class)
|
||||
public WxMpConfigStorage wxMpConfigStorage() {
|
||||
StorageType type = wxMpProperties.getConfigStorage().getType();
|
||||
WxMpConfigStorage config;
|
||||
switch (type) {
|
||||
case Jedis:
|
||||
config = jedisConfigStorage();
|
||||
break;
|
||||
default:
|
||||
config = defaultConfigStorage();
|
||||
break;
|
||||
}
|
||||
// wx host config
|
||||
if (null != wxMpProperties.getHosts() && StringUtils.isNotEmpty(wxMpProperties.getHosts().getApiHost())) {
|
||||
WxMpHostConfig hostConfig = new WxMpHostConfig();
|
||||
hostConfig.setApiHost(wxMpProperties.getHosts().getApiHost());
|
||||
hostConfig.setMpHost(wxMpProperties.getHosts().getMpHost());
|
||||
hostConfig.setOpenHost(wxMpProperties.getHosts().getOpenHost());
|
||||
config.setHostConfig(hostConfig);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxMpConfigStorage defaultConfigStorage() {
|
||||
WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
|
||||
setWxMpInfo(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxMpConfigStorage jedisConfigStorage() {
|
||||
Pool<Jedis> jedisPool;
|
||||
if (wxMpProperties.getConfigStorage() != null && wxMpProperties.getConfigStorage().getRedis() != null
|
||||
&& StringUtils.isNotEmpty(wxMpProperties.getConfigStorage().getRedis().getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
WxMpRedisConfigImpl wxMpRedisConfig = new WxMpRedisConfigImpl(redisOps,
|
||||
wxMpProperties.getConfigStorage().getKeyPrefix());
|
||||
setWxMpInfo(wxMpRedisConfig);
|
||||
return wxMpRedisConfig;
|
||||
}
|
||||
|
||||
private void setWxMpInfo(WxMpDefaultConfigImpl config) {
|
||||
WxMpProperties properties = wxMpProperties;
|
||||
WxMpProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setAppId(properties.getAppId());
|
||||
config.setSecret(properties.getSecret());
|
||||
config.setToken(properties.getToken());
|
||||
config.setAesKey(properties.getAesKey());
|
||||
config.setUseStableAccessToken(wxMpProperties.isUseStableAccessToken());
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
}
|
||||
|
||||
private Pool<Jedis> getJedisPool() {
|
||||
RedisProperties redis = wxMpProperties.getConfigStorage().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);
|
||||
if (StringUtils.isNotEmpty(redis.getSentinelIps())) {
|
||||
Set<String> sentinels = Sets.newHashSet(redis.getSentinelIps().split(","));
|
||||
return new JedisSentinelPool(redis.getSentinelName(), sentinels);
|
||||
}
|
||||
|
||||
return new JedisPool(config, redis.getHost(), redis.getPort(), redis.getTimeout(), redis.getPassword(),
|
||||
redis.getDatabase());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.binarywang.solon.wxjava.mp.enums;
|
||||
|
||||
/**
|
||||
* httpclient类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
HttpClient,
|
||||
/**
|
||||
* OkHttp.
|
||||
*/
|
||||
OkHttp,
|
||||
/**
|
||||
* JoddHttp.
|
||||
*/
|
||||
JoddHttp,
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.solon.wxjava.mp.enums;
|
||||
|
||||
/**
|
||||
* storage类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存.
|
||||
*/
|
||||
Memory,
|
||||
/**
|
||||
* redis(JedisClient).
|
||||
*/
|
||||
Jedis,
|
||||
/**
|
||||
* redis(Redisson).
|
||||
*/
|
||||
Redisson,
|
||||
/**
|
||||
* redis(RedisTemplate).
|
||||
*/
|
||||
RedisTemplate
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.binarywang.solon.wxjava.mp.integration;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.config.WxMpServiceAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.config.WxMpStorageAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.mp.properties.WxMpProperties;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
|
||||
/**
|
||||
* @author noear 2024/9/2 created
|
||||
*/
|
||||
public class WxMpPluginImpl implements Plugin {
|
||||
@Override
|
||||
public void start(AppContext context) throws Throwable {
|
||||
context.beanMake(WxMpProperties.class);
|
||||
|
||||
context.beanMake(WxMpStorageAutoConfiguration.class);
|
||||
context.beanMake(WxMpServiceAutoConfiguration.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.binarywang.solon.wxjava.mp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class HostConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -4172767630740346001L;
|
||||
|
||||
/**
|
||||
* 对应于:https://api.weixin.qq.com
|
||||
*/
|
||||
private String apiHost;
|
||||
|
||||
/**
|
||||
* 对应于:https://open.weixin.qq.com
|
||||
*/
|
||||
private String openHost;
|
||||
|
||||
/**
|
||||
* 对应于:https://mp.weixin.qq.com
|
||||
*/
|
||||
private String mpHost;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.binarywang.solon.wxjava.mp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* redis 配置属性.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
@Data
|
||||
public class RedisProperties implements Serializable {
|
||||
private static final long serialVersionUID = -5924815351660074401L;
|
||||
|
||||
/**
|
||||
* 主机地址.
|
||||
*/
|
||||
private String host = "127.0.0.1";
|
||||
|
||||
/**
|
||||
* 端口号.
|
||||
*/
|
||||
private int port = 6379;
|
||||
|
||||
/**
|
||||
* 密码.
|
||||
*/
|
||||
private String password;
|
||||
|
||||
/**
|
||||
* 超时.
|
||||
*/
|
||||
private int timeout = 2000;
|
||||
|
||||
/**
|
||||
* 数据库.
|
||||
*/
|
||||
private int database = 0;
|
||||
|
||||
/**
|
||||
* sentinel ips
|
||||
*/
|
||||
private String sentinelIps;
|
||||
|
||||
/**
|
||||
* sentinel name
|
||||
*/
|
||||
private String sentinelName;
|
||||
|
||||
private Integer maxActive;
|
||||
private Integer maxIdle;
|
||||
private Integer maxWaitMillis;
|
||||
private Integer minIdle;
|
||||
}
|
||||
@@ -0,0 +1,106 @@
|
||||
package com.binarywang.solon.wxjava.mp.properties;
|
||||
|
||||
import com.binarywang.solon.wxjava.mp.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.mp.enums.StorageType;
|
||||
import lombok.Data;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.annotation.Inject;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import static com.binarywang.solon.wxjava.mp.enums.StorageType.Memory;
|
||||
import static com.binarywang.solon.wxjava.mp.properties.WxMpProperties.PREFIX;
|
||||
|
||||
/**
|
||||
* 微信接入相关配置属性.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@Inject("${" + PREFIX + "}")
|
||||
public class WxMpProperties {
|
||||
public static final String PREFIX = "wx.mp";
|
||||
|
||||
/**
|
||||
* 设置微信公众号的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置微信公众号的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置微信公众号的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置微信公众号的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 是否使用稳定版 Access Token
|
||||
*/
|
||||
private boolean useStableAccessToken = false;
|
||||
|
||||
/**
|
||||
* 自定义host配置
|
||||
*/
|
||||
private HostConfig hosts;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
private final ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
@Data
|
||||
public static class ConfigStorage implements Serializable {
|
||||
private static final long serialVersionUID = 4815731027000065434L;
|
||||
|
||||
/**
|
||||
* 存储类型.
|
||||
*/
|
||||
private StorageType type = Memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀.
|
||||
*/
|
||||
private String keyPrefix = "wx";
|
||||
|
||||
/**
|
||||
* redis连接配置.
|
||||
*/
|
||||
private final RedisProperties redis = new RedisProperties();
|
||||
|
||||
/**
|
||||
* http客户端类型.
|
||||
*/
|
||||
private HttpClientType httpClientType = HttpClientType.HttpClient;
|
||||
|
||||
/**
|
||||
* http代理主机.
|
||||
*/
|
||||
private String httpProxyHost;
|
||||
|
||||
/**
|
||||
* http代理端口.
|
||||
*/
|
||||
private Integer httpProxyPort;
|
||||
|
||||
/**
|
||||
* http代理用户名.
|
||||
*/
|
||||
private String httpProxyUsername;
|
||||
|
||||
/**
|
||||
* http代理密码.
|
||||
*/
|
||||
private String httpProxyPassword;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
solon.plugin=com.binarywang.solon.wxjava.mp.integration.WxMpPluginImpl
|
||||
solon.plugin.priority=10
|
||||
Reference in New Issue
Block a user