|
43 | 43 | import java.util.Map; |
44 | 44 | import java.util.concurrent.TimeUnit; |
45 | 45 | import java.util.concurrent.locks.Lock; |
| 46 | +import java.util.function.Function; |
46 | 47 |
|
47 | 48 | import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*; |
48 | 49 |
|
@@ -156,6 +157,10 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH |
156 | 157 | @Setter |
157 | 158 | private WxMpFreePublishService freePublishService = new WxMpFreePublishServiceImpl(this); |
158 | 159 |
|
| 160 | + @Getter |
| 161 | + @Setter |
| 162 | + private Function<String, WxMpConfigStorage> configStorageFunction; |
| 163 | + |
159 | 164 | private Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>(); |
160 | 165 |
|
161 | 166 | private int retrySleepMillis = 1000; |
@@ -575,21 +580,43 @@ public void removeConfigStorage(String mpId) { |
575 | 580 |
|
576 | 581 | @Override |
577 | 582 | public WxMpService switchoverTo(String mpId) { |
| 583 | + return switchoverTo(mpId, configStorageFunction); |
| 584 | + } |
| 585 | + |
| 586 | + @Override |
| 587 | + public WxMpService switchoverTo(String mpId, Function<String, WxMpConfigStorage> func) { |
578 | 588 | if (this.configStorageMap.containsKey(mpId)) { |
579 | 589 | WxMpConfigStorageHolder.set(mpId); |
580 | 590 | return this; |
581 | 591 | } |
582 | | - |
| 592 | + if (func != null) { |
| 593 | + WxMpConfigStorage storage = func.apply(mpId); |
| 594 | + if (storage != null) { |
| 595 | + this.addConfigStorage(mpId, storage); |
| 596 | + return this; |
| 597 | + } |
| 598 | + } |
583 | 599 | throw new WxRuntimeException(String.format("无法找到对应【%s】的公众号配置信息,请核实!", mpId)); |
584 | 600 | } |
585 | 601 |
|
586 | 602 | @Override |
587 | 603 | public boolean switchover(String mpId) { |
| 604 | + return switchover(mpId, configStorageFunction); |
| 605 | + } |
| 606 | + |
| 607 | + @Override |
| 608 | + public boolean switchover(String mpId, Function<String, WxMpConfigStorage> func) { |
588 | 609 | if (this.configStorageMap.containsKey(mpId)) { |
589 | 610 | WxMpConfigStorageHolder.set(mpId); |
590 | 611 | return true; |
591 | 612 | } |
592 | | - |
| 613 | + if (func != null) { |
| 614 | + WxMpConfigStorage storage = func.apply(mpId); |
| 615 | + if (storage != null) { |
| 616 | + this.addConfigStorage(mpId, storage); |
| 617 | + return true; |
| 618 | + } |
| 619 | + } |
593 | 620 | log.error("无法找到对应【{}】的公众号配置信息,请核实!", mpId); |
594 | 621 | return false; |
595 | 622 | } |
|
0 commit comments