WxCpConfiguration.java 4.7 KB

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