|
| 1 | +package com.binarywang.spring.starter.wxjava.mp.configuration.services; |
| 2 | + |
| 3 | +import com.binarywang.spring.starter.wxjava.mp.properties.WxMpMultiProperties; |
| 4 | +import com.binarywang.spring.starter.wxjava.mp.properties.WxMpSingleProperties; |
| 5 | +import com.binarywang.spring.starter.wxjava.mp.service.WxMpMultiServices; |
| 6 | +import com.binarywang.spring.starter.wxjava.mp.service.WxMpMultiServicesImpl; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import lombok.extern.slf4j.Slf4j; |
| 9 | +import me.chanjar.weixin.mp.api.WxMpService; |
| 10 | +import me.chanjar.weixin.mp.api.impl.WxMpServiceHttpClientImpl; |
| 11 | +import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl; |
| 12 | +import me.chanjar.weixin.mp.api.impl.WxMpServiceJoddHttpImpl; |
| 13 | +import me.chanjar.weixin.mp.api.impl.WxMpServiceOkHttpImpl; |
| 14 | +import me.chanjar.weixin.mp.config.WxMpConfigStorage; |
| 15 | +import me.chanjar.weixin.mp.config.WxMpHostConfig; |
| 16 | +import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl; |
| 17 | +import org.apache.commons.lang3.StringUtils; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Map; |
| 21 | +import java.util.Set; |
| 22 | +import java.util.stream.Collectors; |
| 23 | + |
| 24 | +/** |
| 25 | + * WxMpConfigStorage 抽象配置类 |
| 26 | + * |
| 27 | + * @author yl |
| 28 | + * created on 2024/1/23 |
| 29 | + */ |
| 30 | +@RequiredArgsConstructor |
| 31 | +@Slf4j |
| 32 | +public abstract class AbstractWxMpConfiguration { |
| 33 | + |
| 34 | + protected WxMpMultiServices wxMpMultiServices(WxMpMultiProperties wxCpMultiProperties) { |
| 35 | + Map<String, WxMpSingleProperties> appsMap = wxCpMultiProperties.getApps(); |
| 36 | + if (appsMap == null || appsMap.isEmpty()) { |
| 37 | + log.warn("微信公众号应用参数未配置,通过 WxMpMultiServices#getWxMpService(\"tenantId\")获取实例将返回空"); |
| 38 | + return new WxMpMultiServicesImpl(); |
| 39 | + } |
| 40 | + /** |
| 41 | + * 校验 appId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。 |
| 42 | + * |
| 43 | + * 查看 {@link me.chanjar.weixin.mp.config.impl.WxMpRedisConfigImpl#setAppId(String)} |
| 44 | + */ |
| 45 | + Collection<WxMpSingleProperties> apps = appsMap.values(); |
| 46 | + if (apps.size() > 1) { |
| 47 | + // 校验 appId 是否唯一 |
| 48 | + boolean multi = apps.stream() |
| 49 | + // 没有 appId,如果不判断是否为空,这里会报 NPE 异常 |
| 50 | + .collect(Collectors.groupingBy(c -> c.getAppId() == null ? 0 : c.getAppId(), Collectors.counting())) |
| 51 | + .entrySet().stream().anyMatch(e -> e.getValue() > 1); |
| 52 | + if (multi) { |
| 53 | + throw new RuntimeException("请确保微信公众号配置 appId 的唯一性"); |
| 54 | + } |
| 55 | + } |
| 56 | + WxMpMultiServicesImpl services = new WxMpMultiServicesImpl(); |
| 57 | + |
| 58 | + Set<Map.Entry<String, WxMpSingleProperties>> entries = appsMap.entrySet(); |
| 59 | + for (Map.Entry<String, WxMpSingleProperties> entry : entries) { |
| 60 | + String tenantId = entry.getKey(); |
| 61 | + WxMpSingleProperties wxMpSingleProperties = entry.getValue(); |
| 62 | + WxMpDefaultConfigImpl storage = this.wxMpConfigStorage(wxCpMultiProperties); |
| 63 | + this.configApp(storage, wxMpSingleProperties); |
| 64 | + this.configHttp(storage, wxCpMultiProperties.getConfigStorage()); |
| 65 | + this.configHost(storage, wxCpMultiProperties.getHosts()); |
| 66 | + WxMpService wxCpService = this.wxMpService(storage, wxCpMultiProperties); |
| 67 | + services.addWxMpService(tenantId, wxCpService); |
| 68 | + } |
| 69 | + return services; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 配置 WxMpDefaultConfigImpl |
| 74 | + * |
| 75 | + * @param wxMpMultiProperties 参数 |
| 76 | + * @return WxMpDefaultConfigImpl |
| 77 | + */ |
| 78 | + protected abstract WxMpDefaultConfigImpl wxMpConfigStorage(WxMpMultiProperties wxMpMultiProperties); |
| 79 | + |
| 80 | + public WxMpService wxMpService(WxMpConfigStorage configStorage, WxMpMultiProperties wxMpMultiProperties) { |
| 81 | + WxMpMultiProperties.ConfigStorage storage = wxMpMultiProperties.getConfigStorage(); |
| 82 | + WxMpMultiProperties.HttpClientType httpClientType = storage.getHttpClientType(); |
| 83 | + WxMpService wxMpService; |
| 84 | + switch (httpClientType) { |
| 85 | + case OK_HTTP: |
| 86 | + wxMpService = new WxMpServiceOkHttpImpl(); |
| 87 | + break; |
| 88 | + case JODD_HTTP: |
| 89 | + wxMpService = new WxMpServiceJoddHttpImpl(); |
| 90 | + break; |
| 91 | + case HTTP_CLIENT: |
| 92 | + wxMpService = new WxMpServiceHttpClientImpl(); |
| 93 | + break; |
| 94 | + default: |
| 95 | + wxMpService = new WxMpServiceImpl(); |
| 96 | + break; |
| 97 | + } |
| 98 | + |
| 99 | + wxMpService.setWxMpConfigStorage(configStorage); |
| 100 | + int maxRetryTimes = storage.getMaxRetryTimes(); |
| 101 | + if (maxRetryTimes < 0) { |
| 102 | + maxRetryTimes = 0; |
| 103 | + } |
| 104 | + int retrySleepMillis = storage.getRetrySleepMillis(); |
| 105 | + if (retrySleepMillis < 0) { |
| 106 | + retrySleepMillis = 1000; |
| 107 | + } |
| 108 | + wxMpService.setRetrySleepMillis(retrySleepMillis); |
| 109 | + wxMpService.setMaxRetryTimes(maxRetryTimes); |
| 110 | + return wxMpService; |
| 111 | + } |
| 112 | + |
| 113 | + private void configApp(WxMpDefaultConfigImpl config, WxMpSingleProperties corpProperties) { |
| 114 | + String appId = corpProperties.getAppId(); |
| 115 | + String appSecret = corpProperties.getAppSecret(); |
| 116 | + String token = corpProperties.getToken(); |
| 117 | + String aesKey = corpProperties.getAesKey(); |
| 118 | + |
| 119 | + config.setAppId(appId); |
| 120 | + config.setSecret(appSecret); |
| 121 | + if (StringUtils.isNotBlank(token)) { |
| 122 | + config.setToken(token); |
| 123 | + } |
| 124 | + if (StringUtils.isNotBlank(aesKey)) { |
| 125 | + config.setAesKey(aesKey); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + private void configHttp(WxMpDefaultConfigImpl config, WxMpMultiProperties.ConfigStorage storage) { |
| 130 | + String httpProxyHost = storage.getHttpProxyHost(); |
| 131 | + Integer httpProxyPort = storage.getHttpProxyPort(); |
| 132 | + String httpProxyUsername = storage.getHttpProxyUsername(); |
| 133 | + String httpProxyPassword = storage.getHttpProxyPassword(); |
| 134 | + if (StringUtils.isNotBlank(httpProxyHost)) { |
| 135 | + config.setHttpProxyHost(httpProxyHost); |
| 136 | + if (httpProxyPort != null) { |
| 137 | + config.setHttpProxyPort(httpProxyPort); |
| 138 | + } |
| 139 | + if (StringUtils.isNotBlank(httpProxyUsername)) { |
| 140 | + config.setHttpProxyUsername(httpProxyUsername); |
| 141 | + } |
| 142 | + if (StringUtils.isNotBlank(httpProxyPassword)) { |
| 143 | + config.setHttpProxyPassword(httpProxyPassword); |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * wx host config |
| 150 | + */ |
| 151 | + private void configHost(WxMpDefaultConfigImpl config, WxMpMultiProperties.HostConfig hostConfig) { |
| 152 | + if (hostConfig != null) { |
| 153 | + String apiHost = hostConfig.getApiHost(); |
| 154 | + String mpHost = hostConfig.getMpHost(); |
| 155 | + String openHost = hostConfig.getOpenHost(); |
| 156 | + WxMpHostConfig wxMpHostConfig = new WxMpHostConfig(); |
| 157 | + wxMpHostConfig.setApiHost(StringUtils.isNotBlank(apiHost) ? apiHost : null); |
| 158 | + wxMpHostConfig.setMpHost(StringUtils.isNotBlank(mpHost) ? mpHost : null); |
| 159 | + wxMpHostConfig.setOpenHost(StringUtils.isNotBlank(openHost) ? openHost : null); |
| 160 | + config.setHostConfig(wxMpHostConfig); |
| 161 | + } |
| 162 | + } |
| 163 | +} |
0 commit comments