WxCpConfiguration.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. package cn.nosum.wx.cp.config;
  2. import cn.nosum.http.RequestHttp;
  3. import cn.nosum.http.RequestHttpProperties;
  4. import cn.nosum.http.enums.HttpType;
  5. import cn.nosum.wx.cp.api.WxCpService;
  6. import cn.nosum.wx.cp.api.impl.WxCpServiceApacheHttpClientImpl;
  7. import cn.nosum.wx.cp.api.impl.WxCpServiceJoddHttpImpl;
  8. import cn.nosum.wx.cp.api.impl.WxCpServiceOkHttpImpl;
  9. import cn.nosum.wx.cp.config.impl.WxCpDefaultConfigImpl;
  10. import lombok.val;
  11. import org.apache.commons.lang3.StringUtils;
  12. import org.apache.http.impl.BHttpConnectionBase;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  15. import org.springframework.context.annotation.Configuration;
  16. import javax.annotation.PostConstruct;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Objects;
  20. import java.util.concurrent.ConcurrentHashMap;
  21. import java.util.stream.Collectors;
  22. /**
  23. * 企业微信配置类.
  24. *
  25. * @author Young
  26. */
  27. @Configuration
  28. @EnableConfigurationProperties({WxCpMultiAppProperties.class, WxCpHttpProperties.class, WxCpHttpProxyProperties.class})
  29. public class WxCpConfiguration {
  30. @Autowired
  31. private WxCpMultiAppProperties properties;
  32. private static Map<Integer, WxCpService> cpAgentIdServiceMap = new ConcurrentHashMap<>();
  33. @PostConstruct
  34. public void initServices() {
  35. // 获取所有服务的配置
  36. List<WxCpService> wxCpServiceList = this.properties.getAppConfigs().stream().map(agent -> {
  37. val configStorage = new WxCpDefaultConfigImpl();
  38. configStorage.setCorpId(this.properties.getCorpId());
  39. configStorage.setAgentId(agent.getAgentId());
  40. configStorage.setCorpSecret(agent.getSecret());
  41. configStorage.setToken(agent.getToken());
  42. configStorage.setAesKey(agent.getAesKey());
  43. // HTTP 配置
  44. RequestHttpProperties httpProperties = configStorage.getHttpProperties();
  45. httpProperties.setReadTimeout(properties.getHttp().getReadTimeout());
  46. httpProperties.setConnectTimeout(properties.getHttp().getConnectTimeout());
  47. // 设置代理对象
  48. WxCpHttpProxyProperties proxy = properties.getHttp().getProxy();
  49. if (proxy.isEnabled()) {
  50. httpProperties.setHttpProxyHost(proxy.getHost());
  51. httpProperties.setHttpProxyPort(proxy.getPort());
  52. httpProperties.setHttpProxyUsername(proxy.getUserName());
  53. httpProperties.setHttpProxyPassword(proxy.getPassword());
  54. }
  55. WxCpService service = null;
  56. String type = properties.getHttp().getType();
  57. if (StringUtils.isNotBlank(type)) {
  58. if (type.equals(HttpType.APACHE_HTTP.getName())) {
  59. WxCpServiceApacheHttpClientImpl wxCpServiceApacheHttpClient = new WxCpServiceApacheHttpClientImpl();
  60. wxCpServiceApacheHttpClient.getApacheHttpClientBuilder().build();
  61. service = new WxCpServiceApacheHttpClientImpl();
  62. }
  63. if (type.equals(HttpType.JODD_HTTP.getName())) {
  64. service = new WxCpServiceJoddHttpImpl();
  65. }
  66. if (type.equals(HttpType.OK_HTTP.getName())) {
  67. service = new WxCpServiceOkHttpImpl();
  68. }
  69. }
  70. if (service == null) {
  71. service = new WxCpServiceApacheHttpClientImpl();
  72. }
  73. service.setWxCpConfigStorage(configStorage);
  74. return service;
  75. }).collect(Collectors.toList());
  76. // 保存所有的服务
  77. cpAgentIdServiceMap = wxCpServiceList.stream().filter(service -> Objects.nonNull(service.getWxCpConfigStorage().getAgentId())).collect(Collectors.toMap(service -> service.getWxCpConfigStorage().getAgentId(), service -> service));
  78. }
  79. /**
  80. * 根据应用 ID 获取对应的服务.
  81. *
  82. * @param agentId 应用 ID
  83. * @return 服务
  84. */
  85. public static WxCpService getCpService(Integer agentId) {
  86. return cpAgentIdServiceMap.get(agentId);
  87. }
  88. }