🆕 #2991【视频号】增加视频号模块,实现视频号大部分相关接口

This commit is contained in:
Zeyes Lee
2023-05-11 20:20:48 +08:00
committed by Binary Wang
parent 899ea653be
commit 9d6faa0935
420 changed files with 20931 additions and 1 deletions

View File

@ -0,0 +1,20 @@
package com.binarywang.spring.starter.wxjava.channel.config;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* 自动配置
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@EnableConfigurationProperties(WxChannelProperties.class)
@Import({
WxChannelStorageAutoConfiguration.class,
WxChannelServiceAutoConfiguration.class
})
public class WxChannelAutoConfiguration {
}

View File

@ -0,0 +1,37 @@
package com.binarywang.spring.starter.wxjava.channel.config;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import lombok.AllArgsConstructor;
import me.chanjar.weixin.channel.api.WxChannelService;
import me.chanjar.weixin.channel.api.impl.WxChannelServiceImpl;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 微信小程序平台相关服务自动注册
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@AllArgsConstructor
public class WxChannelServiceAutoConfiguration {
private final WxChannelProperties properties;
/**
* Channel Service
*
* @return Channel Service
*/
@Bean
@ConditionalOnMissingBean(WxChannelService.class)
@ConditionalOnBean(WxChannelConfig.class)
public WxChannelService wxChannelService(WxChannelConfig wxChannelConfig) {
WxChannelService wxChannelService = new WxChannelServiceImpl();
wxChannelService.setConfig(wxChannelConfig);
return wxChannelService;
}
}

View File

@ -0,0 +1,23 @@
package com.binarywang.spring.starter.wxjava.channel.config;
import com.binarywang.spring.starter.wxjava.channel.config.storage.WxChannelInJedisConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.channel.config.storage.WxChannelInMemoryConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.channel.config.storage.WxChannelInRedisTemplateConfigStorageConfiguration;
import com.binarywang.spring.starter.wxjava.channel.config.storage.WxChannelInRedissonConfigStorageConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* 微信小程序存储策略自动配置
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@Import({
WxChannelInMemoryConfigStorageConfiguration.class,
WxChannelInJedisConfigStorageConfiguration.class,
WxChannelInRedisTemplateConfigStorageConfiguration.class,
WxChannelInRedissonConfigStorageConfiguration.class
})
public class WxChannelStorageAutoConfiguration {
}

View File

@ -0,0 +1,39 @@
package com.binarywang.spring.starter.wxjava.channel.config.storage;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import me.chanjar.weixin.channel.config.impl.WxChannelDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;
/**
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
public abstract class AbstractWxChannelConfigStorageConfiguration {
protected WxChannelDefaultConfigImpl config(WxChannelDefaultConfigImpl config, WxChannelProperties 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()));
WxChannelProperties.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;
}
}

View File

@ -0,0 +1,73 @@
package com.binarywang.spring.starter.wxjava.channel.config.storage;
import com.binarywang.spring.starter.wxjava.channel.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.config.impl.WxChannelRedisConfigImpl;
import me.chanjar.weixin.common.redis.JedisWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;
/**
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@ConditionalOnProperty(prefix = WxChannelProperties.PREFIX + ".config-storage", name = "type", havingValue = "jedis")
@ConditionalOnClass({JedisPool.class, JedisPoolConfig.class})
@RequiredArgsConstructor
public class WxChannelInJedisConfigStorageConfiguration extends AbstractWxChannelConfigStorageConfiguration {
private final WxChannelProperties properties;
private final ApplicationContext applicationContext;
@Bean
@ConditionalOnMissingBean(WxChannelConfig.class)
public WxChannelConfig wxChannelConfig() {
WxChannelRedisConfigImpl config = getWxChannelRedisConfig();
return this.config(config, properties);
}
private WxChannelRedisConfigImpl getWxChannelRedisConfig() {
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 WxChannelRedisConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix());
}
private JedisPool getJedisPool() {
WxChannelProperties.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());
}
}

View File

@ -0,0 +1,29 @@
package com.binarywang.spring.starter.wxjava.channel.config.storage;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.config.impl.WxChannelDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@ConditionalOnProperty(prefix = WxChannelProperties.PREFIX + ".config-storage", name = "type",
matchIfMissing = true, havingValue = "memory")
@RequiredArgsConstructor
public class WxChannelInMemoryConfigStorageConfiguration extends AbstractWxChannelConfigStorageConfiguration {
private final WxChannelProperties properties;
@Bean
@ConditionalOnMissingBean(WxChannelProperties.class)
public WxChannelConfig wxChannelConfig() {
WxChannelDefaultConfigImpl config = new WxChannelDefaultConfigImpl();
return this.config(config, properties);
}
}

View File

@ -0,0 +1,40 @@
package com.binarywang.spring.starter.wxjava.channel.config.storage;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.config.impl.WxChannelRedisConfigImpl;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
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;
/**
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@ConditionalOnProperty(prefix = WxChannelProperties.PREFIX + ".config-storage", name = "type", havingValue = "redistemplate")
@ConditionalOnClass(StringRedisTemplate.class)
@RequiredArgsConstructor
public class WxChannelInRedisTemplateConfigStorageConfiguration extends AbstractWxChannelConfigStorageConfiguration {
private final WxChannelProperties properties;
private final ApplicationContext applicationContext;
@Bean
@ConditionalOnMissingBean(WxChannelConfig.class)
public WxChannelConfig wxChannelConfig() {
WxChannelRedisConfigImpl config = getWxChannelInRedisTemplateConfig();
return this.config(config, properties);
}
private WxChannelRedisConfigImpl getWxChannelInRedisTemplateConfig() {
StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
WxRedisOps redisOps = new RedisTemplateWxRedisOps(redisTemplate);
return new WxChannelRedisConfigImpl(redisOps, properties.getConfigStorage().getKeyPrefix());
}
}

View File

@ -0,0 +1,62 @@
package com.binarywang.spring.starter.wxjava.channel.config.storage;
import com.binarywang.spring.starter.wxjava.channel.properties.RedisProperties;
import com.binarywang.spring.starter.wxjava.channel.properties.WxChannelProperties;
import lombok.RequiredArgsConstructor;
import me.chanjar.weixin.channel.config.WxChannelConfig;
import me.chanjar.weixin.channel.config.impl.WxChannelRedissonConfigImpl;
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.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Configuration
@ConditionalOnProperty(prefix = WxChannelProperties.PREFIX + ".config-storage", name = "type", havingValue = "redisson")
@ConditionalOnClass({Redisson.class, RedissonClient.class})
@RequiredArgsConstructor
public class WxChannelInRedissonConfigStorageConfiguration extends AbstractWxChannelConfigStorageConfiguration {
private final WxChannelProperties properties;
private final ApplicationContext applicationContext;
@Bean
@ConditionalOnMissingBean(WxChannelConfig.class)
public WxChannelConfig wxChannelConfig() {
WxChannelRedissonConfigImpl config = getWxChannelRedissonConfig();
return this.config(config, properties);
}
private WxChannelRedissonConfigImpl getWxChannelRedissonConfig() {
RedisProperties redisProperties = properties.getConfigStorage().getRedis();
RedissonClient redissonClient;
if (redisProperties != null && StringUtils.isNotEmpty(redisProperties.getHost())) {
redissonClient = getRedissonClient();
} else {
redissonClient = applicationContext.getBean(RedissonClient.class);
}
return new WxChannelRedissonConfigImpl(redissonClient, properties.getConfigStorage().getKeyPrefix());
}
private RedissonClient getRedissonClient() {
WxChannelProperties.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);
}
}

View File

@ -0,0 +1,13 @@
package com.binarywang.spring.starter.wxjava.channel.enums;
/**
* httpclient类型
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
public enum HttpClientType {
/**
* HttpClient
*/
HttpClient
}

View File

@ -0,0 +1,25 @@
package com.binarywang.spring.starter.wxjava.channel.enums;
/**
* storage类型
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
public enum StorageType {
/**
* 内存
*/
Memory,
/**
* redis(JedisClient)
*/
Jedis,
/**
* redis(Redisson)
*/
Redisson,
/**
* redis(RedisTemplate)
*/
RedisTemplate
}

View File

@ -0,0 +1,42 @@
package com.binarywang.spring.starter.wxjava.channel.properties;
import lombok.Data;
/**
* redis 配置
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Data
public class RedisProperties {
/**
* 主机地址,不填则从spring容器内获取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;
}

View File

@ -0,0 +1,109 @@
package com.binarywang.spring.starter.wxjava.channel.properties;
import com.binarywang.spring.starter.wxjava.channel.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.channel.enums.StorageType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
/**
* 属性配置类
*
* @author <a href="https://github.com/lixize">Zeyes</a>
*/
@Data
@ConfigurationProperties(prefix = WxChannelProperties.PREFIX)
public class WxChannelProperties {
public static final String PREFIX = "wx.channel";
/**
* 设置视频号小店的appid
*/
private String appid;
/**
* 设置视频号小店的Secret
*/
private String secret;
/**
* 设置视频号小店消息服务器配置的token.
*/
private String token;
/**
* 设置视频号小店消息服务器配置的EncodingAESKey
*/
private String aesKey;
/**
* 消息格式XML或者JSON
*/
private String msgDataFormat = "JSON";
/**
* 存储策略
*/
private final ConfigStorage configStorage = new ConfigStorage();
@Data
public static class ConfigStorage {
/**
* 存储类型
*/
private StorageType type = StorageType.Memory;
/**
* 指定key前缀
*/
private String keyPrefix = "wh";
/**
* redis连接配置
*/
@NestedConfigurationProperty
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 me.chanjar.weixin.channel.api.BaseWxChannelService#setRetrySleepMillis(int)}
* </pre>
*/
private int retrySleepMillis = 1000;
/**
* http 请求最大重试次数
* <pre>
* {@link me.chanjar.weixin.channel.api.BaseWxChannelService#setMaxRetryTimes(int)}
* </pre>
*/
private int maxRetryTimes = 5;
}
}

View File

@ -0,0 +1,2 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.binarywang.spring.starter.wxjava.channel.config.WxChannelAutoConfiguration

View File

@ -0,0 +1 @@
com.binarywang.spring.starter.wxjava.channel.config.WxChannelAutoConfiguration