|
3 | 3 | import cn.binarywang.wx.miniapp.api.*;
|
4 | 4 | import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
5 | 5 | import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
| 6 | +import cn.binarywang.wx.miniapp.util.WxMaConfigHolder; |
6 | 7 | import com.google.common.base.Joiner;
|
7 | 8 | import com.google.common.collect.ImmutableMap;
|
| 9 | +import com.google.common.collect.Maps; |
8 | 10 | import com.google.gson.Gson;
|
9 | 11 | import com.google.gson.JsonObject;
|
10 | 12 | import lombok.extern.slf4j.Slf4j;
|
11 | 13 | import me.chanjar.weixin.common.api.WxImgProcService;
|
12 |
| -import me.chanjar.weixin.common.enums.WxType; |
13 | 14 | import me.chanjar.weixin.common.api.WxOcrService;
|
14 | 15 | import me.chanjar.weixin.common.bean.WxAccessToken;
|
| 16 | +import me.chanjar.weixin.common.enums.WxType; |
15 | 17 | import me.chanjar.weixin.common.error.WxError;
|
16 | 18 | import me.chanjar.weixin.common.error.WxErrorException;
|
17 | 19 | import me.chanjar.weixin.common.util.DataUtils;
|
|
38 | 40 | */
|
39 | 41 | @Slf4j
|
40 | 42 | public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestHttp<H, P> {
|
41 |
| - |
| 43 | + private Map<String, WxMaConfig> configMap; |
42 | 44 | private WxMaConfig wxMaConfig;
|
43 | 45 |
|
44 | 46 | private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
|
@@ -300,11 +302,74 @@ public WxMaConfig getWxMaConfig() {
|
300 | 302 | }
|
301 | 303 |
|
302 | 304 | @Override
|
303 |
| - public void setWxMaConfig(WxMaConfig wxConfigProvider) { |
304 |
| - this.wxMaConfig = wxConfigProvider; |
| 305 | + public void setWxMaConfig(WxMaConfig maConfig) { |
| 306 | + final String appid = maConfig.getAppid(); |
| 307 | + this.setMultiConfigs(ImmutableMap.of(appid, maConfig), appid); |
| 308 | + } |
| 309 | + |
| 310 | + @Override |
| 311 | + public void setMultiConfigs(Map<String, WxMaConfig> configs) { |
| 312 | + this.setMultiConfigs(configs, configs.keySet().iterator().next()); |
| 313 | + } |
| 314 | + |
| 315 | + @Override |
| 316 | + public void setMultiConfigs(Map<String, WxMaConfig> configs, String defaultMiniappId) { |
| 317 | + this.configMap = Maps.newHashMap(configs); |
| 318 | + WxMaConfigHolder.set(defaultMiniappId); |
305 | 319 | this.initHttp();
|
306 | 320 | }
|
307 | 321 |
|
| 322 | + @Override |
| 323 | + public void addConfig(String mpId, WxMaConfig configStorages) { |
| 324 | + synchronized (this) { |
| 325 | + if (this.configMap == null) { |
| 326 | + this.setWxMaConfig(configStorages); |
| 327 | + } else { |
| 328 | + this.configMap.put(mpId, configStorages); |
| 329 | + } |
| 330 | + } |
| 331 | + } |
| 332 | + |
| 333 | + @Override |
| 334 | + public void removeConfig(String miniappId) { |
| 335 | + synchronized (this) { |
| 336 | + if (this.configMap.size() == 1) { |
| 337 | + this.configMap.remove(miniappId); |
| 338 | + log.warn("已删除最后一个小程序配置:{},须立即使用setWxMaConfig或setMultiConfigs添加配置", miniappId); |
| 339 | + return; |
| 340 | + } |
| 341 | + if (WxMaConfigHolder.get().equals(miniappId)) { |
| 342 | + this.configMap.remove(miniappId); |
| 343 | + final String defaultMpId = this.configMap.keySet().iterator().next(); |
| 344 | + WxMaConfigHolder.set(defaultMpId); |
| 345 | + log.warn("已删除默认小程序配置,小程序【{}】被设为默认配置", defaultMpId); |
| 346 | + return; |
| 347 | + } |
| 348 | + this.configMap.remove(miniappId); |
| 349 | + } |
| 350 | + } |
| 351 | + |
| 352 | + @Override |
| 353 | + public WxMaService switchoverTo(String miniappId) { |
| 354 | + if (this.configMap.containsKey(miniappId)) { |
| 355 | + WxMaConfigHolder.set(miniappId); |
| 356 | + return this; |
| 357 | + } |
| 358 | + |
| 359 | + throw new RuntimeException(String.format("无法找到对应【%s】的小程序配置信息,请核实!", miniappId)); |
| 360 | + } |
| 361 | + |
| 362 | + @Override |
| 363 | + public boolean switchover(String mpId) { |
| 364 | + if (this.configMap.containsKey(mpId)) { |
| 365 | + WxMaConfigHolder.set(mpId); |
| 366 | + return true; |
| 367 | + } |
| 368 | + |
| 369 | + log.error("无法找到对应【{}】的小程序配置信息,请核实!", mpId); |
| 370 | + return false; |
| 371 | + } |
| 372 | + |
308 | 373 | @Override
|
309 | 374 | public void setRetrySleepMillis(int retrySleepMillis) {
|
310 | 375 | this.retrySleepMillis = retrySleepMillis;
|
|
0 commit comments