mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-04-05 17:38:05 +08:00
🆕 #1267 小程序模块增加多账号切换功能支持
This commit is contained in:
parent
9145628c7a
commit
79f00925a7
@ -10,6 +10,8 @@ import me.chanjar.weixin.common.util.http.MediaUploadRequestExecutor;
|
|||||||
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
import me.chanjar.weixin.common.util.http.RequestExecutor;
|
||||||
import me.chanjar.weixin.common.util.http.RequestHttp;
|
import me.chanjar.weixin.common.util.http.RequestHttp;
|
||||||
|
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
*/
|
*/
|
||||||
@ -148,9 +150,56 @@ public interface WxMaService extends WxService {
|
|||||||
/**
|
/**
|
||||||
* 注入 {@link WxMaConfig} 的实现.
|
* 注入 {@link WxMaConfig} 的实现.
|
||||||
*
|
*
|
||||||
* @param wxConfigProvider config
|
* @param maConfig config
|
||||||
*/
|
*/
|
||||||
void setWxMaConfig(WxMaConfig wxConfigProvider);
|
void setWxMaConfig(WxMaConfig maConfig);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Map里 加入新的 {@link WxMaConfig},适用于动态添加新的微信公众号配置.
|
||||||
|
*
|
||||||
|
* @param miniappId 小程序标识
|
||||||
|
* @param configStorage 新的微信配置
|
||||||
|
*/
|
||||||
|
void addConfig(String miniappId, WxMaConfig configStorage);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 Map中 移除 {@link String miniappId} 所对应的 {@link WxMaConfig},适用于动态移除小程序配置.
|
||||||
|
*
|
||||||
|
* @param miniappId 对应小程序的标识
|
||||||
|
*/
|
||||||
|
void removeConfig(String miniappId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入多个 {@link WxMaConfig} 的实现. 并为每个 {@link WxMaConfig} 赋予不同的 {@link String mpId} 值
|
||||||
|
* 随机采用一个{@link String mpId}进行Http初始化操作
|
||||||
|
*
|
||||||
|
* @param configs WxMaConfig map
|
||||||
|
*/
|
||||||
|
void setMultiConfigs(Map<String, WxMaConfig> configs);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入多个 {@link WxMaConfig} 的实现. 并为每个 {@link WxMaConfig} 赋予不同的 {@link String label} 值
|
||||||
|
*
|
||||||
|
* @param configs WxMaConfig map
|
||||||
|
* @param defaultMiniappId 设置一个{@link WxMaConfig} 所对应的{@link String defaultMiniappId}进行Http初始化
|
||||||
|
*/
|
||||||
|
void setMultiConfigs(Map<String, WxMaConfig> configs, String defaultMiniappId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进行相应的公众号切换.
|
||||||
|
*
|
||||||
|
* @param mpId 公众号标识
|
||||||
|
* @return 切换是否成功
|
||||||
|
*/
|
||||||
|
boolean switchover(String mpId);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 进行相应的公众号切换.
|
||||||
|
*
|
||||||
|
* @param miniappId 小程序标识
|
||||||
|
* @return 切换成功,则返回当前对象,方便链式调用,否则抛出异常
|
||||||
|
*/
|
||||||
|
WxMaService switchoverTo(String miniappId);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回消息(客服消息和模版消息)发送接口方法实现类,以方便调用其各个接口.
|
* 返回消息(客服消息和模版消息)发送接口方法实现类,以方便调用其各个接口.
|
||||||
|
@ -3,15 +3,17 @@ package cn.binarywang.wx.miniapp.api.impl;
|
|||||||
import cn.binarywang.wx.miniapp.api.*;
|
import cn.binarywang.wx.miniapp.api.*;
|
||||||
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
||||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||||
|
import cn.binarywang.wx.miniapp.util.WxMaConfigHolder;
|
||||||
import com.google.common.base.Joiner;
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.collect.ImmutableMap;
|
import com.google.common.collect.ImmutableMap;
|
||||||
|
import com.google.common.collect.Maps;
|
||||||
import com.google.gson.Gson;
|
import com.google.gson.Gson;
|
||||||
import com.google.gson.JsonObject;
|
import com.google.gson.JsonObject;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import me.chanjar.weixin.common.api.WxImgProcService;
|
import me.chanjar.weixin.common.api.WxImgProcService;
|
||||||
import me.chanjar.weixin.common.enums.WxType;
|
|
||||||
import me.chanjar.weixin.common.api.WxOcrService;
|
import me.chanjar.weixin.common.api.WxOcrService;
|
||||||
import me.chanjar.weixin.common.bean.WxAccessToken;
|
import me.chanjar.weixin.common.bean.WxAccessToken;
|
||||||
|
import me.chanjar.weixin.common.enums.WxType;
|
||||||
import me.chanjar.weixin.common.error.WxError;
|
import me.chanjar.weixin.common.error.WxError;
|
||||||
import me.chanjar.weixin.common.error.WxErrorException;
|
import me.chanjar.weixin.common.error.WxErrorException;
|
||||||
import me.chanjar.weixin.common.util.DataUtils;
|
import me.chanjar.weixin.common.util.DataUtils;
|
||||||
@ -38,7 +40,7 @@ import static cn.binarywang.wx.miniapp.constant.WxMaConstants.ErrorCode.*;
|
|||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestHttp<H, P> {
|
public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestHttp<H, P> {
|
||||||
|
private Map<String, WxMaConfig> configMap;
|
||||||
private WxMaConfig wxMaConfig;
|
private WxMaConfig wxMaConfig;
|
||||||
|
|
||||||
private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
|
private final WxMaMsgService kefuService = new WxMaMsgServiceImpl(this);
|
||||||
@ -300,11 +302,74 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setWxMaConfig(WxMaConfig wxConfigProvider) {
|
public void setWxMaConfig(WxMaConfig maConfig) {
|
||||||
this.wxMaConfig = wxConfigProvider;
|
final String appid = maConfig.getAppid();
|
||||||
|
this.setMultiConfigs(ImmutableMap.of(appid, maConfig), appid);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMultiConfigs(Map<String, WxMaConfig> configs) {
|
||||||
|
this.setMultiConfigs(configs, configs.keySet().iterator().next());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setMultiConfigs(Map<String, WxMaConfig> configs, String defaultMiniappId) {
|
||||||
|
this.configMap = Maps.newHashMap(configs);
|
||||||
|
WxMaConfigHolder.set(defaultMiniappId);
|
||||||
this.initHttp();
|
this.initHttp();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addConfig(String mpId, WxMaConfig configStorages) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (this.configMap == null) {
|
||||||
|
this.setWxMaConfig(configStorages);
|
||||||
|
} else {
|
||||||
|
this.configMap.put(mpId, configStorages);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeConfig(String miniappId) {
|
||||||
|
synchronized (this) {
|
||||||
|
if (this.configMap.size() == 1) {
|
||||||
|
this.configMap.remove(miniappId);
|
||||||
|
log.warn("已删除最后一个小程序配置:{},须立即使用setWxMaConfig或setMultiConfigs添加配置", miniappId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (WxMaConfigHolder.get().equals(miniappId)) {
|
||||||
|
this.configMap.remove(miniappId);
|
||||||
|
final String defaultMpId = this.configMap.keySet().iterator().next();
|
||||||
|
WxMaConfigHolder.set(defaultMpId);
|
||||||
|
log.warn("已删除默认小程序配置,小程序【{}】被设为默认配置", defaultMpId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.configMap.remove(miniappId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public WxMaService switchoverTo(String miniappId) {
|
||||||
|
if (this.configMap.containsKey(miniappId)) {
|
||||||
|
WxMaConfigHolder.set(miniappId);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new RuntimeException(String.format("无法找到对应【%s】的小程序配置信息,请核实!", miniappId));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean switchover(String mpId) {
|
||||||
|
if (this.configMap.containsKey(mpId)) {
|
||||||
|
WxMaConfigHolder.set(mpId);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
log.error("无法找到对应【{}】的小程序配置信息,请核实!", mpId);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setRetrySleepMillis(int retrySleepMillis) {
|
public void setRetrySleepMillis(int retrySleepMillis) {
|
||||||
this.retrySleepMillis = retrySleepMillis;
|
this.retrySleepMillis = retrySleepMillis;
|
||||||
|
@ -0,0 +1,31 @@
|
|||||||
|
package cn.binarywang.wx.miniapp.util;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 小程序存储值存放类.
|
||||||
|
*
|
||||||
|
* @author <a href="https://github.com/binarywang">Binary Wang</a>
|
||||||
|
* @date 2020-08-16
|
||||||
|
*/
|
||||||
|
public class WxMaConfigHolder {
|
||||||
|
private final static ThreadLocal<String> THREAD_LOCAL = new ThreadLocal<String>() {
|
||||||
|
@Override
|
||||||
|
protected String initialValue() {
|
||||||
|
return "default";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public static String get() {
|
||||||
|
return THREAD_LOCAL.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void set(String label) {
|
||||||
|
THREAD_LOCAL.set(label);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此方法需要用户根据自己程序代码,在适当位置手动触发调用,本SDK里无法判断调用时机
|
||||||
|
*/
|
||||||
|
public static void remove() {
|
||||||
|
THREAD_LOCAL.remove();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user