|
| 1 | +package com.binarywang.spring.starter.wxjava.cp.configuration.services; |
| 2 | + |
| 3 | +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpMultiProperties; |
| 4 | +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpTpSingleProperties; |
| 5 | +import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServices; |
| 6 | +import com.binarywang.spring.starter.wxjava.cp.service.WxCpTpMultiServicesImpl; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import me.chanjar.weixin.cp.config.WxCpTpConfigStorage; |
| 10 | +import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl; |
| 11 | +import me.chanjar.weixin.cp.tp.service.WxCpTpService; |
| 12 | +import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceApacheHttpClientImpl; |
| 13 | +import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceImpl; |
| 14 | +import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceJoddHttpImpl; |
| 15 | +import me.chanjar.weixin.cp.tp.service.impl.WxCpTpServiceOkHttpImpl; |
| 16 | +import org.apache.commons.lang3.StringUtils; |
| 17 | + |
| 18 | +import java.util.Collection; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +/** |
| 25 | + * WxCpConfigStorage 抽象配置类 |
| 26 | + * |
| 27 | + * @author yl |
| 28 | + * created on 2023/10/16 |
| 29 | + */ |
| 30 | +@RequiredArgsConstructor |
| 31 | +@Slf4j |
| 32 | +public abstract class AbstractWxCpTpConfiguration { |
| 33 | + |
| 34 | + /** |
| 35 | + * |
| 36 | + * @param wxCpTpMultiProperties 应用列表配置 |
| 37 | + * @param services 用于支持,应用启动之后,可以调用这个接口添加新服务对象。主要是配置是从数据库中读取的 |
| 38 | + * @return |
| 39 | + */ |
| 40 | + public WxCpTpMultiServices wxCpMultiServices(WxCpTpMultiProperties wxCpTpMultiProperties,WxCpTpMultiServices services) { |
| 41 | + Map<String, WxCpTpSingleProperties> corps = wxCpTpMultiProperties.getCorps(); |
| 42 | + if (corps == null || corps.isEmpty()) { |
| 43 | + log.warn("企业微信应用参数未配置,通过 WxCpMultiServices#getWxCpTpService(\"tenantId\")获取实例将返回空"); |
| 44 | + return new WxCpTpMultiServicesImpl(); |
| 45 | + } |
| 46 | + |
| 47 | + if (services == null) { |
| 48 | + services = new WxCpTpMultiServicesImpl(); |
| 49 | + } |
| 50 | + |
| 51 | + Set<Map.Entry<String, WxCpTpSingleProperties>> entries = corps.entrySet(); |
| 52 | + for (Map.Entry<String, WxCpTpSingleProperties> entry : entries) { |
| 53 | + String tenantId = entry.getKey(); |
| 54 | + WxCpTpSingleProperties wxCpTpSingleProperties = entry.getValue(); |
| 55 | + WxCpTpDefaultConfigImpl storage = this.wxCpTpConfigStorage(wxCpTpMultiProperties); |
| 56 | + this.configCorp(storage, wxCpTpSingleProperties); |
| 57 | + this.configHttp(storage, wxCpTpMultiProperties.getConfigStorage()); |
| 58 | + WxCpTpService wxCpTpService = this.wxCpTpService(storage, wxCpTpMultiProperties.getConfigStorage()); |
| 59 | + if (services.getWxCpTpService(tenantId) == null) { |
| 60 | + // 不存在的才会添加到服务列表中 |
| 61 | + services.addWxCpTpService(tenantId, wxCpTpService); |
| 62 | + } |
| 63 | + } |
| 64 | + return services; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 配置 WxCpDefaultConfigImpl |
| 69 | + * |
| 70 | + * @param wxCpTpMultiProperties 参数 |
| 71 | + * @return WxCpDefaultConfigImpl |
| 72 | + */ |
| 73 | + protected abstract WxCpTpDefaultConfigImpl wxCpTpConfigStorage(WxCpTpMultiProperties wxCpTpMultiProperties); |
| 74 | + |
| 75 | + private WxCpTpService wxCpTpService(WxCpTpConfigStorage wxCpTpConfigStorage, WxCpTpMultiProperties.ConfigStorage storage) { |
| 76 | + WxCpTpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType(); |
| 77 | + WxCpTpService cpTpService; |
| 78 | + switch (httpClientType) { |
| 79 | + case OK_HTTP: |
| 80 | + cpTpService = new WxCpTpServiceOkHttpImpl(); |
| 81 | + break; |
| 82 | + case JODD_HTTP: |
| 83 | + cpTpService = new WxCpTpServiceJoddHttpImpl(); |
| 84 | + break; |
| 85 | + case HTTP_CLIENT: |
| 86 | + cpTpService = new WxCpTpServiceApacheHttpClientImpl(); |
| 87 | + break; |
| 88 | + default: |
| 89 | + cpTpService = new WxCpTpServiceImpl(); |
| 90 | + break; |
| 91 | + } |
| 92 | + cpTpService.setWxCpTpConfigStorage(wxCpTpConfigStorage); |
| 93 | + int maxRetryTimes = storage.getMaxRetryTimes(); |
| 94 | + if (maxRetryTimes < 0) { |
| 95 | + maxRetryTimes = 0; |
| 96 | + } |
| 97 | + int retrySleepMillis = storage.getRetrySleepMillis(); |
| 98 | + if (retrySleepMillis < 0) { |
| 99 | + retrySleepMillis = 1000; |
| 100 | + } |
| 101 | + cpTpService.setRetrySleepMillis(retrySleepMillis); |
| 102 | + cpTpService.setMaxRetryTimes(maxRetryTimes); |
| 103 | + return cpTpService; |
| 104 | + } |
| 105 | + |
| 106 | + private void configCorp(WxCpTpDefaultConfigImpl config, WxCpTpSingleProperties wxCpTpSingleProperties) { |
| 107 | + String corpId = wxCpTpSingleProperties.getCorpId(); |
| 108 | + String providerSecret = wxCpTpSingleProperties.getProviderSecret(); |
| 109 | + String suiteId = wxCpTpSingleProperties.getSuiteId(); |
| 110 | + String token = wxCpTpSingleProperties.getToken(); |
| 111 | + String suiteSecret = wxCpTpSingleProperties.getSuiteSecret(); |
| 112 | + // 企业微信,私钥,会话存档路径 |
| 113 | + config.setCorpId(corpId); |
| 114 | + config.setProviderSecret(providerSecret); |
| 115 | + config.setEncodingAESKey(wxCpTpSingleProperties.getEncodingAESKey()); |
| 116 | + config.setSuiteId(suiteId); |
| 117 | + config.setToken(token); |
| 118 | + config.setSuiteSecret(suiteSecret); |
| 119 | + } |
| 120 | + |
| 121 | + private void configHttp(WxCpTpDefaultConfigImpl config, WxCpTpMultiProperties.ConfigStorage storage) { |
| 122 | + String httpProxyHost = storage.getHttpProxyHost(); |
| 123 | + Integer httpProxyPort = storage.getHttpProxyPort(); |
| 124 | + String httpProxyUsername = storage.getHttpProxyUsername(); |
| 125 | + String httpProxyPassword = storage.getHttpProxyPassword(); |
| 126 | + if (StringUtils.isNotBlank(httpProxyHost)) { |
| 127 | + config.setHttpProxyHost(httpProxyHost); |
| 128 | + if (httpProxyPort != null) { |
| 129 | + config.setHttpProxyPort(httpProxyPort); |
| 130 | + } |
| 131 | + if (StringUtils.isNotBlank(httpProxyUsername)) { |
| 132 | + config.setHttpProxyUsername(httpProxyUsername); |
| 133 | + } |
| 134 | + if (StringUtils.isNotBlank(httpProxyPassword)) { |
| 135 | + config.setHttpProxyPassword(httpProxyPassword); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments