puzhibing
2024-01-30 03f1f3372a10a08f96f3308bfa099e86a55046d0
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
package com.ruoyi.system.config;
 
import lombok.AllArgsConstructor;
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl;
import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * @ClassName WxCpConfiguration
 * @Description TODO
 * @Author jqs
 * @Date 2023/8/17 12:42
 * @Version 1.0
 */
@Configuration
@ConditionalOnClass(WxCpService.class)
@EnableConfigurationProperties(WxCpProperties.class)
@AllArgsConstructor
public class WxCpConfiguration {
 
    private WxCpProperties properties;
 
    @Bean
    @ConditionalOnMissingBean(name = "wxService")
    public WxCpService wxService() {
        WxCpDefaultConfigImpl wxCpConfigStorage = new WxCpDefaultConfigImpl();
        wxCpConfigStorage.setCorpId(properties.getCorpId());
        wxCpConfigStorage.setAgentId(properties.getAgentId());
        wxCpConfigStorage.setCorpSecret(properties.getAgentSecret());
        wxCpConfigStorage.setAesKey(properties.getEncodingAESKey());
        wxCpConfigStorage.setToken(properties.getToken());
        WxCpService wxService = new WxCpServiceImpl();
        wxService.setWxCpConfigStorage(wxCpConfigStorage);
        return wxService;
    }
 
    @Bean
    @ConditionalOnMissingBean(name = "adWxService")
    public WxCpService adWxService() {
        WxCpDefaultConfigImpl wxAdCpConfigStorage = new WxCpDefaultConfigImpl();
        wxAdCpConfigStorage.setCorpId(properties.getCorpId());
        wxAdCpConfigStorage.setAgentId(properties.getAgentId());
        wxAdCpConfigStorage.setCorpSecret(properties.getAddressSecret());
        wxAdCpConfigStorage.setAesKey(properties.getEncodingAESKey());
        wxAdCpConfigStorage.setToken(properties.getToken());
        WxCpService adWxService = new WxCpServiceImpl();
        adWxService.setWxCpConfigStorage(wxAdCpConfigStorage);
        return adWxService;
    }
 
 
    @Bean
    @ConditionalOnMissingBean
    public WxCryptUtil wxCryptUtil() {
        WxCryptUtil wxCryptUtil = new WxCryptUtil(properties.getToken(), properties.getEncodingAESKey(), properties.getCorpId());
        return wxCryptUtil;
    }
}