123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- package cn.nosum.wx.cp.config;
- import cn.nosum.http.RequestHttp;
- import cn.nosum.http.RequestHttpProperties;
- import cn.nosum.http.enums.HttpType;
- import cn.nosum.wx.cp.api.WxCpService;
- import cn.nosum.wx.cp.api.impl.WxCpServiceApacheHttpClientImpl;
- import cn.nosum.wx.cp.api.impl.WxCpServiceJoddHttpImpl;
- import cn.nosum.wx.cp.api.impl.WxCpServiceOkHttpImpl;
- import cn.nosum.wx.cp.config.impl.WxCpDefaultConfigImpl;
- import lombok.val;
- import org.apache.commons.lang3.StringUtils;
- import org.apache.http.impl.BHttpConnectionBase;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.context.properties.EnableConfigurationProperties;
- import org.springframework.context.annotation.Configuration;
- import javax.annotation.PostConstruct;
- import java.util.List;
- import java.util.Map;
- import java.util.Objects;
- import java.util.concurrent.ConcurrentHashMap;
- import java.util.stream.Collectors;
- /**
- * 企业微信配置类.
- *
- * @author Young
- */
- @Configuration
- @EnableConfigurationProperties({WxCpMultiAppProperties.class, WxCpHttpProperties.class, WxCpHttpProxyProperties.class})
- public class WxCpConfiguration {
- @Autowired
- private WxCpMultiAppProperties properties;
- private static Map<Integer, WxCpService> cpAgentIdServiceMap = new ConcurrentHashMap<>();
- @PostConstruct
- public void initServices() {
- // 获取所有服务的配置
- List<WxCpService> wxCpServiceList = this.properties.getAppConfigs().stream().map(agent -> {
- val configStorage = new WxCpDefaultConfigImpl();
- configStorage.setCorpId(this.properties.getCorpId());
- configStorage.setAgentId(agent.getAgentId());
- configStorage.setCorpSecret(agent.getSecret());
- configStorage.setToken(agent.getToken());
- configStorage.setAesKey(agent.getAesKey());
- // HTTP 配置
- RequestHttpProperties httpProperties = configStorage.getHttpProperties();
- httpProperties.setReadTimeout(properties.getHttp().getReadTimeout());
- httpProperties.setConnectTimeout(properties.getHttp().getConnectTimeout());
- // 设置代理对象
- WxCpHttpProxyProperties proxy = properties.getHttp().getProxy();
- if (proxy.isEnabled()) {
- httpProperties.setHttpProxyHost(proxy.getHost());
- httpProperties.setHttpProxyPort(proxy.getPort());
- httpProperties.setHttpProxyUsername(proxy.getUserName());
- httpProperties.setHttpProxyPassword(proxy.getPassword());
- }
- WxCpService service = null;
- String type = properties.getHttp().getType();
- if (StringUtils.isNotBlank(type)) {
- if (type.equals(HttpType.APACHE_HTTP.getName())) {
- WxCpServiceApacheHttpClientImpl wxCpServiceApacheHttpClient = new WxCpServiceApacheHttpClientImpl();
- wxCpServiceApacheHttpClient.getApacheHttpClientBuilder().build();
- service = new WxCpServiceApacheHttpClientImpl();
- }
- if (type.equals(HttpType.JODD_HTTP.getName())) {
- service = new WxCpServiceJoddHttpImpl();
- }
- if (type.equals(HttpType.OK_HTTP.getName())) {
- service = new WxCpServiceOkHttpImpl();
- }
- }
- if (service == null) {
- service = new WxCpServiceApacheHttpClientImpl();
- }
- service.setWxCpConfigStorage(configStorage);
- return service;
- }).collect(Collectors.toList());
- // 保存所有的服务
- cpAgentIdServiceMap = wxCpServiceList.stream().filter(service -> Objects.nonNull(service.getWxCpConfigStorage().getAgentId())).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), service -> service));
- }
- /**
- * 根据应用 ID 获取对应的服务.
- *
- * @param agentId 应用 ID
- * @return 服务
- */
- public static WxCpService getCpService(Integer agentId) {
- return cpAgentIdServiceMap.get(agentId);
- }
- }
|