Skip to content

Commit 22287a4

Browse files
committed
binarywang#281 消息路由器增加对EventKey正则表达式匹配的支持
1 parent c8c51a9 commit 22287a4

File tree

3 files changed

+37
-14
lines changed

3 files changed

+37
-14
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import me.chanjar.weixin.cp.api.WxCpService;
77
import me.chanjar.weixin.cp.bean.WxCpXmlMessage;
88
import me.chanjar.weixin.cp.bean.WxCpXmlOutMessage;
9+
import org.apache.commons.lang3.StringUtils;
910

1011
import java.util.ArrayList;
1112
import java.util.HashMap;
@@ -27,6 +28,8 @@ public class WxCpMessageRouterRule {
2728

2829
private String eventKey;
2930

31+
private String eventKeyRegex;
32+
3033
private String content;
3134

3235
private String rContent;
@@ -95,6 +98,14 @@ public WxCpMessageRouterRule eventKey(String eventKey) {
9598
return this;
9699
}
97100

101+
/**
102+
* 如果eventKey匹配该正则表达式
103+
*/
104+
public WxCpMessageRouterRule eventKeyRegex(String regex) {
105+
this.eventKeyRegex = regex;
106+
return this;
107+
}
108+
98109
/**
99110
* 如果content等于某值
100111
*
@@ -207,17 +218,17 @@ protected boolean test(WxCpXmlMessage wxMessage) {
207218
&&
208219
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
209220
&&
210-
(this.msgType == null || this.msgType.equals(wxMessage.getMsgType()))
221+
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
222+
&&
223+
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
211224
&&
212-
(this.event == null || this.event.equals(wxMessage.getEvent()))
225+
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
213226
&&
214-
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
227+
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
215228
&&
216-
(this.content == null || this.content
217-
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
229+
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
218230
&&
219-
(this.rContent == null || Pattern
220-
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
231+
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
221232
&&
222233
(this.matcher == null || this.matcher.match(wxMessage))
223234
;

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouterRule.java

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import me.chanjar.weixin.common.session.WxSessionManager;
66
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
77
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
8+
import org.apache.commons.lang3.StringUtils;
89

910
import java.util.ArrayList;
1011
import java.util.HashMap;
@@ -26,6 +27,8 @@ public class WxMpMessageRouterRule {
2627

2728
private String eventKey;
2829

30+
private String eventKeyRegex;
31+
2932
private String content;
3033

3134
private String rContent;
@@ -74,6 +77,14 @@ public WxMpMessageRouterRule eventKey(String eventKey) {
7477
return this;
7578
}
7679

80+
/**
81+
* 如果eventKey匹配该正则表达式
82+
*/
83+
public WxMpMessageRouterRule eventKeyRegex(String regex) {
84+
this.eventKeyRegex = regex;
85+
return this;
86+
}
87+
7788
/**
7889
* 如果content等于某值
7990
*/
@@ -170,17 +181,17 @@ protected boolean test(WxMpXmlMessage wxMessage) {
170181
return
171182
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUser()))
172183
&&
173-
(this.msgType == null || this.msgType.toLowerCase().equals((wxMessage.getMsgType() == null ? null : wxMessage.getMsgType().toLowerCase())))
184+
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
185+
&&
186+
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
174187
&&
175-
(this.event == null || this.event.toLowerCase().equals((wxMessage.getEvent() == null ? null : wxMessage.getEvent().toLowerCase())))
188+
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
176189
&&
177-
(this.eventKey == null || this.eventKey.toLowerCase().equals((wxMessage.getEventKey() == null ? null : wxMessage.getEventKey().toLowerCase())))
190+
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
178191
&&
179-
(this.content == null || this.content
180-
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
192+
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
181193
&&
182-
(this.rContent == null || Pattern
183-
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
194+
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
184195
&&
185196
(this.matcher == null || this.matcher.match(wxMessage))
186197
;

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMessageRouterTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public void prepare(boolean async, StringBuffer sb, WxMpMessageRouter router) {
3939
.rule().async(async).msgType(WxConsts.XML_MSG_TEXT).handler(new WxEchoMpMessageHandler(sb, WxConsts.XML_MSG_TEXT)).end()
4040
.rule().async(async).event(WxConsts.EVT_CLICK).handler(new WxEchoMpMessageHandler(sb, WxConsts.EVT_CLICK)).end()
4141
.rule().async(async).eventKey("KEY_1").handler(new WxEchoMpMessageHandler(sb, "KEY_1")).end()
42+
.rule().async(async).eventKeyRegex("KEY_1*").handler(new WxEchoMpMessageHandler(sb, "KEY_123")).end()
4243
.rule().async(async).content("CONTENT_1").handler(new WxEchoMpMessageHandler(sb, "CONTENT_1")).end()
4344
.rule().async(async).rContent(".*bc.*").handler(new WxEchoMpMessageHandler(sb, "abcd")).end()
4445
.rule().async(async).matcher(new WxMpMessageMatcher() {

0 commit comments

Comments
 (0)