mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-02-18 06:06:23 +08:00
issue #77 添加自定义消息匹配器的支持
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package me.chanjar.weixin.mp.api;
|
||||
|
||||
import me.chanjar.weixin.mp.bean.WxMpXmlMessage;
|
||||
|
||||
/**
|
||||
* 消息匹配器,用在消息路由的时候
|
||||
*/
|
||||
public interface WxMpMessageMatcher {
|
||||
|
||||
/**
|
||||
* 消息是否匹配某种模式
|
||||
* @param message
|
||||
* @return
|
||||
*/
|
||||
public boolean match(WxMpXmlMessage message);
|
||||
|
||||
}
|
||||
@@ -225,7 +225,9 @@ public class WxMpMessageRouter {
|
||||
private String content;
|
||||
|
||||
private String rContent;
|
||||
|
||||
|
||||
private WxMpMessageMatcher matcher;
|
||||
|
||||
private boolean reEnter = false;
|
||||
|
||||
private List<WxMpMessageHandler> handlers = new ArrayList<WxMpMessageHandler>();
|
||||
@@ -306,6 +308,16 @@ public class WxMpMessageRouter {
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
|
||||
* @param matcher
|
||||
* @return
|
||||
*/
|
||||
public Rule matcher(WxMpMessageMatcher matcher) {
|
||||
this.matcher = matcher;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置微信消息拦截器
|
||||
* @param interceptor
|
||||
@@ -387,6 +399,8 @@ public class WxMpMessageRouter {
|
||||
(this.content == null || this.content.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
|
||||
&&
|
||||
(this.rContent == null || Pattern.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
|
||||
&&
|
||||
(this.matcher == null || this.matcher.match(wxMessage))
|
||||
;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user