mirror of
				https://gitee.com/binary/weixin-java-tools.git
				synced 2025-10-31 02:28:25 +08:00 
			
		
		
		
	🐛 #1828 修复企业微信第三方应用消息路由相关方法参数错误的问题
This commit is contained in:
		| @ -2,6 +2,7 @@ package me.chanjar.weixin.cp.tp.message; | |||||||
|  |  | ||||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||||
| import me.chanjar.weixin.common.session.WxSessionManager; | 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.WxCpXmlMessage; | ||||||
| import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | ||||||
| import me.chanjar.weixin.cp.tp.service.WxCpTpService; | import me.chanjar.weixin.cp.tp.service.WxCpTpService; | ||||||
| @ -25,7 +26,7 @@ public interface WxCpTpMessageHandler { | |||||||
|    * @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null |    * @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null | ||||||
|    * @throws WxErrorException the wx error exception |    * @throws WxErrorException the wx error exception | ||||||
|    */ |    */ | ||||||
|   WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage, |   WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage, | ||||||
|                            Map<String, Object> context, |                            Map<String, Object> context, | ||||||
|                            WxCpTpService wxCpService, |                            WxCpTpService wxCpService, | ||||||
|                            WxSessionManager sessionManager) throws WxErrorException; |                            WxSessionManager sessionManager) throws WxErrorException; | ||||||
|  | |||||||
| @ -2,6 +2,7 @@ package me.chanjar.weixin.cp.tp.message; | |||||||
|  |  | ||||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||||
| import me.chanjar.weixin.common.session.WxSessionManager; | 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.WxCpXmlMessage; | ||||||
| import me.chanjar.weixin.cp.tp.service.WxCpTpService; | import me.chanjar.weixin.cp.tp.service.WxCpTpService; | ||||||
|  |  | ||||||
| @ -24,7 +25,7 @@ public interface WxCpTpMessageInterceptor { | |||||||
|    * @return true代表OK ,false代表不OK |    * @return true代表OK ,false代表不OK | ||||||
|    * @throws WxErrorException the wx error exception |    * @throws WxErrorException the wx error exception | ||||||
|    */ |    */ | ||||||
|   boolean intercept(WxCpXmlMessage wxMessage, |   boolean intercept(WxCpTpXmlMessage wxMessage, | ||||||
|                     Map<String, Object> context, |                     Map<String, Object> context, | ||||||
|                     WxCpTpService wxCpService, |                     WxCpTpService wxCpService, | ||||||
|                     WxSessionManager sessionManager) throws WxErrorException; |                     WxSessionManager sessionManager) throws WxErrorException; | ||||||
|  | |||||||
| @ -9,6 +9,7 @@ import me.chanjar.weixin.common.session.InternalSession; | |||||||
| import me.chanjar.weixin.common.session.InternalSessionManager; | import me.chanjar.weixin.common.session.InternalSessionManager; | ||||||
| import me.chanjar.weixin.common.session.WxSessionManager; | import me.chanjar.weixin.common.session.WxSessionManager; | ||||||
| import me.chanjar.weixin.common.util.LogExceptionHandler; | 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.WxCpXmlMessage; | ||||||
| import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | ||||||
| import me.chanjar.weixin.cp.message.WxCpMessageRouterRule; | import me.chanjar.weixin.cp.message.WxCpMessageRouterRule; | ||||||
| @ -131,7 +132,7 @@ public class WxCpTpMessageRouter { | |||||||
|   /** |   /** | ||||||
|    * 处理微信消息. |    * 处理微信消息. | ||||||
|    */ |    */ | ||||||
|   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)) { |     if (isMsgDuplicated(wxMessage)) { | ||||||
|       // 如果是重复消息,那么就不做处理 |       // 如果是重复消息,那么就不做处理 | ||||||
|       return null; |       return null; | ||||||
| @ -165,7 +166,7 @@ public class WxCpTpMessageRouter { | |||||||
|       } else { |       } else { | ||||||
|         res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler); |         res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler); | ||||||
|         // 在同步操作结束,session访问结束 |         // 在同步操作结束,session访问结束 | ||||||
|         log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName()); |         log.debug("End session access: async=false, sessionId={}", wxMessage.getSuiteId()); | ||||||
|         sessionEndAccess(wxMessage); |         sessionEndAccess(wxMessage); | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
| @ -175,7 +176,7 @@ public class WxCpTpMessageRouter { | |||||||
|         for (Future future : futures) { |         for (Future future : futures) { | ||||||
|           try { |           try { | ||||||
|             future.get(); |             future.get(); | ||||||
|             log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName()); |             log.debug("End session access: async=true, sessionId={}", wxMessage.getSuiteId()); | ||||||
|             // 异步操作结束,session访问结束 |             // 异步操作结束,session访问结束 | ||||||
|             sessionEndAccess(wxMessage); |             sessionEndAccess(wxMessage); | ||||||
|           } catch (InterruptedException e) { |           } catch (InterruptedException e) { | ||||||
| @ -193,30 +194,22 @@ public class WxCpTpMessageRouter { | |||||||
|   /** |   /** | ||||||
|    * 处理微信消息. |    * 处理微信消息. | ||||||
|    */ |    */ | ||||||
|   public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) { |   public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage) { | ||||||
|     return this.route(wxMessage, new HashMap<>(2)); |     return this.route(wxMessage, new HashMap<>(2)); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) { |   private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) { | ||||||
|     StringBuilder messageId = new StringBuilder(); |     StringBuilder messageId = new StringBuilder(); | ||||||
|     if (wxMessage.getMsgId() == null) { |     if (StringUtils.isNotEmpty(wxMessage.getSuiteId())) { | ||||||
|       messageId.append(wxMessage.getCreateTime()) |       messageId.append("-").append(wxMessage.getSuiteId()); | ||||||
|         .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.getUserId())) { |     if (StringUtils.isNotEmpty(wxMessage.getInfoType())) { | ||||||
|       messageId.append("-").append(wxMessage.getUserId()); |       messageId.append("-").append(wxMessage.getInfoType()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     if (StringUtils.isNotEmpty(wxMessage.getChangeType())) { |     if (StringUtils.isNotEmpty(wxMessage.getTimeStamp())) { | ||||||
|       messageId.append("-").append(wxMessage.getChangeType()); |       messageId.append("-").append(wxMessage.getTimeStamp()); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     return this.messageDuplicateChecker.isDuplicate(messageId.toString()); |     return this.messageDuplicateChecker.isDuplicate(messageId.toString()); | ||||||
| @ -225,8 +218,8 @@ public class WxCpTpMessageRouter { | |||||||
|   /** |   /** | ||||||
|    * 对session的访问结束. |    * 对session的访问结束. | ||||||
|    */ |    */ | ||||||
|   private void sessionEndAccess(WxCpXmlMessage wxMessage) { |   private void sessionEndAccess(WxCpTpXmlMessage wxMessage) { | ||||||
|     InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName()); |     InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getSuiteId()); | ||||||
|     if (session != null) { |     if (session != null) { | ||||||
|       session.endAccess(); |       session.endAccess(); | ||||||
|     } |     } | ||||||
|  | |||||||
| @ -4,6 +4,7 @@ import lombok.Data; | |||||||
| import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | import me.chanjar.weixin.common.api.WxErrorExceptionHandler; | ||||||
| import me.chanjar.weixin.common.error.WxErrorException; | import me.chanjar.weixin.common.error.WxErrorException; | ||||||
| import me.chanjar.weixin.common.session.WxSessionManager; | 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.WxCpXmlMessage; | ||||||
| import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage; | ||||||
| import me.chanjar.weixin.cp.message.WxCpMessageMatcher; | import me.chanjar.weixin.cp.message.WxCpMessageMatcher; | ||||||
| @ -24,29 +25,17 @@ public class WxCpTpMessageRouterRule { | |||||||
|  |  | ||||||
|   private boolean async = true; |   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 WxCpMessageMatcher matcher; | ||||||
|  |  | ||||||
|   private boolean reEnter = false; |   private boolean reEnter = false; | ||||||
|  |  | ||||||
|   private Integer agentId; |  | ||||||
|  |  | ||||||
|   private List<WxCpTpMessageHandler> handlers = new ArrayList<>(); |   private List<WxCpTpMessageHandler> handlers = new ArrayList<>(); | ||||||
|  |  | ||||||
|   private List<WxCpTpMessageInterceptor> interceptors = 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. |    * Instantiates a new Wx cp message router rule. | ||||||
| @ -68,94 +57,6 @@ public class WxCpTpMessageRouterRule { | |||||||
|     return this; |     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,用在用户需要自定义更复杂的匹配规则的时候 |    * 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候 | ||||||
|    * |    * | ||||||
| @ -243,25 +144,15 @@ public class WxCpTpMessageRouterRule { | |||||||
|    * @param wxMessage the wx message |    * @param wxMessage the wx message | ||||||
|    * @return the boolean |    * @return the boolean | ||||||
|    */ |    */ | ||||||
|   protected boolean test(WxCpXmlMessage wxMessage) { |   protected boolean test(WxCpTpXmlMessage wxMessage) { | ||||||
|     return |     return | ||||||
|       (this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName())) |       (this.suiteId == null || this.suiteId.equals(wxMessage.getSuiteId())) | ||||||
|         && |         && | ||||||
|         (this.agentId == null || this.agentId.equals(wxMessage.getAgentId())) |         (this.infoType == null || this.infoType.equals(wxMessage.getInfoType())) | ||||||
|         && |         && | ||||||
|         (this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType())) |         (this.suiteTicket == null || this.suiteTicket.equalsIgnoreCase(wxMessage.getSuiteTicket())) | ||||||
|         && |         && | ||||||
|         (this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent())) |         (this.authCode == null || this.authCode.equalsIgnoreCase(wxMessage.getAuthCode())) | ||||||
|         && |  | ||||||
|         (this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey())) |  | ||||||
|         && |  | ||||||
|         (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)) |  | ||||||
|       ; |       ; | ||||||
|   } |   } | ||||||
|  |  | ||||||
| @ -275,12 +166,11 @@ public class WxCpTpMessageRouterRule { | |||||||
|    * @param exceptionHandler the exception handler |    * @param exceptionHandler the exception handler | ||||||
|    * @return true 代表继续执行别的router,false 代表停止执行别的router |    * @return true 代表继续执行别的router,false 代表停止执行别的router | ||||||
|    */ |    */ | ||||||
|   protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage, |   protected WxCpXmlOutMessage service(WxCpTpXmlMessage wxMessage, | ||||||
|                                       Map<String, Object> context, |                                       Map<String, Object> context, | ||||||
|                                       WxCpTpService wxCpService, |                                       WxCpTpService wxCpService, | ||||||
|                                       WxSessionManager sessionManager, |                                       WxSessionManager sessionManager, | ||||||
|                                       WxErrorExceptionHandler exceptionHandler) { |                                       WxErrorExceptionHandler exceptionHandler) { | ||||||
|  |  | ||||||
|     if (context == null) { |     if (context == null) { | ||||||
|       context = new HashMap<>(2); |       context = new HashMap<>(2); | ||||||
|     } |     } | ||||||
|  | |||||||
		Reference in New Issue
	
	Block a user
	 Binary Wang
					Binary Wang