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; } }