Skip to content

Commit 90e1a9e

Browse files
authored
🎨 增加switchoverTo方法在mpId不存在时的Storage实例获取获取
1 parent bcc4839 commit 90e1a9e

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpService.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import me.chanjar.weixin.mp.enums.WxMpApiUrl;
2121

2222
import java.util.Map;
23+
import java.util.function.Function;
2324

2425
/**
2526
* 微信公众号API的Service.
@@ -393,6 +394,8 @@ public interface WxMpService extends WxService {
393394
*/
394395
boolean switchover(String mpId);
395396

397+
boolean switchover(String mpId, Function<String, WxMpConfigStorage> func);
398+
396399
/**
397400
* 进行相应的公众号切换.
398401
*
@@ -401,6 +404,8 @@ public interface WxMpService extends WxService {
401404
*/
402405
WxMpService switchoverTo(String mpId);
403406

407+
WxMpService switchoverTo(String mpId, Function<String, WxMpConfigStorage> func);
408+
404409
/**
405410
* 返回客服接口方法实现类,以方便调用其各个接口.
406411
*

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/impl/BaseWxMpServiceImpl.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import java.util.Map;
4444
import java.util.concurrent.TimeUnit;
4545
import java.util.concurrent.locks.Lock;
46+
import java.util.function.Function;
4647

4748
import static me.chanjar.weixin.mp.enums.WxMpApiUrl.Other.*;
4849

@@ -156,6 +157,10 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
156157
@Setter
157158
private WxMpFreePublishService freePublishService = new WxMpFreePublishServiceImpl(this);
158159

160+
@Getter
161+
@Setter
162+
private Function<String, WxMpConfigStorage> configStorageFunction;
163+
159164
private Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>();
160165

161166
private int retrySleepMillis = 1000;
@@ -575,21 +580,43 @@ public void removeConfigStorage(String mpId) {
575580

576581
@Override
577582
public WxMpService switchoverTo(String mpId) {
583+
return switchoverTo(mpId, configStorageFunction);
584+
}
585+
586+
@Override
587+
public WxMpService switchoverTo(String mpId, Function<String, WxMpConfigStorage> func) {
578588
if (this.configStorageMap.containsKey(mpId)) {
579589
WxMpConfigStorageHolder.set(mpId);
580590
return this;
581591
}
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+
}
583599
throw new WxRuntimeException(String.format("无法找到对应【%s】的公众号配置信息,请核实!", mpId));
584600
}
585601

586602
@Override
587603
public boolean switchover(String mpId) {
604+
return switchover(mpId, configStorageFunction);
605+
}
606+
607+
@Override
608+
public boolean switchover(String mpId, Function<String, WxMpConfigStorage> func) {
588609
if (this.configStorageMap.containsKey(mpId)) {
589610
WxMpConfigStorageHolder.set(mpId);
590611
return true;
591612
}
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+
}
593620
log.error("无法找到对应【{}】的公众号配置信息,请核实!", mpId);
594621
return false;
595622
}

0 commit comments

Comments
 (0)