|
|
@@ -1,4 +1,4 @@ |
|
|
|
package me.chanjar.weixin.enterprise.api; |
|
|
|
package me.chanjar.weixin.cp.api; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashMap; |
|
|
@@ -8,20 +8,20 @@ import java.util.concurrent.ExecutorService; |
|
|
|
import java.util.concurrent.Executors; |
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
import me.chanjar.weixin.enterprise.bean.WxCpXmlMessage; |
|
|
|
import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; |
|
|
|
import me.chanjar.weixin.cp.bean.WxCpXmlMessage; |
|
|
|
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage; |
|
|
|
|
|
|
|
/** |
|
|
|
* <pre> |
|
|
|
* 微信消息路由器,通过代码化的配置,把来自微信的消息交给handler处理 |
|
|
|
* |
|
|
|
* |
|
|
|
* 说明: |
|
|
|
* 1. 配置路由规则时要按照从细到粗的原则,否则可能消息可能会被提前处理 |
|
|
|
* 2. 默认情况下消息只会被处理一次,除非使用 {@link Rule#next()} |
|
|
|
* 3. 规则的结束必须用{@link Rule#end()}或者{@link Rule#next()},否则不会生效 |
|
|
|
* |
|
|
|
* |
|
|
|
* 使用方法: |
|
|
|
* WxMessageRouter router = new WxMessageRouter(); |
|
|
|
* WxCpMessageRouter router = new WxCpMessageRouter(); |
|
|
|
* router |
|
|
|
* .rule() |
|
|
|
* .msgType("MSG_TYPE").event("EVENT").eventKey("EVENT_KEY").content("CONTENT") |
|
|
@@ -31,20 +31,20 @@ import me.chanjar.weixin.enterprise.bean.WxCpXmlOutMessage; |
|
|
|
* // 另外一个匹配规则 |
|
|
|
* .end() |
|
|
|
* ; |
|
|
|
* |
|
|
|
* |
|
|
|
* // 将WxXmlMessage交给消息路由器 |
|
|
|
* router.route(message); |
|
|
|
* |
|
|
|
* |
|
|
|
* </pre> |
|
|
|
* @author Daniel Qian |
|
|
|
* |
|
|
|
*/ |
|
|
|
public class WxCpMessageRouter { |
|
|
|
|
|
|
|
|
|
|
|
private final List<Rule> rules = new ArrayList<Rule>(); |
|
|
|
|
|
|
|
private final ExecutorService es = Executors.newCachedThreadPool(); |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 开始一个新的Route规则 |
|
|
|
* @return |
|
|
@@ -65,11 +65,11 @@ public class WxCpMessageRouter { |
|
|
|
matchRules.add(rule); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (matchRules.size() == 0) { |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (matchRules.get(0).async) { |
|
|
|
// 只要第一个是异步的,那就异步执行 |
|
|
|
// 在另一个线程里执行 |
|
|
@@ -85,7 +85,7 @@ public class WxCpMessageRouter { |
|
|
|
}); |
|
|
|
return null; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
WxCpXmlOutMessage res = null; |
|
|
|
for (final Rule rule : matchRules) { |
|
|
|
// 返回最后一个匹配规则的结果 |
|
|
@@ -96,33 +96,35 @@ public class WxCpMessageRouter { |
|
|
|
} |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public static class Rule { |
|
|
|
|
|
|
|
|
|
|
|
private final WxCpMessageRouter routerBuilder; |
|
|
|
|
|
|
|
private boolean async = true; |
|
|
|
|
|
|
|
|
|
|
|
private String msgType; |
|
|
|
|
|
|
|
private String event; |
|
|
|
|
|
|
|
|
|
|
|
private String eventKey; |
|
|
|
|
|
|
|
|
|
|
|
private String content; |
|
|
|
|
|
|
|
|
|
|
|
private String rContent; |
|
|
|
|
|
|
|
|
|
|
|
private boolean reEnter = false; |
|
|
|
|
|
|
|
|
|
|
|
private Integer agentId; |
|
|
|
|
|
|
|
private List<WxCpMessageHandler> handlers = new ArrayList<WxCpMessageHandler>(); |
|
|
|
|
|
|
|
|
|
|
|
private List<WxCpMessageInterceptor> interceptors = new ArrayList<WxCpMessageInterceptor>(); |
|
|
|
|
|
|
|
|
|
|
|
protected Rule(WxCpMessageRouter routerBuilder) { |
|
|
|
this.routerBuilder = routerBuilder; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 设置是否异步执行,默认是true |
|
|
|
* @param async |
|
|
@@ -132,7 +134,17 @@ public class WxCpMessageRouter { |
|
|
|
this.async = async; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 如果agentId匹配 |
|
|
|
* @param agentId |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
public Rule agentId(Integer agentId) { |
|
|
|
this.agentId = agentId; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 如果msgType等于某值 |
|
|
|
* @param msgType |
|
|
@@ -142,7 +154,7 @@ public class WxCpMessageRouter { |
|
|
|
this.msgType = msgType; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 如果event等于某值 |
|
|
|
* @param event |
|
|
@@ -152,7 +164,7 @@ public class WxCpMessageRouter { |
|
|
|
this.event = event; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 如果eventKey等于某值 |
|
|
|
* @param eventKey |
|
|
@@ -162,7 +174,7 @@ public class WxCpMessageRouter { |
|
|
|
this.eventKey = eventKey; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 如果content等于某值 |
|
|
|
* @param content |
|
|
@@ -172,7 +184,7 @@ public class WxCpMessageRouter { |
|
|
|
this.content = content; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 如果content匹配该正则表达式 |
|
|
|
* @param regex |
|
|
@@ -182,7 +194,7 @@ public class WxCpMessageRouter { |
|
|
|
this.rContent = regex; |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 设置微信消息拦截器 |
|
|
|
* @param interceptor |
|
|
@@ -191,7 +203,7 @@ public class WxCpMessageRouter { |
|
|
|
public Rule interceptor(WxCpMessageInterceptor interceptor) { |
|
|
|
return interceptor(interceptor, (WxCpMessageInterceptor[]) null); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 设置微信消息拦截器 |
|
|
|
* @param interceptor |
|
|
@@ -207,7 +219,7 @@ public class WxCpMessageRouter { |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 设置微信消息处理器 |
|
|
|
* @param handler |
|
|
@@ -216,7 +228,7 @@ public class WxCpMessageRouter { |
|
|
|
public Rule handler(WxCpMessageHandler handler) { |
|
|
|
return handler(handler, (WxCpMessageHandler[]) null); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 设置微信消息处理器 |
|
|
|
* @param handler |
|
|
@@ -232,7 +244,7 @@ public class WxCpMessageRouter { |
|
|
|
} |
|
|
|
return this; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 规则结束,代表如果一个消息匹配该规则,那么它将不再会进入其他规则 |
|
|
|
* @return |
|
|
@@ -241,7 +253,7 @@ public class WxCpMessageRouter { |
|
|
|
this.routerBuilder.rules.add(this); |
|
|
|
return this.routerBuilder; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 规则结束,但是消息还会进入其他规则 |
|
|
|
* @return |
|
|
@@ -250,9 +262,11 @@ public class WxCpMessageRouter { |
|
|
|
this.reEnter = true; |
|
|
|
return end(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected boolean test(WxCpXmlMessage wxMessage) { |
|
|
|
return |
|
|
|
return |
|
|
|
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId())) |
|
|
|
&& |
|
|
|
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType())) |
|
|
|
&& |
|
|
|
(this.event == null || this.event.equals(wxMessage.getEvent())) |
|
|
@@ -264,7 +278,7 @@ public class WxCpMessageRouter { |
|
|
|
(this.rContent == null || Pattern.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim())) |
|
|
|
; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 处理微信推送过来的消息 |
|
|
|
* @param wxMessage |
|
|
@@ -278,7 +292,7 @@ public class WxCpMessageRouter { |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 交给handler处理 |
|
|
|
WxCpXmlOutMessage res = null; |
|
|
|
for (WxCpMessageHandler handler : this.handlers) { |
|
|
@@ -287,7 +301,7 @@ public class WxCpMessageRouter { |
|
|
|
} |
|
|
|
return res; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |