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