#281 消息路由器增加对EventKey正则表达式匹配的支持

This commit is contained in:
Binary Wang 2017-07-15 18:05:00 +08:00
parent c8c51a9cee
commit 22287a482d
3 changed files with 37 additions and 14 deletions

View File

@ -6,6 +6,7 @@ import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.api.WxCpService;
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
@ -27,6 +28,8 @@ public class WxCpMessageRouterRule {
private String eventKey;
private String eventKeyRegex;
private String content;
private String rContent;
@ -95,6 +98,14 @@ public class WxCpMessageRouterRule {
return this;
}
/**
* 如果eventKey匹配该正则表达式
*/
public WxCpMessageRouterRule eventKeyRegex(String regex) {
this.eventKeyRegex = regex;
return this;
}
/**
* 如果content等于某值
*
@ -207,17 +218,17 @@ public class WxCpMessageRouterRule {
&&
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
&&
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
&&
(this.event == null || this.event.equals(wxMessage.getEvent()))
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
&&
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
&&
(this.content == null || this.content
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(this.rContent == null || Pattern
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
&&
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
&&
(this.matcher == null || this.matcher.match(wxMessage))
;

View File

@ -5,6 +5,7 @@ import me.chanjar.weixin.common.exception.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.HashMap;
@ -26,6 +27,8 @@ public class WxMpMessageRouterRule {
private String eventKey;
private String eventKeyRegex;
private String content;
private String rContent;
@ -74,6 +77,14 @@ public class WxMpMessageRouterRule {
return this;
}
/**
* 如果eventKey匹配该正则表达式
*/
public WxMpMessageRouterRule eventKeyRegex(String regex) {
this.eventKeyRegex = regex;
return this;
}
/**
* 如果content等于某值
*/
@ -170,17 +181,17 @@ public class WxMpMessageRouterRule {
return
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUser()))
&&
(this.msgType == null || this.msgType.toLowerCase().equals((wxMessage.getMsgType() == null ? null : wxMessage.getMsgType().toLowerCase())))
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
&&
(this.event == null || this.event.toLowerCase().equals((wxMessage.getEvent() == null ? null : wxMessage.getEvent().toLowerCase())))
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
&&
(this.eventKey == null || this.eventKey.toLowerCase().equals((wxMessage.getEventKey() == null ? null : wxMessage.getEventKey().toLowerCase())))
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
&&
(this.content == null || this.content
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(this.rContent == null || Pattern
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
&&
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
&&
(this.matcher == null || this.matcher.match(wxMessage))
;

View File

@ -39,6 +39,7 @@ public class WxMpMessageRouterTest {
.rule().async(async).msgType(WxConsts.XML_MSG_TEXT).handler(new WxEchoMpMessageHandler(sb, WxConsts.XML_MSG_TEXT)).end()
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMpMessageHandler(sb, WxConsts.EVT_CLICK)).end()
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMpMessageHandler(sb, "KEY_1")).end()
.rule().async(async).eventKeyRegex("KEY_1*").handler(new WxEchoMpMessageHandler(sb, "KEY_123")).end()
.rule().async(async).content("CONTENT_1").handler(new WxEchoMpMessageHandler(sb, "CONTENT_1")).end()
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMpMessageHandler(sb, "abcd")).end()
.rule().async(async).matcher(new WxMpMessageMatcher() {