#959 公众号模块配置加入多公众号支持

This commit is contained in:
liaochuntao 2019-03-22 21:55:37 +08:00 committed by Binary Wang
parent f6285f049e
commit 53a4da00cd
5 changed files with 64 additions and 0 deletions

1
.gitignore vendored
View File

@ -42,6 +42,7 @@ Icon
.Spotlight-V100
.TemporaryItems
.Trashes
.vscode
.VolumeIcon.icns
.AppleDB
.AppleDesktop

3
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"java.configuration.updateBuildConfiguration": "interactive"
}

View File

@ -12,6 +12,8 @@ import me.chanjar.weixin.mp.bean.result.WxMpSemanticQueryResult;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import me.chanjar.weixin.mp.enums.TicketType;
import java.util.HashMap;
/**
* 微信公众号API的Service.
*
@ -306,6 +308,19 @@ public interface WxMpService {
*/
void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider);
/**
* 注入多个 {@link WxMpConfigStorage} 的实现. 并为每个 {@link WxMpConfigStorage} 赋予不同的 {@link String label}
* @return
*/
void setMultiWxMpConfigStorage(HashMap<String, WxMpConfigStorage> configStorages);
/**
* 进行相应的 WxApp 切换
* @param label
* @return
*/
boolean switchover(String label);
/**
* 返回客服接口方法实现类以方便调用其各个接口.
*

View File

@ -1,9 +1,11 @@
package me.chanjar.weixin.mp.api.impl;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.locks.Lock;
import me.chanjar.weixin.mp.api.*;
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -64,6 +66,9 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
private WxMpWifiService wifiService = new WxMpWifiServiceImpl(this);
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
private HashMap<String, WxMpConfigStorage> wxMpConfigStoragePool;
private boolean isMultiWxApp = false;
private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
@ -334,6 +339,10 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
@Override
public WxMpConfigStorage getWxMpConfigStorage() {
if (isMultiWxApp) {
String label = WxMpConfigStorageHolder.get();
return wxMpConfigStoragePool.getOrDefault(label, null);
}
return this.wxMpConfigStorage;
}
@ -343,6 +352,22 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
this.initHttp();
}
@Override
public void setMultiWxMpConfigStorage(HashMap<String, WxMpConfigStorage> configStorages) {
wxMpConfigStoragePool = configStorages;
isMultiWxApp = true;
this.initHttp();
}
@Override
public boolean switchover(String label) {
if (wxMpConfigStoragePool.containsKey(label)) {
WxMpConfigStorageHolder.set(label);
return true;
}
return false;
}
@Override
public void setRetrySleepMillis(int retrySleepMillis) {
this.retrySleepMillis = retrySleepMillis;

View File

@ -0,0 +1,20 @@
package me.chanjar.weixin.mp.util;
/**
* @Author: yd
* @Date: 2019-03-20 22:06
*/
public class WxMpConfigStorageHolder {
private final static ThreadLocal<String> WX_MP_CONFIG_STORAGE_CHOSE = new ThreadLocal<>();
public static String get() {
return WX_MP_CONFIG_STORAGE_CHOSE.get();
}
public static void set(String label) {
WX_MP_CONFIG_STORAGE_CHOSE.set(label);
}
}