Skip to content

Commit aa26d6b

Browse files
committed
🐛 #1828 修复企业微信第三方应用消息路由相关方法参数错误的问题
1 parent c584cd0 commit aa26d6b

File tree

4 files changed

+29
-144
lines changed

4 files changed

+29
-144
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageHandler.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.common.session.WxSessionManager;
5+
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
56
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
67
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
78
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
@@ -25,7 +26,7 @@ public interface WxCpTpMessageHandler {
2526
* @return xml格式的消息 ,如果在异步规则里处理的话,可以返回null
2627
* @throws WxErrorException the wx error exception
2728
*/
28-
WxCpXmlOutMessage handle(WxCpXmlMessage wxMessage,
29+
WxCpXmlOutMessage handle(WxCpTpXmlMessage wxMessage,
2930
Map<String, Object> context,
3031
WxCpTpService wxCpService,
3132
WxSessionManager sessionManager) throws WxErrorException;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageInterceptor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import me.chanjar.weixin.common.error.WxErrorException;
44
import me.chanjar.weixin.common.session.WxSessionManager;
5+
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
56
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
67
import me.chanjar.weixin.cp.tp.service.WxCpTpService;
78

@@ -24,7 +25,7 @@ public interface WxCpTpMessageInterceptor {
2425
* @return true代表OK ,false代表不OK
2526
* @throws WxErrorException the wx error exception
2627
*/
27-
boolean intercept(WxCpXmlMessage wxMessage,
28+
boolean intercept(WxCpTpXmlMessage wxMessage,
2829
Map<String, Object> context,
2930
WxCpTpService wxCpService,
3031
WxSessionManager sessionManager) throws WxErrorException;

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouter.java

+14-21
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import me.chanjar.weixin.common.session.InternalSessionManager;
1010
import me.chanjar.weixin.common.session.WxSessionManager;
1111
import me.chanjar.weixin.common.util.LogExceptionHandler;
12+
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
1213
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
1314
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
1415
import me.chanjar.weixin.cp.message.WxCpMessageRouterRule;
@@ -131,7 +132,7 @@ public WxCpTpMessageRouterRule rule() {
131132
/**
132133
* 处理微信消息.
133134
*/
134-
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String, Object> context) {
135+
public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage, final Map<String, Object> context) {
135136
if (isMsgDuplicated(wxMessage)) {
136137
// 如果是重复消息,那么就不做处理
137138
return null;
@@ -165,7 +166,7 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
165166
} else {
166167
res = rule.service(wxMessage, context, this.wxCpService, this.sessionManager, this.exceptionHandler);
167168
// 在同步操作结束,session访问结束
168-
log.debug("End session access: async=false, sessionId={}", wxMessage.getFromUserName());
169+
log.debug("End session access: async=false, sessionId={}", wxMessage.getSuiteId());
169170
sessionEndAccess(wxMessage);
170171
}
171172
}
@@ -175,7 +176,7 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
175176
for (Future future : futures) {
176177
try {
177178
future.get();
178-
log.debug("End session access: async=true, sessionId={}", wxMessage.getFromUserName());
179+
log.debug("End session access: async=true, sessionId={}", wxMessage.getSuiteId());
179180
// 异步操作结束,session访问结束
180181
sessionEndAccess(wxMessage);
181182
} catch (InterruptedException e) {
@@ -193,30 +194,22 @@ public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage, final Map<String,
193194
/**
194195
* 处理微信消息.
195196
*/
196-
public WxCpXmlOutMessage route(final WxCpXmlMessage wxMessage) {
197+
public WxCpXmlOutMessage route(final WxCpTpXmlMessage wxMessage) {
197198
return this.route(wxMessage, new HashMap<>(2));
198199
}
199200

200-
private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
201+
private boolean isMsgDuplicated(WxCpTpXmlMessage wxMessage) {
201202
StringBuilder messageId = new StringBuilder();
202-
if (wxMessage.getMsgId() == null) {
203-
messageId.append(wxMessage.getCreateTime())
204-
.append("-").append(StringUtils.trimToEmpty(String.valueOf(wxMessage.getAgentId())))
205-
.append("-").append(wxMessage.getFromUserName())
206-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEventKey()))
207-
.append("-").append(StringUtils.trimToEmpty(wxMessage.getEvent()));
208-
} else {
209-
messageId.append(wxMessage.getMsgId())
210-
.append("-").append(wxMessage.getCreateTime())
211-
.append("-").append(wxMessage.getFromUserName());
203+
if (StringUtils.isNotEmpty(wxMessage.getSuiteId())) {
204+
messageId.append("-").append(wxMessage.getSuiteId());
212205
}
213206

214-
if (StringUtils.isNotEmpty(wxMessage.getUserId())) {
215-
messageId.append("-").append(wxMessage.getUserId());
207+
if (StringUtils.isNotEmpty(wxMessage.getInfoType())) {
208+
messageId.append("-").append(wxMessage.getInfoType());
216209
}
217210

218-
if (StringUtils.isNotEmpty(wxMessage.getChangeType())) {
219-
messageId.append("-").append(wxMessage.getChangeType());
211+
if (StringUtils.isNotEmpty(wxMessage.getTimeStamp())) {
212+
messageId.append("-").append(wxMessage.getTimeStamp());
220213
}
221214

222215
return this.messageDuplicateChecker.isDuplicate(messageId.toString());
@@ -225,8 +218,8 @@ private boolean isMsgDuplicated(WxCpXmlMessage wxMessage) {
225218
/**
226219
* 对session的访问结束.
227220
*/
228-
private void sessionEndAccess(WxCpXmlMessage wxMessage) {
229-
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getFromUserName());
221+
private void sessionEndAccess(WxCpTpXmlMessage wxMessage) {
222+
InternalSession session = ((InternalSessionManager) this.sessionManager).findSession(wxMessage.getSuiteId());
230223
if (session != null) {
231224
session.endAccess();
232225
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/tp/message/WxCpTpMessageRouterRule.java

+11-121
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import me.chanjar.weixin.common.api.WxErrorExceptionHandler;
55
import me.chanjar.weixin.common.error.WxErrorException;
66
import me.chanjar.weixin.common.session.WxSessionManager;
7+
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
78
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
89
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
910
import me.chanjar.weixin.cp.message.WxCpMessageMatcher;
@@ -24,29 +25,17 @@ public class WxCpTpMessageRouterRule {
2425

2526
private boolean async = true;
2627

27-
private String fromUser;
28-
29-
private String msgType;
30-
31-
private String event;
32-
33-
private String eventKey;
34-
35-
private String eventKeyRegex;
36-
37-
private String content;
38-
39-
private String rContent;
40-
4128
private WxCpMessageMatcher matcher;
4229

4330
private boolean reEnter = false;
4431

45-
private Integer agentId;
46-
4732
private List<WxCpTpMessageHandler> handlers = new ArrayList<>();
4833

4934
private List<WxCpTpMessageInterceptor> interceptors = new ArrayList<>();
35+
private String suiteId;
36+
private String infoType;
37+
private String authCode;
38+
private String suiteTicket;
5039

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

71-
/**
72-
* 如果agentId匹配
73-
*
74-
* @param agentId the agent id
75-
* @return the wx cp message router rule
76-
*/
77-
public WxCpTpMessageRouterRule agentId(Integer agentId) {
78-
this.agentId = agentId;
79-
return this;
80-
}
81-
82-
/**
83-
* 如果msgType等于某值
84-
*
85-
* @param msgType the msg type
86-
* @return the wx cp message router rule
87-
*/
88-
public WxCpTpMessageRouterRule msgType(String msgType) {
89-
this.msgType = msgType;
90-
return this;
91-
}
92-
93-
/**
94-
* 如果event等于某值
95-
*
96-
* @param event the event
97-
* @return the wx cp message router rule
98-
*/
99-
public WxCpTpMessageRouterRule event(String event) {
100-
this.event = event;
101-
return this;
102-
}
103-
104-
/**
105-
* 如果eventKey等于某值
106-
*
107-
* @param eventKey the event key
108-
* @return the wx cp message router rule
109-
*/
110-
public WxCpTpMessageRouterRule eventKey(String eventKey) {
111-
this.eventKey = eventKey;
112-
return this;
113-
}
114-
115-
/**
116-
* 如果eventKey匹配该正则表达式
117-
*
118-
* @param regex the regex
119-
* @return the wx cp message router rule
120-
*/
121-
public WxCpTpMessageRouterRule eventKeyRegex(String regex) {
122-
this.eventKeyRegex = regex;
123-
return this;
124-
}
125-
126-
/**
127-
* 如果content等于某值
128-
*
129-
* @param content the content
130-
* @return the wx cp message router rule
131-
*/
132-
public WxCpTpMessageRouterRule content(String content) {
133-
this.content = content;
134-
return this;
135-
}
136-
137-
/**
138-
* 如果content匹配该正则表达式
139-
*
140-
* @param regex the regex
141-
* @return the wx cp message router rule
142-
*/
143-
public WxCpTpMessageRouterRule rContent(String regex) {
144-
this.rContent = regex;
145-
return this;
146-
}
147-
148-
/**
149-
* 如果fromUser等于某值
150-
*
151-
* @param fromUser the from user
152-
* @return the wx cp message router rule
153-
*/
154-
public WxCpTpMessageRouterRule fromUser(String fromUser) {
155-
this.fromUser = fromUser;
156-
return this;
157-
}
158-
15960
/**
16061
* 如果消息匹配某个matcher,用在用户需要自定义更复杂的匹配规则的时候
16162
*
@@ -243,25 +144,15 @@ public WxCpTpMessageRouter next() {
243144
* @param wxMessage the wx message
244145
* @return the boolean
245146
*/
246-
protected boolean test(WxCpXmlMessage wxMessage) {
147+
protected boolean test(WxCpTpXmlMessage wxMessage) {
247148
return
248-
(this.fromUser == null || this.fromUser.equals(wxMessage.getFromUserName()))
249-
&&
250-
(this.agentId == null || this.agentId.equals(wxMessage.getAgentId()))
251-
&&
252-
(this.msgType == null || this.msgType.equalsIgnoreCase(wxMessage.getMsgType()))
149+
(this.suiteId == null || this.suiteId.equals(wxMessage.getSuiteId()))
253150
&&
254-
(this.event == null || this.event.equalsIgnoreCase(wxMessage.getEvent()))
151+
(this.infoType == null || this.infoType.equals(wxMessage.getInfoType()))
255152
&&
256-
(this.eventKey == null || this.eventKey.equalsIgnoreCase(wxMessage.getEventKey()))
153+
(this.suiteTicket == null || this.suiteTicket.equalsIgnoreCase(wxMessage.getSuiteTicket()))
257154
&&
258-
(this.eventKeyRegex == null || Pattern.matches(this.eventKeyRegex, StringUtils.trimToEmpty(wxMessage.getEventKey())))
259-
&&
260-
(this.content == null || this.content.equals(StringUtils.trimToNull(wxMessage.getContent())))
261-
&&
262-
(this.rContent == null || Pattern.matches(this.rContent, StringUtils.trimToEmpty(wxMessage.getContent())))
263-
&&
264-
(this.matcher == null || this.matcher.match(wxMessage))
155+
(this.authCode == null || this.authCode.equalsIgnoreCase(wxMessage.getAuthCode()))
265156
;
266157
}
267158

@@ -275,12 +166,11 @@ protected boolean test(WxCpXmlMessage wxMessage) {
275166
* @param exceptionHandler the exception handler
276167
* @return true 代表继续执行别的router,false 代表停止执行别的router
277168
*/
278-
protected WxCpXmlOutMessage service(WxCpXmlMessage wxMessage,
169+
protected WxCpXmlOutMessage service(WxCpTpXmlMessage wxMessage,
279170
Map<String, Object> context,
280171
WxCpTpService wxCpService,
281172
WxSessionManager sessionManager,
282173
WxErrorExceptionHandler exceptionHandler) {
283-
284174
if (context == null) {
285175
context = new HashMap<>(2);
286176
}

0 commit comments

Comments
 (0)