Преглед изворни кода

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

master
Binary Wang пре 7 година
родитељ
комит
22287a482d
3 измењених фајлова са 37 додато и 14 уклоњено
  1. +18
    -7
      weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java
  2. +18
    -7
      weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouterRule.java
  3. +1
    -0
      weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMessageRouterTest.java

+ 18
- 7
weixin-java-cp/src/main/java/me/chanjar/weixin/cp/message/WxCpMessageRouterRule.java Прегледај датотеку

@@ -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.equalsIgnoreCase(wxMessage.getEvent()))
&&
(this.event == null || this.event.equals(wxMessage.getEvent()))
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
&&
(this.eventKey == null || this.eventKey.equals(wxMessage.getEventKey()))
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(this.content == null || this.content
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
&&
(this.rContent == null || Pattern
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
&&
(this.matcher == null || this.matcher.match(wxMessage))
;


+ 18
- 7
weixin-java-mp/src/main/java/me/chanjar/weixin/mp/api/WxMpMessageRouterRule.java Прегледај датотеку

@@ -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.equalsIgnoreCase(wxMessage.getEvent()))
&&
(this.event == null || this.event.toLowerCase().equals((wxMessage.getEvent() == null ? null : wxMessage.getEvent().toLowerCase())))
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
&&
(this.eventKey == null || this.eventKey.toLowerCase().equals((wxMessage.getEventKey() == null ? null : wxMessage.getEventKey().toLowerCase())))
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(this.content == null || this.content
.equals(wxMessage.getContent() == null ? null : wxMessage.getContent().trim()))
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
&&
(this.rContent == null || Pattern
.matches(this.rContent, wxMessage.getContent() == null ? "" : wxMessage.getContent().trim()))
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
&&
(this.matcher == null || this.matcher.match(wxMessage))
;


+ 1
- 0
weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMessageRouterTest.java Прегледај датотеку

@@ -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() {


Loading…
Откажи
Сачувај