From 90e1a9ebc17335230a26ed7b595fc05c274e650c Mon Sep 17 00:00:00 2001 From: zacone Date: Thu, 24 Aug 2023 20:28:33 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E5=A2=9E=E5=8A=A0switchoverTo=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=9C=A8mpId=E4=B8=8D=E5=AD=98=E5=9C=A8=E6=97=B6?= =?UTF-8?q?=E7=9A=84Storage=E5=AE=9E=E4=BE=8B=E8=8E=B7=E5=8F=96=E8=8E=B7?= =?UTF-8?q?=E5=8F=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../me/chanjar/weixin/mp/api/WxMpService.java | 5 +++ .../mp/api/impl/BaseWxMpServiceImpl.java | 31 +++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java index c544c7f46..6df78c12d 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java @@ -20,6 +20,7 @@ import me.chanjar.weixin.mp.config.WxMpConfigStorage; import me.chanjar.weixin.mp.enums.WxMpApiUrl; import java.util.Map; +import java.util.function.Function; /** * 微信公众号API的Service. @@ -393,6 +394,8 @@ public interface WxMpService extends WxService { */ boolean switchover(String mpId); + boolean switchover(String mpId, Function func); + /** * 进行相应的公众号切换. * @@ -401,6 +404,8 @@ public interface WxMpService extends WxService { */ WxMpService switchoverTo(String mpId); + WxMpService switchoverTo(String mpId, Function func); + /** * 返回客服接口方法实现类,以方便调用其各个接口. * diff --git a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java index e2230e2a8..cb2479c57 100644 --- a/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java +++ b/weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java @@ -43,6 +43,7 @@ import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; +import java.util.function.Function; import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*; @@ -156,6 +157,10 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH @Setter private WxMpFreePublishService freePublishService = new WxMpFreePublishServiceImpl(this); + @Getter + @Setter + private Function configStorageFunction; + private Map configStorageMap = new HashMap<>(); private int retrySleepMillis = 1000; @@ -575,21 +580,43 @@ public abstract class BaseWxMpServiceImpl implements WxMpService, RequestH @Override public WxMpService switchoverTo(String mpId) { + return switchoverTo(mpId, configStorageFunction); + } + + @Override + public WxMpService switchoverTo(String mpId, Function func) { if (this.configStorageMap.containsKey(mpId)) { WxMpConfigStorageHolder.set(mpId); return this; } - + if (func != null) { + WxMpConfigStorage storage = func.apply(mpId); + if (storage != null) { + this.addConfigStorage(mpId, storage); + return this; + } + } throw new WxRuntimeException(String.format("无法找到对应【%s】的公众号配置信息,请核实!", mpId)); } @Override public boolean switchover(String mpId) { + return switchover(mpId, configStorageFunction); + } + + @Override + public boolean switchover(String mpId, Function func) { if (this.configStorageMap.containsKey(mpId)) { WxMpConfigStorageHolder.set(mpId); return true; } - + if (func != null) { + WxMpConfigStorage storage = func.apply(mpId); + if (storage != null) { + this.addConfigStorage(mpId, storage); + return true; + } + } log.error("无法找到对应【{}】的公众号配置信息,请核实!", mpId); return false; }