|
| 1 | +package com.binarywang.spring.starter.wxjava.open.configuration.services; |
| 2 | + |
| 3 | +import com.binarywang.spring.starter.wxjava.open.properties.WxOpenMultiProperties; |
| 4 | +import com.binarywang.spring.starter.wxjava.open.properties.WxOpenSingleProperties; |
| 5 | +import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServices; |
| 6 | +import com.binarywang.spring.starter.wxjava.open.service.WxOpenMultiServicesImpl; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import me.chanjar.weixin.common.util.http.apache.ApacheHttpClientBuilder; |
| 10 | +import me.chanjar.weixin.common.util.http.apache.DefaultApacheHttpClientBuilder; |
| 11 | +import me.chanjar.weixin.open.api.WxOpenConfigStorage; |
| 12 | +import me.chanjar.weixin.open.api.WxOpenService; |
| 13 | +import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage; |
| 14 | +import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl; |
| 15 | +import org.apache.commons.lang3.StringUtils; |
| 16 | + |
| 17 | +import java.util.Collection; |
| 18 | +import java.util.Map; |
| 19 | +import java.util.Set; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +/** |
| 23 | + * WxOpenConfigStorage 抽象配置类 |
| 24 | + * |
| 25 | + * @author Binary Wang |
| 26 | + */ |
| 27 | +@RequiredArgsConstructor |
| 28 | +@Slf4j |
| 29 | +public abstract class AbstractWxOpenConfiguration { |
| 30 | + |
| 31 | + protected WxOpenMultiServices wxOpenMultiServices(WxOpenMultiProperties wxOpenMultiProperties) { |
| 32 | + Map<String, WxOpenSingleProperties> appsMap = wxOpenMultiProperties.getApps(); |
| 33 | + if (appsMap == null || appsMap.isEmpty()) { |
| 34 | + log.warn("微信开放平台应用参数未配置,通过 WxOpenMultiServices#getWxOpenService(\"tenantId\")获取实例将返回空"); |
| 35 | + return new WxOpenMultiServicesImpl(); |
| 36 | + } |
| 37 | + /** |
| 38 | + * 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。 |
| 39 | + */ |
| 40 | + Collection<WxOpenSingleProperties> apps = appsMap.values(); |
| 41 | + if (apps.size() > 1) { |
| 42 | + // 校验 appId 是否唯一 |
| 43 | + String nullAppIdPlaceholder = "__NULL_APP_ID__"; |
| 44 | + boolean multi = apps.stream() |
| 45 | + // 没有 appId,如果不判断是否为空,这里会报 NPE 异常 |
| 46 | + .collect(Collectors.groupingBy(c -> c.getAppId() == null ? nullAppIdPlaceholder : c.getAppId(), Collectors.counting())) |
| 47 | + .entrySet().stream().anyMatch(e -> e.getValue() > 1); |
| 48 | + if (multi) { |
| 49 | + throw new RuntimeException("请确保微信开放平台配置 appId 的唯一性"); |
| 50 | + } |
| 51 | + } |
| 52 | + WxOpenMultiServicesImpl services = new WxOpenMultiServicesImpl(); |
| 53 | + |
| 54 | + Set<Map.Entry<String, WxOpenSingleProperties>> entries = appsMap.entrySet(); |
| 55 | + for (Map.Entry<String, WxOpenSingleProperties> entry : entries) { |
| 56 | + String tenantId = entry.getKey(); |
| 57 | + WxOpenSingleProperties wxOpenSingleProperties = entry.getValue(); |
| 58 | + WxOpenInMemoryConfigStorage storage = this.wxOpenConfigStorage(wxOpenMultiProperties); |
| 59 | + this.configApp(storage, wxOpenSingleProperties); |
| 60 | + this.configHttp(storage, wxOpenMultiProperties.getConfigStorage()); |
| 61 | + WxOpenService wxOpenService = this.wxOpenService(storage, wxOpenMultiProperties); |
| 62 | + services.addWxOpenService(tenantId, wxOpenService); |
| 63 | + } |
| 64 | + return services; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 配置 WxOpenInMemoryConfigStorage |
| 69 | + * |
| 70 | + * @param wxOpenMultiProperties 参数 |
| 71 | + * @return WxOpenInMemoryConfigStorage |
| 72 | + */ |
| 73 | + protected abstract WxOpenInMemoryConfigStorage wxOpenConfigStorage(WxOpenMultiProperties wxOpenMultiProperties); |
| 74 | + |
| 75 | + public WxOpenService wxOpenService(WxOpenConfigStorage configStorage, WxOpenMultiProperties wxOpenMultiProperties) { |
| 76 | + WxOpenService wxOpenService = new WxOpenServiceImpl(); |
| 77 | + wxOpenService.setWxOpenConfigStorage(configStorage); |
| 78 | + return wxOpenService; |
| 79 | + } |
| 80 | + |
| 81 | + private void configApp(WxOpenInMemoryConfigStorage config, WxOpenSingleProperties appProperties) { |
| 82 | + String appId = appProperties.getAppId(); |
| 83 | + String secret = appProperties.getSecret(); |
| 84 | + String token = appProperties.getToken(); |
| 85 | + String aesKey = appProperties.getAesKey(); |
| 86 | + String apiHostUrl = appProperties.getApiHostUrl(); |
| 87 | + String accessTokenUrl = appProperties.getAccessTokenUrl(); |
| 88 | + |
| 89 | + // appId 和 secret 是必需的 |
| 90 | + if (StringUtils.isBlank(appId)) { |
| 91 | + throw new IllegalArgumentException("微信开放平台 appId 不能为空"); |
| 92 | + } |
| 93 | + if (StringUtils.isBlank(secret)) { |
| 94 | + throw new IllegalArgumentException("微信开放平台 secret 不能为空"); |
| 95 | + } |
| 96 | + |
| 97 | + config.setComponentAppId(appId); |
| 98 | + config.setComponentAppSecret(secret); |
| 99 | + if (StringUtils.isNotBlank(token)) { |
| 100 | + config.setComponentToken(token); |
| 101 | + } |
| 102 | + if (StringUtils.isNotBlank(aesKey)) { |
| 103 | + config.setComponentAesKey(aesKey); |
| 104 | + } |
| 105 | + // 设置URL配置 |
| 106 | + config.setApiHostUrl(StringUtils.trimToNull(apiHostUrl)); |
| 107 | + config.setAccessTokenUrl(StringUtils.trimToNull(accessTokenUrl)); |
| 108 | + } |
| 109 | + |
| 110 | + private void configHttp(WxOpenInMemoryConfigStorage config, WxOpenMultiProperties.ConfigStorage storage) { |
| 111 | + String httpProxyHost = storage.getHttpProxyHost(); |
| 112 | + Integer httpProxyPort = storage.getHttpProxyPort(); |
| 113 | + String httpProxyUsername = storage.getHttpProxyUsername(); |
| 114 | + String httpProxyPassword = storage.getHttpProxyPassword(); |
| 115 | + if (StringUtils.isNotBlank(httpProxyHost)) { |
| 116 | + config.setHttpProxyHost(httpProxyHost); |
| 117 | + if (httpProxyPort != null) { |
| 118 | + config.setHttpProxyPort(httpProxyPort); |
| 119 | + } |
| 120 | + if (StringUtils.isNotBlank(httpProxyUsername)) { |
| 121 | + config.setHttpProxyUsername(httpProxyUsername); |
| 122 | + } |
| 123 | + if (StringUtils.isNotBlank(httpProxyPassword)) { |
| 124 | + config.setHttpProxyPassword(httpProxyPassword); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // 设置重试配置 |
| 129 | + int maxRetryTimes = storage.getMaxRetryTimes(); |
| 130 | + if (maxRetryTimes < 0) { |
| 131 | + maxRetryTimes = 0; |
| 132 | + } |
| 133 | + int retrySleepMillis = storage.getRetrySleepMillis(); |
| 134 | + if (retrySleepMillis < 0) { |
| 135 | + retrySleepMillis = 1000; |
| 136 | + } |
| 137 | + config.setRetrySleepMillis(retrySleepMillis); |
| 138 | + config.setMaxRetryTimes(maxRetryTimes); |
| 139 | + |
| 140 | + // 设置自定义的HttpClient超时配置 |
| 141 | + ApacheHttpClientBuilder clientBuilder = config.getApacheHttpClientBuilder(); |
| 142 | + if (clientBuilder == null) { |
| 143 | + clientBuilder = DefaultApacheHttpClientBuilder.get(); |
| 144 | + } |
| 145 | + if (clientBuilder instanceof DefaultApacheHttpClientBuilder) { |
| 146 | + DefaultApacheHttpClientBuilder defaultBuilder = (DefaultApacheHttpClientBuilder) clientBuilder; |
| 147 | + defaultBuilder.setConnectionTimeout(storage.getConnectionTimeout()); |
| 148 | + defaultBuilder.setSoTimeout(storage.getSoTimeout()); |
| 149 | + defaultBuilder.setConnectionRequestTimeout(storage.getConnectionRequestTimeout()); |
| 150 | + config.setApacheHttpClientBuilder(defaultBuilder); |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments