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
package cn.iocoder.yudao.module.wxcp.framework.config;
 
import cn.iocoder.yudao.module.wxcp.framework.core.WxCpClientFactory;
import lombok.extern.slf4j.Slf4j;
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.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
/**
 * 企业微信配置类
 *
 * 注意:业务模块位于组件扫描路径 cn.iocoder.yudao.module 下,因此使用 @Configuration;
 * 若用 @AutoConfiguration 会被 Spring Boot 的 AutoConfigurationExcludeFilter 排除,导致 Bean 不注册
 *
 * @author 超级管理员
 */
@Configuration(proxyBeanMethods = false)
@Slf4j
@ConditionalOnClass(WxCpService.class)
@EnableConfigurationProperties(WxCpProperties.class)
@ConditionalOnProperty(value = "wx.cp.enabled", havingValue = "true", matchIfMissing = true)
public class YudaoWxCpConfiguration {
 
    /**
     * 创建企业微信 WxCpService Bean(单应用模式)
     */
    @Bean
    public WxCpService wxCpService(WxCpProperties properties) {
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(properties.getCorpId());
        config.setAgentId(Integer.valueOf(properties.getAgentId()));
        config.setCorpSecret(properties.getSecret());
        config.setToken(properties.getToken());
        config.setAesKey(properties.getAesKey());
 
        WxCpServiceImpl wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
 
        log.info("[wxCpService][企业微信服务初始化完成 corpId={} agentId={}]",
                properties.getCorpId(), properties.getAgentId());
        return wxCpService;
    }
 
    /**
     * 创建企业微信客户端工厂(支持多应用)
     */
    @Bean
    public WxCpClientFactory wxCpClientFactory(WxCpProperties properties) {
        return new WxCpClientFactory(properties);
    }
 
}