|
| 1 | +package com.binarywang.spring.starter.wxjava.cp.autoconfigure.services; |
| 2 | + |
| 3 | +import com.binarywang.spring.starter.wxjava.cp.properties.CorpProperties; |
| 4 | +import com.binarywang.spring.starter.wxjava.cp.properties.WxCpMultiProperties; |
| 5 | +import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServices; |
| 6 | +import com.binarywang.spring.starter.wxjava.cp.service.WxCpMultiServicesImpl; |
| 7 | +import lombok.RequiredArgsConstructor; |
| 8 | +import me.chanjar.weixin.cp.api.WxCpService; |
| 9 | +import me.chanjar.weixin.cp.api.impl.WxCpServiceImpl; |
| 10 | +import me.chanjar.weixin.cp.config.WxCpConfigStorage; |
| 11 | +import me.chanjar.weixin.cp.config.impl.WxCpDefaultConfigImpl; |
| 12 | +import org.apache.commons.lang3.StringUtils; |
| 13 | + |
| 14 | +import java.util.Collection; |
| 15 | +import java.util.List; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.Set; |
| 18 | +import java.util.stream.Collectors; |
| 19 | + |
| 20 | +/** |
| 21 | + * WxCpConfigStorage 抽象配置类 |
| 22 | + * |
| 23 | + * @author yl |
| 24 | + * created on 2023/10/16 |
| 25 | + */ |
| 26 | +@RequiredArgsConstructor |
| 27 | +public abstract class AbstractWxCpConfiguration { |
| 28 | + |
| 29 | + protected WxCpMultiServices configWxCpServices(WxCpMultiProperties wxCpMultiProperties) { |
| 30 | + WxCpMultiServicesImpl wxCpServices = new WxCpMultiServicesImpl(); |
| 31 | + Map<String, CorpProperties> corps = wxCpMultiProperties.getCorps(); |
| 32 | + if (corps == null || corps.isEmpty()) { |
| 33 | + throw new RuntimeException("企业微信配置为null"); |
| 34 | + } |
| 35 | + /** |
| 36 | + * 校验同一个企业下,agentId 是否唯一,避免使用 redis 缓存 token、ticket 时错乱。 |
| 37 | + * |
| 38 | + * 查看 {@link me.chanjar.weixin.cp.config.impl.AbstractWxCpInRedisConfigImpl#setAgentId(Integer)} |
| 39 | + */ |
| 40 | + Collection<CorpProperties> corpList = corps.values(); |
| 41 | + if (corpList.size() > 1) { |
| 42 | + // 先按 corpId 分组统计 |
| 43 | + Map<String, List<CorpProperties>> corpsMap = corpList.stream() |
| 44 | + .collect(Collectors.groupingBy(CorpProperties::getCorpId)); |
| 45 | + Set<Map.Entry<String, List<CorpProperties>>> entries = corpsMap.entrySet(); |
| 46 | + for (Map.Entry<String, List<CorpProperties>> entry : entries) { |
| 47 | + String corpId = entry.getKey(); |
| 48 | + // 校验每个企业下,agentId 是否唯一 |
| 49 | + boolean multi = entry.getValue().stream() |
| 50 | + // 通讯录没有 agentId,如果不判断是否为空,这里会报 NPE 异常 |
| 51 | + .collect(Collectors.groupingBy(c -> c.getAgentId() == null ? 0 : c.getAgentId(), Collectors.counting())) |
| 52 | + .entrySet().stream().anyMatch(e -> e.getValue() > 1); |
| 53 | + if (multi) { |
| 54 | + throw new RuntimeException("请确保企业微信配置唯一性[" + corpId + "]"); |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + Set<Map.Entry<String, CorpProperties>> entries = corps.entrySet(); |
| 60 | + for (Map.Entry<String, CorpProperties> entry : entries) { |
| 61 | + String tenantId = entry.getKey(); |
| 62 | + CorpProperties corpProperties = entry.getValue(); |
| 63 | + WxCpDefaultConfigImpl storage = this.configWxCpDefaultConfigImpl(wxCpMultiProperties); |
| 64 | + this.configCorp(storage, corpProperties); |
| 65 | + this.configHttp(storage, wxCpMultiProperties.getConfigStorage()); |
| 66 | + WxCpService wxCpService = this.configWxCpService(storage, wxCpMultiProperties.getConfigStorage()); |
| 67 | + wxCpServices.addWxCpService(tenantId, wxCpService); |
| 68 | + } |
| 69 | + return wxCpServices; |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * 配置 WxCpDefaultConfigImpl |
| 74 | + * |
| 75 | + * @param wxCpMultiProperties 参数 |
| 76 | + * @return WxCpDefaultConfigImpl |
| 77 | + */ |
| 78 | + protected abstract WxCpDefaultConfigImpl configWxCpDefaultConfigImpl(WxCpMultiProperties wxCpMultiProperties); |
| 79 | + |
| 80 | + private WxCpService configWxCpService(WxCpConfigStorage wxCpConfigStorage, WxCpMultiProperties.ConfigStorage storage) { |
| 81 | + WxCpService wxCpService = new WxCpServiceImpl(); |
| 82 | + wxCpService.setWxCpConfigStorage(wxCpConfigStorage); |
| 83 | + |
| 84 | + int maxRetryTimes = storage.getMaxRetryTimes(); |
| 85 | + if (maxRetryTimes < 0) { |
| 86 | + maxRetryTimes = 0; |
| 87 | + } |
| 88 | + int retrySleepMillis = storage.getRetrySleepMillis(); |
| 89 | + if (retrySleepMillis < 0) { |
| 90 | + retrySleepMillis = 1000; |
| 91 | + } |
| 92 | + wxCpService.setRetrySleepMillis(retrySleepMillis); |
| 93 | + wxCpService.setMaxRetryTimes(maxRetryTimes); |
| 94 | + return wxCpService; |
| 95 | + } |
| 96 | + |
| 97 | + private void configCorp(WxCpDefaultConfigImpl config, CorpProperties corpProperties) { |
| 98 | + String corpId = corpProperties.getCorpId(); |
| 99 | + String corpSecret = corpProperties.getCorpSecret(); |
| 100 | + Integer agentId = corpProperties.getAgentId(); |
| 101 | + String token = corpProperties.getToken(); |
| 102 | + String aesKey = corpProperties.getAesKey(); |
| 103 | + // 企业微信,私钥,会话存档路径 |
| 104 | + String msgAuditPriKey = corpProperties.getMsgAuditPriKey(); |
| 105 | + String msgAuditLibPath = corpProperties.getMsgAuditLibPath(); |
| 106 | + |
| 107 | + config.setCorpId(corpId); |
| 108 | + config.setCorpSecret(corpSecret); |
| 109 | + config.setAgentId(agentId); |
| 110 | + if (StringUtils.isNotBlank(token)) { |
| 111 | + config.setToken(token); |
| 112 | + } |
| 113 | + if (StringUtils.isNotBlank(aesKey)) { |
| 114 | + config.setAesKey(aesKey); |
| 115 | + } |
| 116 | + if (StringUtils.isNotBlank(msgAuditPriKey)) { |
| 117 | + config.setMsgAuditPriKey(msgAuditPriKey); |
| 118 | + } |
| 119 | + if (StringUtils.isNotBlank(msgAuditLibPath)) { |
| 120 | + config.setMsgAuditLibPath(msgAuditLibPath); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + private void configHttp(WxCpDefaultConfigImpl config, WxCpMultiProperties.ConfigStorage storage) { |
| 125 | + String httpProxyHost = storage.getHttpProxyHost(); |
| 126 | + Integer httpProxyPort = storage.getHttpProxyPort(); |
| 127 | + String httpProxyUsername = storage.getHttpProxyUsername(); |
| 128 | + String httpProxyPassword = storage.getHttpProxyPassword(); |
| 129 | + if (StringUtils.isNotBlank(httpProxyHost)) { |
| 130 | + config.setHttpProxyHost(httpProxyHost); |
| 131 | + if (httpProxyPort != null) { |
| 132 | + config.setHttpProxyPort(httpProxyPort); |
| 133 | + } |
| 134 | + if (StringUtils.isNotBlank(httpProxyUsername)) { |
| 135 | + config.setHttpProxyUsername(httpProxyUsername); |
| 136 | + } |
| 137 | + if (StringUtils.isNotBlank(httpProxyPassword)) { |
| 138 | + config.setHttpProxyPassword(httpProxyPassword); |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | +} |
0 commit comments