BAOLH
2025-12-02 d8eddeba2456ad1c89df68979112ae98d82c7948
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package me.zhengjie.config;
 
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpProperties;
import com.binarywang.spring.starter.wxjava.cp.properties.WxCpRedisProperties;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpJedisConfigImpl;
import me.chanjar.weixin.cp.config.impl.WxCpRedisTemplateConfigImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
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.JedisPoolConfig;
 
/**
 * @author baoLh
 * @date 2025-09-22
 */
@Configuration
@EnableConfigurationProperties(WxCpProperties.class)
public class WxCpConfig {
 
    private final WxCpProperties wxCpProperties;
    private final ApplicationContext applicationContext;
 
    public WxCpConfig(WxCpProperties wxCpProperties, ApplicationContext applicationContext) {
        this.wxCpProperties = wxCpProperties;
        this.applicationContext = applicationContext;
    }
 
    @Bean
    public WxCpService wxCpService() {
        WxCpDefaultConfigImpl configStorage;
        if (wxCpProperties.getConfigStorage().getType().equals(WxCpProperties.StorageType.redistemplate)) {
            // redis缓存配置
            StringRedisTemplate redisTemplate = applicationContext.getBean(StringRedisTemplate.class);
            configStorage = new WxCpRedisTemplateConfigImpl(redisTemplate, wxCpProperties.getConfigStorage().getKeyPrefix());
        } else if (wxCpProperties.getConfigStorage().getType().equals(WxCpProperties.StorageType.jedis)) {
            // jedis配置
            WxCpRedisProperties wxCpRedisProperties = wxCpProperties.getConfigStorage().getRedis();
            JedisPool jedisPool;
            if (wxCpRedisProperties != null && StringUtils.isNotEmpty(wxCpRedisProperties.getHost())) {
                jedisPool = getJedisPool();
            } else {
                jedisPool = applicationContext.getBean(JedisPool.class);
            }
            configStorage = new WxCpJedisConfigImpl(jedisPool, wxCpProperties.getConfigStorage().getKeyPrefix());
        } else {
            // 默认缓存配置
            configStorage = new WxCpDefaultConfigImpl();
        }
 
        // 基本配置
        configStorage.setCorpId(wxCpProperties.getCorpId());
        configStorage.setAgentId(wxCpProperties.getAgentId());
        configStorage.setCorpSecret(wxCpProperties.getCorpSecret());
        if (StringUtils.isNotBlank(wxCpProperties.getToken())) {
            configStorage.setToken(wxCpProperties.getToken());
        }
        if (StringUtils.isNotBlank(wxCpProperties.getAesKey())) {
            configStorage.setAesKey(wxCpProperties.getAesKey());
        }
        if (StringUtils.isNotBlank(wxCpProperties.getMsgAuditPriKey())) {
            configStorage.setMsgAuditPriKey(wxCpProperties.getMsgAuditPriKey());
        }
        if (StringUtils.isNotBlank(wxCpProperties.getMsgAuditLibPath())) {
            configStorage.setMsgAuditLibPath(wxCpProperties.getMsgAuditLibPath());
        }
 
        WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
        if (StringUtils.isNotBlank(storage.getHttpProxyHost())) {
            configStorage.setHttpProxyHost(storage.getHttpProxyHost());
            if (storage.getHttpProxyPort() != null) {
                configStorage.setHttpProxyPort(storage.getHttpProxyPort());
            }
            if (StringUtils.isNotBlank(storage.getHttpProxyUsername())) {
                configStorage.setHttpProxyUsername(storage.getHttpProxyUsername());
            }
            if (StringUtils.isNotBlank(storage.getHttpProxyPassword())) {
                configStorage.setHttpProxyPassword(storage.getHttpProxyPassword());
            }
        }
 
        WxCpService wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(configStorage);
 
        return wxCpService;
    }
 
    private JedisPool getJedisPool() {
        WxCpProperties.ConfigStorage storage = wxCpProperties.getConfigStorage();
        WxCpRedisProperties 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());
    }
}