Update wechat official-account spring demo project

This commit is contained in:
YuCheng Hu 2023-01-11 11:41:27 -05:00
commit 257c7cc097
19 changed files with 2503 additions and 0 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

18
.idea/compiler.xml Normal file
View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="wechat-official-account-spring" />
</profile>
</annotationProcessing>
</component>
<component name="JavacSettings">
<option name="ADDITIONAL_OPTIONS_OVERRIDE">
<module name="wechat-official-account-spring" options="-parameters" />
</option>
</component>
</project>

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

File diff suppressed because it is too large Load Diff

30
.idea/jarRepositories.xml Normal file
View File

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="ossez-repo-snapshots" />
<option name="name" value="OSSEZ Private Snapshots" />
<option name="url" value="https://repo.ossez.com/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.ossez.com/repository/maven-public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="ossez-repo-releases" />
<option name="name" value="OSSEZ Private Releases" />
<option name="url" value="https://repo.ossez.com/repository/maven-public/" />
</remote-repository>
</component>
</project>

15
.idea/misc.xml Normal file
View File

@ -0,0 +1,15 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_11" default="true" project-jdk-name="11" project-jdk-type="JavaSDK" />
<component name="ProjectType">
<option name="id" value="jpab" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

47
README.md Normal file
View File

@ -0,0 +1,47 @@
# wx-java-mp-spring-boot-starter
## 快速开始
1. 引入依赖
```xml
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>wx-java-mp-spring-boot-starter</artifactId>
<version>${version}</version>
</dependency>
```
2. 添加配置(application.properties)
```properties
# 公众号配置(必填)
wx.mp.appId = appId
wx.mp.secret = @secret
wx.mp.token = @token
wx.mp.aesKey = @aesKey
# 存储配置redis(可选)
wx.mp.config-storage.type = Jedis # 配置类型: 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

76
pom.xml Normal file
View File

@ -0,0 +1,76 @@
<?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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.5</version>
<relativePath/>
</parent>
<groupId>com.ossez.wechat</groupId>
<artifactId>wechat-official-account-spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>WeChat Official Account Spring Demo</name>
<description>WeChat Official Account Demo Example</description>
<licenses>
<license>
<name>The MIT license</name>
<url>https://opensource.org/licenses/mit-license.php</url>
<distribution>repo</distribution>
</license>
</licenses>
<developers>
<developer>
<name>YuCheng Hu</name>
<id>honeymoose</id>
<email>huyuchengus@gmail.com</email>
<timezone>-5</timezone>
<organization>Open Source</organization>
<roles>
<role>Sr. Java Developer</role>
</roles>
</developer>
</developers>
<dependencies>
<dependency>
<groupId>com.ossez.wechat</groupId>
<artifactId>wechat-j-mp</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<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>

View File

@ -0,0 +1,17 @@
package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
/**
* .
*
* @author someone
*/
@Configuration
@EnableConfigurationProperties(WxMpProperties.class)
@Import({ WxMpStorageAutoConfiguration.class, WxMpServiceAutoConfiguration.class })
public class WxMpAutoConfiguration {
}

View File

@ -0,0 +1,63 @@
package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType;
import com.binarywang.spring.starter.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.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 微信公众号相关服务自动注册.
*
* @author someone
*/
@Configuration
public class WxMpServiceAutoConfiguration {
@Bean
@ConditionalOnMissingBean
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();
}
}

View File

@ -0,0 +1,160 @@
package com.binarywang.spring.starter.wxjava.mp.config;
import com.binarywang.spring.starter.wxjava.mp.enums.StorageType;
import com.binarywang.spring.starter.wxjava.mp.properties.RedisProperties;
import com.binarywang.spring.starter.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.RedisTemplateWxRedisOps;
import me.chanjar.weixin.common.redis.WxRedisOps;
import me.chanjar.weixin.mp.config.WxMpHostConfig;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl;
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 Luo
*/
@Slf4j
@Configuration
@RequiredArgsConstructor
public class WxMpStorageAutoConfiguration {
private final ApplicationContext applicationContext;
private final WxMpProperties wxMpProperties;
@Bean
@ConditionalOnMissingBean(WxMpConfigStorage.class)
public WxMpConfigStorage wxMpConfigStorage() {
StorageType type = wxMpProperties.getConfigStorage().getType();
WxMpConfigStorage config;
switch (type) {
case Jedis:
config = jedisConfigStorage();
break;
case RedisTemplate:
config = redisTemplateConfigStorage();
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() {
JedisPoolAbstract 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 WxMpConfigStorage 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);
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.setHttpProxyHost(configStorageProperties.getHttpProxyHost());
config.setHttpProxyUsername(configStorageProperties.getHttpProxyUsername());
config.setHttpProxyPassword(configStorageProperties.getHttpProxyPassword());
if (configStorageProperties.getHttpProxyPort() != null) {
config.setHttpProxyPort(configStorageProperties.getHttpProxyPort());
}
}
private JedisPoolAbstract 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());
}
}

View File

@ -0,0 +1,22 @@
package com.binarywang.spring.starter.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,
}

View File

@ -0,0 +1,26 @@
package com.binarywang.spring.starter.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
}

View File

@ -0,0 +1,18 @@
package com.binarywang.spring.starter.wxjava.mp.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 mpHost;
}

View File

@ -0,0 +1,56 @@
package com.binarywang.spring.starter.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;
}

View File

@ -0,0 +1,101 @@
package com.binarywang.spring.starter.wxjava.mp.properties;
import com.binarywang.spring.starter.wxjava.mp.enums.HttpClientType;
import com.binarywang.spring.starter.wxjava.mp.enums.StorageType;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;
import java.io.Serializable;
import static com.binarywang.spring.starter.wxjava.mp.enums.StorageType.Memory;
import static com.binarywang.spring.starter.wxjava.mp.properties.WxMpProperties.PREFIX;
/**
* 微信接入相关配置属性.
*
* @author someone
*/
@Data
@ConfigurationProperties(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;
/**
* 自定义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连接配置.
*/
@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;
}
}

View File

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.binarywang.spring.starter.wxjava.mp.config.WxMpAutoConfiguration

View File

@ -0,0 +1 @@
com.binarywang.spring.starter.wxjava.mp.config.WxMpAutoConfiguration