Skip to content

Commit 2e4d73e

Browse files
jamy888binarywang
authored andcommitted
🆕 【企业微信】修改解析企业微信推送消息类,添加对企微客户联系变更回调事件的支持
1 parent 43594d7 commit 2e4d73e

6 files changed

Lines changed: 379 additions & 10 deletions

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlMessage.java

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,29 @@ public class WxCpXmlMessage implements Serializable {
138138
@XStreamConverter(value = XStreamCDataConverter.class)
139139
private String event;
140140

141+
@XStreamAlias("UpdateDetail")
142+
@XStreamConverter(value = XStreamCDataConverter.class)
143+
private String updateDetail;
144+
145+
@XStreamAlias("JoinScene")
146+
@XStreamConverter(value = XStreamCDataConverter.class)
147+
private String joinScene;
148+
149+
@XStreamAlias("QuitScene")
150+
@XStreamConverter(value = XStreamCDataConverter.class)
151+
private String quitScene;
152+
153+
@XStreamAlias("MemChangeCnt")
154+
@XStreamConverter(value = XStreamCDataConverter.class)
155+
private String memChangeCnt;
156+
157+
@XStreamAlias("Source")
158+
@XStreamConverter(value = XStreamCDataConverter.class)
159+
private String source;
160+
161+
@XStreamAlias("StrategyId")
162+
private String strategyId;
163+
141164
@XStreamAlias("EventKey")
142165
@XStreamConverter(value = XStreamCDataConverter.class)
143166
private String eventKey;
@@ -453,6 +476,14 @@ protected static WxCpXmlMessage fromXml(String xml) {
453476
return xmlMessage;
454477
}
455478

479+
public static WxCpXmlMessage fromXml(String xml, Integer agentId) {
480+
//修改微信变态的消息内容格式,方便解析
481+
xml = xml.replace("</PicList><PicList>", "");
482+
final WxCpXmlMessage xmlMessage = fromXml(xml);
483+
xmlMessage.setAgentId(agentId);
484+
return xmlMessage;
485+
}
486+
456487
protected static WxCpXmlMessage fromXml(InputStream is) {
457488
return XStreamTransformer.fromXml(WxCpXmlMessage.class, is);
458489
}
@@ -463,9 +494,15 @@ protected static WxCpXmlMessage fromXml(InputStream is) {
463494
public static WxCpXmlMessage fromEncryptedXml(String encryptedXml, WxCpConfigStorage wxCpConfigStorage,
464495
String timestamp, String nonce, String msgSignature) {
465496
WxCpCryptUtil cryptUtil = new WxCpCryptUtil(wxCpConfigStorage);
466-
String plainText = cryptUtil.decryptXml(msgSignature, timestamp, nonce, encryptedXml);
497+
WxCpXmlMessage wxCpXmlMessage = fromXml(encryptedXml);
498+
String plainText = cryptUtil.decrypt(msgSignature, timestamp, nonce, encryptedXml);
467499
log.debug("解密后的原始xml消息内容:{}", plainText);
468-
return fromXml(plainText);
500+
if (null != wxCpXmlMessage.getAgentId()) {
501+
return fromXml(plainText, wxCpXmlMessage.getAgentId());
502+
} else {
503+
return fromXml(plainText);
504+
}
505+
469506
}
470507

471508
public static WxCpXmlMessage fromEncryptedXml(InputStream is, WxCpConfigStorage wxCpConfigStorage,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package me.chanjar.weixin.cp.bean.message;
2+
3+
import com.thoughtworks.xstream.annotations.XStreamAlias;
4+
import com.thoughtworks.xstream.annotations.XStreamConverter;
5+
import lombok.Data;
6+
import lombok.EqualsAndHashCode;
7+
import me.chanjar.weixin.common.api.WxConsts;
8+
import me.chanjar.weixin.common.util.xml.*;
9+
10+
/**
11+
* @author eYoung
12+
* @description:
13+
* @date create at 2021/12/3 16:36
14+
*/
15+
@XStreamAlias("xml")
16+
@Data
17+
@EqualsAndHashCode(callSuper = false)
18+
public class WxCpXmlOutEventMessage extends WxCpXmlOutMessage {
19+
20+
@XStreamAlias("Event")
21+
@XStreamConverter(value = XStreamCDataConverter.class)
22+
private String event;
23+
24+
@XStreamAlias("ChatId")
25+
@XStreamConverter(value = XStreamCDataConverter.class)
26+
private String chatId;
27+
28+
@XStreamAlias("ChangeType")
29+
@XStreamConverter(value = XStreamCDataConverter.class)
30+
private String changeType;
31+
32+
@XStreamAlias("UpdateDetail")
33+
@XStreamConverter(value = XStreamCDataConverter.class)
34+
private String updateDetail;
35+
36+
@XStreamAlias("JoinScene")
37+
private String joinScene;
38+
39+
@XStreamAlias("QuitScene")
40+
private String quitScene;
41+
42+
@XStreamAlias("MemChangeCnt")
43+
private String memChangeCnt;
44+
45+
@XStreamAlias("TagType")
46+
@XStreamConverter(value = XStreamCDataConverter.class)
47+
private String tagType;
48+
49+
@XStreamAlias("StrategyId")
50+
private String strategyId;
51+
52+
@XStreamAlias("UserID")
53+
@XStreamConverter(value = XStreamCDataConverter.class)
54+
private String userID;
55+
56+
@XStreamAlias("ExternalUserID")
57+
@XStreamConverter(value = XStreamCDataConverter.class)
58+
private String externalUserID;
59+
60+
@XStreamAlias("State")
61+
@XStreamConverter(value = XStreamCDataConverter.class)
62+
private String state;
63+
64+
@XStreamAlias("WelcomeCode")
65+
@XStreamConverter(value = XStreamCDataConverter.class)
66+
private String welcomeCode;
67+
68+
@XStreamAlias("Source")
69+
@XStreamConverter(value = XStreamCDataConverter.class)
70+
private String source;
71+
72+
@XStreamAlias("FailReason")
73+
@XStreamConverter(value = XStreamCDataConverter.class)
74+
private String failReason;
75+
76+
@XStreamAlias("Id")
77+
@XStreamConverter(value = XStreamCDataConverter.class)
78+
private String id;
79+
80+
public WxCpXmlOutEventMessage() {
81+
this.msgType = WxConsts.XmlMsgType.EVENT;
82+
}
83+
}

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/message/WxCpXmlOutMessage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,13 @@ public static UpdateButtonBuilder UPDATE_BUTTON() {
8686
return new UpdateButtonBuilder();
8787
}
8888

89+
/**
90+
* 获得事件消息builder.
91+
*/
92+
public static EventBuilder EVENT() {
93+
return new EventBuilder();
94+
}
95+
8996
protected String toXml() {
9097
return XStreamTransformer.toXml((Class) this.getClass(), this);
9198
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package me.chanjar.weixin.cp.bean.outxmlbuilder;
2+
3+
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutEventMessage;
4+
5+
/**
6+
* @author eYoung
7+
* @description:
8+
* @date create at 2021/12/3 16:34
9+
*/
10+
public class EventBuilder extends BaseBuilder<EventBuilder, WxCpXmlOutEventMessage> {
11+
12+
private String event;
13+
private String chatId;
14+
private String changeType;
15+
private String updateDetail;
16+
private String joinScene;
17+
private String quitScene;
18+
private String memChangeCnt;
19+
private String tagType;
20+
private String strategyId;
21+
private String userID;
22+
private String externalUserID;
23+
private String state;
24+
private String welcomeCode;
25+
private String source;
26+
private String failReason;
27+
private String id;
28+
29+
public EventBuilder chatId(String chatId) {
30+
this.chatId = chatId;
31+
return this;
32+
}
33+
34+
public EventBuilder event(String event) {
35+
this.event = event;
36+
return this;
37+
}
38+
39+
public EventBuilder changeType(String changeType) {
40+
this.changeType = changeType;
41+
return this;
42+
}
43+
44+
public EventBuilder updateDetail(String updateDetail) {
45+
this.updateDetail = updateDetail;
46+
return this;
47+
}
48+
49+
public EventBuilder joinScene(String joinScene) {
50+
this.joinScene = joinScene;
51+
return this;
52+
}
53+
54+
public EventBuilder quitScene(String quitScene) {
55+
this.quitScene = quitScene;
56+
return this;
57+
}
58+
59+
public EventBuilder memChangeCnt(String memChangeCnt) {
60+
this.memChangeCnt = memChangeCnt;
61+
return this;
62+
}
63+
64+
public EventBuilder tagType(String tagType) {
65+
this.tagType = tagType;
66+
return this;
67+
}
68+
69+
public EventBuilder strategyId(String strategyId) {
70+
this.strategyId = strategyId;
71+
return this;
72+
}
73+
74+
public EventBuilder userID(String userID) {
75+
this.userID = userID;
76+
return this;
77+
}
78+
79+
public EventBuilder externalUserID(String externalUserID) {
80+
this.externalUserID = externalUserID;
81+
return this;
82+
}
83+
84+
public EventBuilder state(String state) {
85+
this.state = state;
86+
return this;
87+
}
88+
89+
public EventBuilder source(String source){
90+
this.source = source;
91+
return this;
92+
}
93+
94+
public EventBuilder welcomeCode(String welcomeCode) {
95+
this.welcomeCode = welcomeCode;
96+
return this;
97+
}
98+
99+
public EventBuilder failReason(String failReason){
100+
this.failReason = failReason;
101+
return this;
102+
}
103+
104+
public EventBuilder id(String id){
105+
this.id = id;
106+
return this;
107+
}
108+
109+
@Override
110+
public WxCpXmlOutEventMessage build() {
111+
WxCpXmlOutEventMessage m = new WxCpXmlOutEventMessage();
112+
super.setCommon(m);
113+
m.setEvent(this.event);
114+
m.setChatId(this.chatId);
115+
m.setChangeType(this.changeType);
116+
m.setUpdateDetail(this.updateDetail);
117+
m.setJoinScene(this.joinScene);
118+
m.setQuitScene(this.quitScene);
119+
m.setMemChangeCnt(this.memChangeCnt);
120+
m.setTagType(this.tagType);
121+
m.setStrategyId(this.strategyId);
122+
m.setUserID(this.userID);
123+
m.setExternalUserID(this.externalUserID);
124+
m.setState(this.state);
125+
m.setWelcomeCode(this.welcomeCode);
126+
m.setSource(this.source);
127+
m.setFailReason(this.failReason);
128+
m.setId(this.id);
129+
return m;
130+
}
131+
}

0 commit comments

Comments
 (0)