liu
2026-09-30 91491239c5d4611b5b1404a7abeb2bb23d0edad5
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
package cn.iocoder.yudao.module.wxcp.framework.core;
 
import cn.iocoder.yudao.module.wxcp.framework.config.WxCpProperties;
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;
 
/**
 * 企业微信客户端工厂
 *
 * 支持多应用场景,每个应用有自己的 AgentId 和 Secret
 *
 * @author 超级管理员
 */
@Slf4j
public class WxCpClientFactory {
 
    private final WxCpProperties properties;
 
    public WxCpClientFactory(WxCpProperties properties) {
        this.properties = properties;
    }
 
    /**
     * 获取默认应用(主应用)的 WxCpService
     */
    public WxCpService getService() {
        return buildService(properties.getAgentId(), properties.getSecret());
    }
 
    /**
     * 获取指定 AgentId 的 WxCpService
     *
     * @param agentId 应用 AgentId
     * @param secret  应用 Secret
     */
    public WxCpService getService(String agentId, String secret) {
        return buildService(agentId, secret);
    }
 
    private WxCpService buildService(String agentId, String secret) {
        WxCpDefaultConfigImpl config = new WxCpDefaultConfigImpl();
        config.setCorpId(properties.getCorpId());
        config.setAgentId(Integer.valueOf(agentId));
        config.setCorpSecret(secret);
        config.setToken(properties.getToken());
        config.setAesKey(properties.getAesKey());
 
        WxCpServiceImpl wxCpService = new WxCpServiceImpl();
        wxCpService.setWxCpConfigStorage(config);
        return wxCpService;
    }
 
}