mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-01-22 02:52:03 +08:00
🆕 #3217 增加 solon-plugins 适配
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.config;
|
||||
|
||||
import cn.binarywang.wx.miniapp.api.WxMaService;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceHttpClientImpl;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceJoddHttpImpl;
|
||||
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceOkHttpImpl;
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import com.binarywang.solon.wxjava.miniapp.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 微信小程序平台相关服务自动注册.
|
||||
*
|
||||
* @author someone TaoYu
|
||||
*/
|
||||
@Configuration
|
||||
@AllArgsConstructor
|
||||
public class WxMaServiceAutoConfiguration {
|
||||
|
||||
private final WxMaProperties wxMaProperties;
|
||||
|
||||
/**
|
||||
* 小程序service.
|
||||
*
|
||||
* @return 小程序service
|
||||
*/
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMaService.class, onBean=WxMaConfig.class)
|
||||
public WxMaService wxMaService(WxMaConfig wxMaConfig) {
|
||||
HttpClientType httpClientType = wxMaProperties.getConfigStorage().getHttpClientType();
|
||||
WxMaService wxMaService;
|
||||
switch (httpClientType) {
|
||||
case OkHttp:
|
||||
wxMaService = new WxMaServiceOkHttpImpl();
|
||||
break;
|
||||
case JoddHttp:
|
||||
wxMaService = new WxMaServiceJoddHttpImpl();
|
||||
break;
|
||||
case HttpClient:
|
||||
wxMaService = new WxMaServiceHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
wxMaService = new WxMaServiceImpl();
|
||||
break;
|
||||
}
|
||||
wxMaService.setWxMaConfig(wxMaConfig);
|
||||
return wxMaService;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.config.storage;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
/**
|
||||
* @author yl TaoYu
|
||||
*/
|
||||
public abstract class AbstractWxMaConfigStorageConfiguration {
|
||||
|
||||
protected WxMaDefaultConfigImpl config(WxMaDefaultConfigImpl config, WxMaProperties properties) {
|
||||
config.setAppid(StringUtils.trimToNull(properties.getAppid()));
|
||||
config.setSecret(StringUtils.trimToNull(properties.getSecret()));
|
||||
config.setToken(StringUtils.trimToNull(properties.getToken()));
|
||||
config.setAesKey(StringUtils.trimToNull(properties.getAesKey()));
|
||||
config.setMsgDataFormat(StringUtils.trimToNull(properties.getMsgDataFormat()));
|
||||
|
||||
WxMaProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
|
||||
int maxRetryTimes = configStorageProperties.getMaxRetryTimes();
|
||||
if (configStorageProperties.getMaxRetryTimes() < 0) {
|
||||
maxRetryTimes = 0;
|
||||
}
|
||||
int retrySleepMillis = configStorageProperties.getRetrySleepMillis();
|
||||
if (retrySleepMillis < 0) {
|
||||
retrySleepMillis = 1000;
|
||||
}
|
||||
config.setRetrySleepMillis(retrySleepMillis);
|
||||
config.setMaxRetryTimes(maxRetryTimes);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.config.storage;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaRedisBetterConfigImpl;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
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.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
|
||||
/**
|
||||
* @author yl TaoYu
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxMaProperties.PREFIX + ".configStorage.type} = jedis",
|
||||
onClass = JedisPool.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMaInJedisConfigStorageConfiguration extends AbstractWxMaConfigStorageConfiguration {
|
||||
private final WxMaProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMaConfig.class)
|
||||
public WxMaConfig wxMaConfig() {
|
||||
WxMaRedisBetterConfigImpl config = getWxMaRedisBetterConfigImpl();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxMaRedisBetterConfigImpl getWxMaRedisBetterConfigImpl() {
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
JedisPool jedisPool;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
return new WxMaRedisBetterConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private JedisPool getJedisPool() {
|
||||
WxMaProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
RedisProperties 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,28 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.config.storage;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.noear.solon.annotation.Bean;
|
||||
import org.noear.solon.annotation.Condition;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* @author yl TaoYu
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxMaProperties.PREFIX + ".configStorage.type:memory} = memory"
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMaInMemoryConfigStorageConfiguration extends AbstractWxMaConfigStorageConfiguration {
|
||||
private final WxMaProperties properties;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMaConfig.class)
|
||||
public WxMaConfig wxMaConfig() {
|
||||
WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,61 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.config.storage;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
import cn.binarywang.wx.miniapp.config.impl.WxMaRedissonConfigImpl;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.RedisProperties;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
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 org.redisson.Redisson;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.redisson.config.Config;
|
||||
import org.redisson.config.TransportMode;
|
||||
|
||||
/**
|
||||
* @author yl TaoYu
|
||||
*/
|
||||
@Configuration
|
||||
@Condition(
|
||||
onProperty = "${"+WxMaProperties.PREFIX + ".configStorage.type} = redisson",
|
||||
onClass = Redisson.class
|
||||
)
|
||||
@RequiredArgsConstructor
|
||||
public class WxMaInRedissonConfigStorageConfiguration extends AbstractWxMaConfigStorageConfiguration {
|
||||
private final WxMaProperties properties;
|
||||
private final AppContext applicationContext;
|
||||
|
||||
@Bean
|
||||
@Condition(onMissingBean=WxMaConfig.class)
|
||||
public WxMaConfig wxMaConfig() {
|
||||
WxMaRedissonConfigImpl config = getWxMaInRedissonConfigStorage();
|
||||
return this.config(config, properties);
|
||||
}
|
||||
|
||||
private WxMaRedissonConfigImpl getWxMaInRedissonConfigStorage() {
|
||||
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
|
||||
RedissonClient redissonClient;
|
||||
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
|
||||
redissonClient = getRedissonClient();
|
||||
} else {
|
||||
redissonClient = applicationContext.getBean(RedissonClient.class);
|
||||
}
|
||||
return new WxMaRedissonConfigImpl(redissonClient, properties.getConfigStorage().getKeyPrefix());
|
||||
}
|
||||
|
||||
private RedissonClient getRedissonClient() {
|
||||
WxMaProperties.ConfigStorage storage = properties.getConfigStorage();
|
||||
RedisProperties 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,22 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.enums;
|
||||
|
||||
/**
|
||||
* httpclient类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-05-25
|
||||
*/
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
HttpClient,
|
||||
/**
|
||||
* OkHttp.
|
||||
*/
|
||||
OkHttp,
|
||||
/**
|
||||
* JoddHttp.
|
||||
*/
|
||||
JoddHttp,
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.enums;
|
||||
|
||||
/**
|
||||
* storage类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-05-25
|
||||
*/
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存.
|
||||
*/
|
||||
Memory,
|
||||
/**
|
||||
* redis(JedisClient).
|
||||
*/
|
||||
Jedis,
|
||||
/**
|
||||
* redis(Redisson).
|
||||
*/
|
||||
Redisson,
|
||||
/**
|
||||
* redis(RedisTemplate).
|
||||
*/
|
||||
RedisTemplate
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.integration;
|
||||
|
||||
import com.binarywang.solon.wxjava.miniapp.config.WxMaServiceAutoConfiguration;
|
||||
import com.binarywang.solon.wxjava.miniapp.config.storage.WxMaInJedisConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.miniapp.config.storage.WxMaInMemoryConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.miniapp.config.storage.WxMaInRedissonConfigStorageConfiguration;
|
||||
import com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties;
|
||||
import org.noear.solon.core.AppContext;
|
||||
import org.noear.solon.core.Plugin;
|
||||
|
||||
/**
|
||||
* @author noear 2024/9/2 created
|
||||
*/
|
||||
public class WxMiniappPluginImpl implements Plugin {
|
||||
@Override
|
||||
public void start(AppContext context) throws Throwable {
|
||||
context.beanMake(WxMaProperties.class);
|
||||
|
||||
context.beanMake(WxMaServiceAutoConfiguration.class);
|
||||
|
||||
context.beanMake(WxMaInMemoryConfigStorageConfiguration.class);
|
||||
context.beanMake(WxMaInJedisConfigStorageConfiguration.class);
|
||||
context.beanMake(WxMaInRedissonConfigStorageConfiguration.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* redis 配置.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2020-08-30
|
||||
*/
|
||||
@Data
|
||||
public class RedisProperties {
|
||||
|
||||
/**
|
||||
* 主机地址.不填则从solon容器内获取JedisPool
|
||||
*/
|
||||
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,112 @@
|
||||
package com.binarywang.solon.wxjava.miniapp.properties;
|
||||
|
||||
import com.binarywang.solon.wxjava.miniapp.enums.HttpClientType;
|
||||
import com.binarywang.solon.wxjava.miniapp.enums.StorageType;
|
||||
import lombok.Data;
|
||||
import org.noear.solon.annotation.Configuration;
|
||||
import org.noear.solon.annotation.Inject;
|
||||
|
||||
import static com.binarywang.solon.wxjava.miniapp.properties.WxMaProperties.PREFIX;
|
||||
|
||||
/**
|
||||
* 属性配置类.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* created on 2019-08-10
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@Inject("${" + PREFIX + "}")
|
||||
public class WxMaProperties {
|
||||
public static final String PREFIX = "wx.miniapp";
|
||||
|
||||
/**
|
||||
* 设置微信小程序的appid.
|
||||
*/
|
||||
private String appid;
|
||||
|
||||
/**
|
||||
* 设置微信小程序的Secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置微信小程序消息服务器配置的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置微信小程序消息服务器配置的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 消息格式,XML或者JSON.
|
||||
*/
|
||||
private String msgDataFormat;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
private final ConfigStorage configStorage = new ConfigStorage();
|
||||
|
||||
@Data
|
||||
public static class ConfigStorage {
|
||||
|
||||
/**
|
||||
* 存储类型.
|
||||
*/
|
||||
private StorageType type = StorageType.Memory;
|
||||
|
||||
/**
|
||||
* 指定key前缀.
|
||||
*/
|
||||
private String keyPrefix = "wa";
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
* http 请求重试间隔
|
||||
* <pre>
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setRetrySleepMillis(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int retrySleepMillis = 1000;
|
||||
/**
|
||||
* http 请求最大重试次数
|
||||
* <pre>
|
||||
* {@link cn.binarywang.wx.miniapp.api.impl.BaseWxMaServiceImpl#setMaxRetryTimes(int)}
|
||||
* </pre>
|
||||
*/
|
||||
private int maxRetryTimes = 5;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
solon.plugin=com.binarywang.solon.wxjava.miniapp.integration.WxMiniappPluginImpl
|
||||
solon.plugin.priority=10
|
||||
Reference in New Issue
Block a user