Skip to content

Commit

Permalink
🐛 #1828 修复企业微信第三方应用消息路由相关方法参数错误的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Oct 31, 2020
1 parent c584cd0 commit aa26d6b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
Expand All @@ -25,7 +26,7 @@ public interface WxCpTpMessageHandler {
* @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null
* @throws WxErrorException the wx error exception
*/
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage,
Map<String, Object> context,
WxCpTpService wxCpService,
WxSessionManager sessionManager) throws WxErrorException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.tp.service.WxCpTpService;

Expand All @@ -24,7 +25,7 @@ public interface WxCpTpMessageInterceptor {
* @return true代表OK ,false代表不OK
* @throws WxErrorException the wx error exception
*/
boolean intercept(WxCpXmlMessage wxMessage,
boolean intercept(WxCpTpXmlMessage wxMessage,
Map<String, Object> context,
WxCpTpService wxCpService,
WxSessionManager sessionManager) throws WxErrorException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import me.chanjar.weixin.common.session.InternalSessionManager;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.common.util.LogExceptionHandler;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.message.WxCpMessageRouterRule;
Expand Down Expand Up @@ -131,7 +132,7 @@ public WxCpTpMessageRouterRule rule() {
/**
* 处理微信消息.
*/
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) {
public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage, final Map<String, Object> context) {
if (isMsgDuplicated(wxMessage)) {
// 如果是重复消息,那么就不做处理
return null;
Expand Down Expand Up @@ -165,7 +166,7 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
} else {
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
// 在同步操作结束,session访问结束
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
log.debug("End session access: async=false, sessionId={}", wxMessage.getSuiteId());
sessionEndAccess(wxMessage);
}
}
Expand All @@ -175,7 +176,7 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
for (Future future : futures) {
try {
future.get();
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
log.debug("End session access: async=true, sessionId={}", wxMessage.getSuiteId());
// 异步操作结束,session访问结束
sessionEndAccess(wxMessage);
} catch (InterruptedException e) {
Expand All @@ -193,30 +194,22 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
/**
* 处理微信消息.
*/
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage) {
return this.route(wxMessage, new HashMap<>(2));
}

private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) {
StringBuilder messageId = new StringBuilder();
if (wxMessage.getMsgId() == null) {
messageId.append(wxMessage.getCreateTime())
.append("-").append(StringUtils.trimToEmpty(String.valueOf(wxMessage.getAgentId())))
.append("-").append(wxMessage.getFromUserName())
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()))
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()));
} else {
messageId.append(wxMessage.getMsgId())
.append("-").append(wxMessage.getCreateTime())
.append("-").append(wxMessage.getFromUserName());
if (StringUtils.isNotEmpty(wxMessage.getSuiteId())) {
messageId.append("-").append(wxMessage.getSuiteId());
}

if (StringUtils.isNotEmpty(wxMessage.getUserId())) {
messageId.append("-").append(wxMessage.getUserId());
if (StringUtils.isNotEmpty(wxMessage.getInfoType())) {
messageId.append("-").append(wxMessage.getInfoType());
}

if (StringUtils.isNotEmpty(wxMessage.getChangeType())) {
messageId.append("-").append(wxMessage.getChangeType());
if (StringUtils.isNotEmpty(wxMessage.getTimeStamp())) {
messageId.append("-").append(wxMessage.getTimeStamp());
}

return this.messageDuplicateChecker.isDuplicate(messageId.toString());
Expand All @@ -225,8 +218,8 @@ private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
/**
* 对session的访问结束.
*/
private void sessionEndAccess(WxCpXmlMessage wxMessage) {
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName());
private void sessionEndAccess(WxCpTpXmlMessage wxMessage) {
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getSuiteId());
if (session != null) {
session.endAccess();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.common.session.WxSessionManager;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.message.WxCpMessageMatcher;
Expand All @@ -24,29 +25,17 @@ public class WxCpTpMessageRouterRule {

private boolean async = true;

private String fromUser;

private String msgType;

private String event;

private String eventKey;

private String eventKeyRegex;

private String content;

private String rContent;

private WxCpMessageMatcher matcher;

private boolean reEnter = false;

private Integer agentId;

private List<WxCpTpMessageHandler> handlers = new ArrayList<>();

private List<WxCpTpMessageInterceptor> interceptors = new ArrayList<>();
private String suiteId;
private String infoType;
private String authCode;
private String suiteTicket;

/**
* Instantiates a new Wx cp message router rule.
Expand All @@ -68,94 +57,6 @@ public WxCpTpMessageRouterRule async(boolean async) {
return this;
}

/**
* 如果agentId匹配
*
* @param agentId the agent id
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule agentId(Integer agentId) {
this.agentId = agentId;
return this;
}

/**
* 如果msgType等于某值
*
* @param msgType the msg type
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule msgType(String msgType) {
this.msgType = msgType;
return this;
}

/**
* 如果event等于某值
*
* @param event the event
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule event(String event) {
this.event = event;
return this;
}

/**
* 如果eventKey等于某值
*
* @param eventKey the event key
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule eventKey(String eventKey) {
this.eventKey = eventKey;
return this;
}

/**
* 如果eventKey匹配该正则表达式
*
* @param regex the regex
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule eventKeyRegex(String regex) {
this.eventKeyRegex = regex;
return this;
}

/**
* 如果content等于某值
*
* @param content the content
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule content(String content) {
this.content = content;
return this;
}

/**
* 如果content匹配该正则表达式
*
* @param regex the regex
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule rContent(String regex) {
this.rContent = regex;
return this;
}

/**
* 如果fromUser等于某值
*
* @param fromUser the from user
* @return the wx cp message router rule
*/
public WxCpTpMessageRouterRule fromUser(String fromUser) {
this.fromUser = fromUser;
return this;
}

/**
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
*
Expand Down Expand Up @@ -243,25 +144,15 @@ public WxCpTpMessageRouter next() {
* @param wxMessage the wx message
* @return the boolean
*/
protected boolean test(WxCpXmlMessage wxMessage) {
protected boolean test(WxCpTpXmlMessage wxMessage) {
return
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
&&
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
&&
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
(this.suiteId == null || this.suiteId.equals(wxMessage.getSuiteId()))
&&
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
(this.infoType == null || this.infoType.equals(wxMessage.getInfoType()))
&&
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
(this.suiteTicket == null || this.suiteTicket.equalsIgnoreCase(wxMessage.getSuiteTicket()))
&&
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
&&
(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))
(this.authCode == null || this.authCode.equalsIgnoreCase(wxMessage.getAuthCode()))
;
}

Expand All @@ -275,12 +166,11 @@ protected boolean test(WxCpXmlMessage wxMessage) {
* @param exceptionHandler the exception handler
* @return true 代表继续执行别的router,false 代表停止执行别的router
*/
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
protected WxCpXmlOutMessage service(WxCpTpXmlMessage wxMessage,
Map<String, Object> context,
WxCpTpService wxCpService,
WxSessionManager sessionManager,
WxErrorExceptionHandler exceptionHandler) {

if (context == null) {
context = new HashMap<>(2);
}
Expand Down

0 comments on commit aa26d6b

Please sign in to comment.