mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 12:38:22 +08:00
🆕 #1952 增加腾讯企点子模块,用于对接企点开放平台。
This commit is contained in:
@ -0,0 +1,17 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.config;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.qidian.properties.WxQidianProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
|
||||
/**
|
||||
* .
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(WxQidianProperties.class)
|
||||
@Import({ WxQidianStorageAutoConfiguration.class, WxQidianServiceAutoConfiguration.class })
|
||||
public class WxQidianAutoConfiguration {
|
||||
}
|
||||
@ -0,0 +1,63 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.config;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.qidian.enums.HttpClientType;
|
||||
import com.binarywang.spring.starter.wxjava.qidian.properties.WxQidianProperties;
|
||||
import me.chanjar.weixin.qidian.api.WxQidianService;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceHttpClientImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceJoddHttpImpl;
|
||||
import me.chanjar.weixin.qidian.api.impl.WxQidianServiceOkHttpImpl;
|
||||
import me.chanjar.weixin.qidian.config.WxQidianConfigStorage;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 腾讯企点相关服务自动注册.
|
||||
*
|
||||
* @author alegria
|
||||
*/
|
||||
@Configuration
|
||||
public class WxQidianServiceAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public WxQidianService wxQidianService(WxQidianConfigStorage configStorage, WxQidianProperties wxQidianProperties) {
|
||||
HttpClientType httpClientType = wxQidianProperties.getConfigStorage().getHttpClientType();
|
||||
WxQidianService wxQidianService;
|
||||
switch (httpClientType) {
|
||||
case OkHttp:
|
||||
wxQidianService = newWxQidianServiceOkHttpImpl();
|
||||
break;
|
||||
case JoddHttp:
|
||||
wxQidianService = newWxQidianServiceJoddHttpImpl();
|
||||
break;
|
||||
case HttpClient:
|
||||
wxQidianService = newWxQidianServiceHttpClientImpl();
|
||||
break;
|
||||
default:
|
||||
wxQidianService = newWxQidianServiceImpl();
|
||||
break;
|
||||
}
|
||||
|
||||
wxQidianService.setWxMpConfigStorage(configStorage);
|
||||
return wxQidianService;
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceImpl() {
|
||||
return new WxQidianServiceImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceHttpClientImpl() {
|
||||
return new WxQidianServiceHttpClientImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceOkHttpImpl() {
|
||||
return new WxQidianServiceOkHttpImpl();
|
||||
}
|
||||
|
||||
private WxQidianService newWxQidianServiceJoddHttpImpl() {
|
||||
return new WxQidianServiceJoddHttpImpl();
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,166 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.config;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.qidian.enums.StorageType;
|
||||
import com.binarywang.spring.starter.wxjava.qidian.properties.RedisProperties;
|
||||
import com.binarywang.spring.starter.wxjava.qidian.properties.WxQidianProperties;
|
||||
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.RedisTemplateWxRedisOps;
|
||||
import me.chanjar.weixin.common.redis.WxRedisOps;
|
||||
import me.chanjar.weixin.qidian.bean.WxQidianHostConfig;
|
||||
import me.chanjar.weixin.qidian.config.WxQidianConfigStorage;
|
||||
import me.chanjar.weixin.qidian.config.impl.WxQidianDefaultConfigImpl;
|
||||
import me.chanjar.weixin.qidian.config.impl.WxQidianRedisConfigImpl;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import redis.clients.jedis.JedisPool;
|
||||
import redis.clients.jedis.JedisPoolAbstract;
|
||||
import redis.clients.jedis.JedisPoolConfig;
|
||||
import redis.clients.jedis.JedisSentinelPool;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 腾讯企点存储策略自动配置.
|
||||
*
|
||||
* @author alegria
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
@RequiredArgsConstructor
|
||||
public class WxQidianStorageAutoConfiguration {
|
||||
private final ApplicationContext applicationContext;
|
||||
|
||||
private final WxQidianProperties wxQidianProperties;
|
||||
|
||||
@Value("${wx.mp.config-storage.redis.host:")
|
||||
private String redisHost;
|
||||
|
||||
@Value("${wx.mp.configStorage.redis.host:")
|
||||
private String redisHost2;
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(WxQidianConfigStorage.class)
|
||||
public WxQidianConfigStorage wxQidianConfigStorage() {
|
||||
StorageType type = wxQidianProperties.getConfigStorage().getType();
|
||||
WxQidianConfigStorage config;
|
||||
switch (type) {
|
||||
case Jedis:
|
||||
config = jedisConfigStorage();
|
||||
break;
|
||||
case RedisTemplate:
|
||||
config = redisTemplateConfigStorage();
|
||||
break;
|
||||
default:
|
||||
config = defaultConfigStorage();
|
||||
break;
|
||||
}
|
||||
// wx host config
|
||||
if (null != wxQidianProperties.getHosts() && StringUtils.isNotEmpty(wxQidianProperties.getHosts().getApiHost())) {
|
||||
WxQidianHostConfig hostConfig = new WxQidianHostConfig();
|
||||
hostConfig.setApiHost(wxQidianProperties.getHosts().getApiHost());
|
||||
hostConfig.setQidianHost(wxQidianProperties.getHosts().getQidianHost());
|
||||
hostConfig.setOpenHost(wxQidianProperties.getHosts().getOpenHost());
|
||||
config.setHostConfig(hostConfig);
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxQidianConfigStorage defaultConfigStorage() {
|
||||
WxQidianDefaultConfigImpl config = new WxQidianDefaultConfigImpl();
|
||||
setWxMpInfo(config);
|
||||
return config;
|
||||
}
|
||||
|
||||
private WxQidianConfigStorage jedisConfigStorage() {
|
||||
JedisPoolAbstract jedisPool;
|
||||
if (StringUtils.isNotEmpty(redisHost) || StringUtils.isNotEmpty(redisHost2)) {
|
||||
jedisPool = getJedisPool();
|
||||
} else {
|
||||
jedisPool = applicationContext.getBean(JedisPool.class);
|
||||
}
|
||||
WxRedisOps redisOps = new JedisWxRedisOps(jedisPool);
|
||||
WxQidianRedisConfigImpl wxQidianRedisConfig = new WxQidianRedisConfigImpl(redisOps,
|
||||
wxQidianProperties.getConfigStorage().getKeyPrefix());
|
||||
setWxMpInfo(wxQidianRedisConfig);
|
||||
return wxQidianRedisConfig;
|
||||
}
|
||||
|
||||
private WxQidianConfigStorage redisTemplateConfigStorage() {
|
||||
StringRedisTemplate redisTemplate = null;
|
||||
try {
|
||||
redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
try {
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("stringRedisTemplate");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
|
||||
if (null == redisTemplate) {
|
||||
redisTemplate = (StringRedisTemplate) applicationContext.getBean("redisTemplate");
|
||||
}
|
||||
|
||||
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
|
||||
WxQidianRedisConfigImpl wxMpRedisConfig = new WxQidianRedisConfigImpl(redisOps,
|
||||
wxQidianProperties.getConfigStorage().getKeyPrefix());
|
||||
|
||||
setWxMpInfo(wxMpRedisConfig);
|
||||
return wxMpRedisConfig;
|
||||
}
|
||||
|
||||
private void setWxMpInfo(WxQidianDefaultConfigImpl config) {
|
||||
WxQidianProperties properties = wxQidianProperties;
|
||||
WxQidianProperties.ConfigStorage configStorageProperties = properties.getConfigStorage();
|
||||
config.setAppId(properties.getAppId());
|
||||
config.setSecret(properties.getSecret());
|
||||
config.setToken(properties.getToken());
|
||||
config.setAesKey(properties.getAesKey());
|
||||
|
||||
config.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
|
||||
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
|
||||
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
|
||||
if (configStorageProperties.getHttpProxyPort() != null) {
|
||||
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
|
||||
}
|
||||
}
|
||||
|
||||
private JedisPoolAbstract getJedisPool() {
|
||||
WxQidianProperties.ConfigStorage storage = wxQidianProperties.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);
|
||||
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.spring.starter.wxjava.qidian.enums;
|
||||
|
||||
/**
|
||||
* httpclient类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-08-30
|
||||
*/
|
||||
public enum HttpClientType {
|
||||
/**
|
||||
* HttpClient.
|
||||
*/
|
||||
HttpClient,
|
||||
/**
|
||||
* OkHttp.
|
||||
*/
|
||||
OkHttp,
|
||||
/**
|
||||
* JoddHttp.
|
||||
*/
|
||||
JoddHttp,
|
||||
}
|
||||
@ -0,0 +1,22 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.enums;
|
||||
|
||||
/**
|
||||
* storage类型.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 2020-08-30
|
||||
*/
|
||||
public enum StorageType {
|
||||
/**
|
||||
* 内存.
|
||||
*/
|
||||
Memory,
|
||||
/**
|
||||
* redis(JedisClient).
|
||||
*/
|
||||
Jedis,
|
||||
/**
|
||||
* redis(RedisTemplate).
|
||||
*/
|
||||
RedisTemplate
|
||||
}
|
||||
@ -0,0 +1,18 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
public class HostConfig implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -4172767630740346001L;
|
||||
|
||||
private String apiHost;
|
||||
|
||||
private String openHost;
|
||||
|
||||
private String qidianHost;
|
||||
|
||||
}
|
||||
@ -0,0 +1,56 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.properties;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* redis 配置属性.
|
||||
*
|
||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||
* @date 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,99 @@
|
||||
package com.binarywang.spring.starter.wxjava.qidian.properties;
|
||||
|
||||
import com.binarywang.spring.starter.wxjava.qidian.enums.HttpClientType;
|
||||
import com.binarywang.spring.starter.wxjava.qidian.enums.StorageType;
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import static com.binarywang.spring.starter.wxjava.qidian.enums.StorageType.Memory;
|
||||
import static com.binarywang.spring.starter.wxjava.qidian.properties.WxQidianProperties.PREFIX;
|
||||
|
||||
/**
|
||||
* 企点接入相关配置属性.
|
||||
*
|
||||
* @author someone
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(PREFIX)
|
||||
public class WxQidianProperties {
|
||||
public static final String PREFIX = "wx.qidian";
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的appid.
|
||||
*/
|
||||
private String appId;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的app secret.
|
||||
*/
|
||||
private String secret;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的token.
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 设置腾讯企点的EncodingAESKey.
|
||||
*/
|
||||
private String aesKey;
|
||||
|
||||
/**
|
||||
* 自定义host配置
|
||||
*/
|
||||
private HostConfig hosts;
|
||||
|
||||
/**
|
||||
* 存储策略
|
||||
*/
|
||||
private 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 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 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.binarywang.spring.starter.wxjava.qidian.config.WxQidianAutoConfiguration
|
||||
Reference in New Issue
Block a user