Skip to content

Commit 53a4da0

Browse files
chuntaojunbinarywang
authored andcommitted
#959 公众号模块配置加入多公众号支持
1 parent f6285f0 commit 53a4da0

File tree

5 files changed

+64
-0
lines changed

5 files changed

+64
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Icon
4242
.Spotlight-V100
4343
.TemporaryItems
4444
.Trashes
45+
.vscode
4546
.VolumeIcon.icns
4647
.AppleDB
4748
.AppleDesktop

.vscode/settings.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"java.configuration.updateBuildConfiguration": "interactive"
3+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
import me.chanjar.weixin.mp.bean.result.WxMpUser;
1313
import me.chanjar.weixin.mp.enums.TicketType;
1414

15+
import java.util.HashMap;
16+
1517
/**
1618
* 微信公众号API的Service.
1719
*
@@ -306,6 +308,19 @@ public interface WxMpService {
306308
*/
307309
void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider);
308310

311+
/**
312+
* 注入多个 {@link WxMpConfigStorage} 的实现. 并为每个 {@link WxMpConfigStorage} 赋予不同的 {@link String label} 值
313+
* @return
314+
*/
315+
void setMultiWxMpConfigStorage(HashMap<String, WxMpConfigStorage> configStorages);
316+
317+
/**
318+
* 进行相应的 WxApp 切换
319+
* @param label
320+
* @return
321+
*/
322+
boolean switchover(String label);
323+
309324
/**
310325
* 返回客服接口方法实现类,以方便调用其各个接口.
311326
*

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package me.chanjar.weixin.mp.api.impl;
22

33
import java.io.IOException;
4+
import java.util.HashMap;
45
import java.util.concurrent.locks.Lock;
56

67
import me.chanjar.weixin.mp.api.*;
8+
import me.chanjar.weixin.mp.util.WxMpConfigStorageHolder;
79
import org.apache.commons.lang3.StringUtils;
810
import org.slf4j.Logger;
911
import org.slf4j.LoggerFactory;
@@ -64,6 +66,9 @@ public abstract class BaseWxMpServiceImpl<H, P> implements WxMpService, RequestH
6466
private WxMpWifiService wifiService = new WxMpWifiServiceImpl(this);
6567
private WxMpMarketingService marketingService = new WxMpMarketingServiceImpl(this);
6668

69+
private HashMap<String, WxMpConfigStorage> wxMpConfigStoragePool;
70+
private boolean isMultiWxApp = false;
71+
6772
private int retrySleepMillis = 1000;
6873
private int maxRetryTimes = 5;
6974

@@ -334,6 +339,10 @@ public <T, E> T executeInternal(RequestExecutor<T, E> executor, String uri, E da
334339

335340
@Override
336341
public WxMpConfigStorage getWxMpConfigStorage() {
342+
if (isMultiWxApp) {
343+
String label = WxMpConfigStorageHolder.get();
344+
return wxMpConfigStoragePool.getOrDefault(label, null);
345+
}
337346
return this.wxMpConfigStorage;
338347
}
339348

@@ -343,6 +352,22 @@ public void setWxMpConfigStorage(WxMpConfigStorage wxConfigProvider) {
343352
this.initHttp();
344353
}
345354

355+
@Override
356+
public void setMultiWxMpConfigStorage(HashMap<String, WxMpConfigStorage> configStorages) {
357+
wxMpConfigStoragePool = configStorages;
358+
isMultiWxApp = true;
359+
this.initHttp();
360+
}
361+
362+
@Override
363+
public boolean switchover(String label) {
364+
if (wxMpConfigStoragePool.containsKey(label)) {
365+
WxMpConfigStorageHolder.set(label);
366+
return true;
367+
}
368+
return false;
369+
}
370+
346371
@Override
347372
public void setRetrySleepMillis(int retrySleepMillis) {
348373
this.retrySleepMillis = retrySleepMillis;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package me.chanjar.weixin.mp.util;
2+
3+
4+
/**
5+
* @Author: yd
6+
* @Date: 2019-03-20 22:06
7+
*/
8+
public class WxMpConfigStorageHolder {
9+
10+
private final static ThreadLocal<String> WX_MP_CONFIG_STORAGE_CHOSE = new ThreadLocal<>();
11+
12+
public static String get() {
13+
return WX_MP_CONFIG_STORAGE_CHOSE.get();
14+
}
15+
16+
public static void set(String label) {
17+
WX_MP_CONFIG_STORAGE_CHOSE.set(label);
18+
}
19+
20+
}

0 commit comments

Comments
 (0)