Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import me.chanjar.weixin.mp.enums.WxMpApiUrl;

import java.util.Map;
import java.util.function.Function;

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

boolean switchover(String mpId, Function<String, WxMpConfigStorage> func);

/**
* 进行相应的公众号切换.
*
Expand All @@ -401,6 +404,8 @@ public interface WxMpService extends WxService {
*/
WxMpService switchoverTo(String mpId);

WxMpService switchoverTo(String mpId, Function<String, WxMpConfigStorage> func);

/**
* 返回客服接口方法实现类,以方便调用其各个接口.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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.*;

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

@Getter
@Setter
private Function<String, WxMpConfigStorage> configStorageFunction;

private Map<String, WxMpConfigStorage> configStorageMap = new HashMap<>();

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

@Override
public WxMpService switchoverTo(String mpId) {
return switchoverTo(mpId, configStorageFunction);
}

@Override
public WxMpService switchoverTo(String mpId, Function<String, WxMpConfigStorage> 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<String, WxMpConfigStorage> 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;
}
Expand Down