mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-17 05:48:12 +08:00
refactor
This commit is contained in:
@@ -16,8 +16,9 @@ public interface WxMpMessageHandler {
|
||||
*
|
||||
* @param wxMessage
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxMpService
|
||||
* @return xml格式的消息,如果在异步规则里处理的话,可以返回null
|
||||
*/
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context);
|
||||
public WxMpXmlOutMessage handle(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService);
|
||||
|
||||
}
|
||||
|
||||
@@ -15,8 +15,9 @@ public interface WxMpMessageInterceptor {
|
||||
* 拦截微信消息
|
||||
* @param wxMessage
|
||||
* @param context 上下文,如果handler或interceptor之间有信息要传递,可以用这个
|
||||
* @param wxMpService
|
||||
* @return true代表OK,false代表不OK
|
||||
*/
|
||||
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context);
|
||||
public boolean intercept(WxMpXmlMessage wxMessage, Map<String, Object> context, WxMpService wxMpService);
|
||||
|
||||
}
|
||||
|
||||
@@ -43,14 +43,20 @@ public class WxMpMessageRouter {
|
||||
|
||||
private final List<Rule> rules = new ArrayList<Rule>();
|
||||
|
||||
private final ExecutorService es = Executors.newCachedThreadPool();
|
||||
|
||||
private final ExecutorService executorService = Executors.newCachedThreadPool();
|
||||
|
||||
private final WxMpService wxMpService;
|
||||
|
||||
public WxMpMessageRouter(WxMpService wxMpService) {
|
||||
this.wxMpService = wxMpService;
|
||||
}
|
||||
|
||||
/**
|
||||
* 开始一个新的Route规则
|
||||
* @return
|
||||
*/
|
||||
public Rule rule() {
|
||||
return new Rule(this);
|
||||
return new Rule(this, wxMpService);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,7 +79,7 @@ public class WxMpMessageRouter {
|
||||
if (matchRules.get(0).async) {
|
||||
// 只要第一个是异步的,那就异步执行
|
||||
// 在另一个线程里执行
|
||||
es.submit(new Runnable() {
|
||||
executorService.submit(new Runnable() {
|
||||
public void run() {
|
||||
for (final Rule rule : matchRules) {
|
||||
rule.service(wxMessage);
|
||||
@@ -101,6 +107,8 @@ public class WxMpMessageRouter {
|
||||
|
||||
private final WxMpMessageRouter routerBuilder;
|
||||
|
||||
private final WxMpService wxMpService;
|
||||
|
||||
private boolean async = true;
|
||||
|
||||
private String msgType;
|
||||
@@ -119,8 +127,9 @@ public class WxMpMessageRouter {
|
||||
|
||||
private List<WxMpMessageInterceptor> interceptors = new ArrayList<WxMpMessageInterceptor>();
|
||||
|
||||
protected Rule(WxMpMessageRouter routerBuilder) {
|
||||
protected Rule(WxMpMessageRouter routerBuilder, WxMpService wxMpService) {
|
||||
this.routerBuilder = routerBuilder;
|
||||
this.wxMpService = wxMpService;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -274,7 +283,7 @@ public class WxMpMessageRouter {
|
||||
Map<String, Object> context = new HashMap<String, Object>();
|
||||
// 如果拦截器不通过
|
||||
for (WxMpMessageInterceptor interceptor : this.interceptors) {
|
||||
if (!interceptor.intercept(wxMessage, context)) {
|
||||
if (!interceptor.intercept(wxMessage, context, wxMpService)) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -283,7 +292,7 @@ public class WxMpMessageRouter {
|
||||
WxMpXmlOutMessage res = null;
|
||||
for (WxMpMessageHandler handler : this.handlers) {
|
||||
// 返回最后handler的结果
|
||||
res = handler.handle(wxMessage, context);
|
||||
res = handler.handle(wxMessage, context, wxMpService);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import me.chanjar.weixin.common.bean.WxMenu;
|
||||
import me.chanjar.weixin.common.bean.result.WxError;
|
||||
import me.chanjar.weixin.common.bean.result.WxMediaUploadResult;
|
||||
import me.chanjar.weixin.common.exception.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.StringUtils;
|
||||
import me.chanjar.weixin.common.util.crypto.SHA1;
|
||||
import me.chanjar.weixin.common.util.fs.FileUtils;
|
||||
import me.chanjar.weixin.common.util.http.*;
|
||||
@@ -18,7 +19,6 @@ import me.chanjar.weixin.mp.bean.*;
|
||||
import me.chanjar.weixin.mp.bean.result.*;
|
||||
import me.chanjar.weixin.mp.util.http.QrCodeRequestExecutor;
|
||||
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.http.HttpHost;
|
||||
import org.apache.http.auth.AuthScope;
|
||||
import org.apache.http.auth.UsernamePasswordCredentials;
|
||||
@@ -27,7 +27,6 @@ import org.apache.http.client.CredentialsProvider;
|
||||
import org.apache.http.client.config.RequestConfig;
|
||||
import org.apache.http.client.methods.CloseableHttpResponse;
|
||||
import org.apache.http.client.methods.HttpGet;
|
||||
import org.apache.http.client.methods.HttpPost;
|
||||
import org.apache.http.impl.client.BasicCredentialsProvider;
|
||||
import org.apache.http.impl.client.BasicResponseHandler;
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
@@ -38,7 +37,6 @@ import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user