Skip to content

Commit

Permalink
🆕 binarywang#2142 【企业微信】被动回复消息内容新增任务卡片格式
Browse files Browse the repository at this point in the history
  • Loading branch information
arthur0201 authored and hywr committed Jun 23, 2021
1 parent 933e058 commit 1905871
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public static class XmlMsgType {
public static final String DEVICE_STATUS = "device_status";
public static final String HARDWARE = "hardware";
public static final String TRANSFER_CUSTOMER_SERVICE = "transfer_customer_service";
public static final String UPDATE_TASKCARD = "update_taskcard";
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ public HierarchicalStreamWriter createWriter(Writer out) {
private static final String SUFFIX_CDATA = "]]>";
private static final String PREFIX_MEDIA_ID = "<MediaId>";
private static final String SUFFIX_MEDIA_ID = "</MediaId>";
private static final String PREFIX_REPLACE_NAME = "<ReplaceName>";
private static final String SUFFIX_REPLACE_NAME = "</ReplaceName>";

@Override
protected void writeText(QuickWriter writer, String text) {
if (text.startsWith(PREFIX_CDATA) && text.endsWith(SUFFIX_CDATA)) {
writer.write(text);
} else if (text.startsWith(PREFIX_MEDIA_ID) && text.endsWith(SUFFIX_MEDIA_ID)) {
writer.write(text);
} else if (text.startsWith(PREFIX_REPLACE_NAME) && text.endsWith(SUFFIX_REPLACE_NAME)){
writer.write(text);
} else {
super.writeText(writer, text);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package me.chanjar.weixin.common.util.xml;

public class XStreamReplaceNameConverter extends XStreamCDataConverter {
@Override
public String toString(Object obj) {
return "<ReplaceName>" + super.toString(obj) + "</ReplaceName>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@
import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data;
import me.chanjar.weixin.common.util.xml.XStreamCDataConverter;
import me.chanjar.weixin.cp.bean.outxmlbuilder.ImageBuilder;
import me.chanjar.weixin.cp.bean.outxmlbuilder.NewsBuilder;
import me.chanjar.weixin.cp.bean.outxmlbuilder.TextBuilder;
import me.chanjar.weixin.cp.bean.outxmlbuilder.VideoBuilder;
import me.chanjar.weixin.cp.bean.outxmlbuilder.VoiceBuilder;
import me.chanjar.weixin.cp.bean.outxmlbuilder.*;
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
import me.chanjar.weixin.cp.util.crypto.WxCpCryptUtil;
import me.chanjar.weixin.cp.util.xml.XStreamTransformer;
Expand Down Expand Up @@ -76,6 +72,12 @@ public static NewsBuilder NEWS() {
return new NewsBuilder();
}

/**
* 获得任务卡片消息builder.
*/
public static TaskCardBuilder TASK_CARD() {
return new TaskCardBuilder();
}
protected String toXml() {
return XStreamTransformer.toXml((Class) this.getClass(), this);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package me.chanjar.weixin.cp.bean.message;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import lombok.Data;
import lombok.EqualsAndHashCode;
import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.common.util.xml.XStreamReplaceNameConverter;

@XStreamAlias("xml")
@Data
@EqualsAndHashCode(callSuper = false)
public class WxCpXmlOutTaskCardMessage extends WxCpXmlOutMessage {
private static final long serialVersionUID = 7028014900972827324L;

@XStreamAlias("TaskCard")
@XStreamConverter(value = XStreamReplaceNameConverter.class)
private String replaceName;

public WxCpXmlOutTaskCardMessage() {
this.msgType = WxConsts.XmlMsgType.UPDATE_TASKCARD;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.chanjar.weixin.cp.bean.outxmlbuilder;

import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTaskCardMessage;

/**
* 任务卡片消息builder
*
* @author tao zhang
*/
public final class TaskCardBuilder extends BaseBuilder<TaskCardBuilder, WxCpXmlOutTaskCardMessage> {

private String replaceName;

public TaskCardBuilder replaceName(String replaceName) {
this.replaceName = replaceName;
return this;
}

@Override
public WxCpXmlOutTaskCardMessage build() {
WxCpXmlOutTaskCardMessage m = new WxCpXmlOutTaskCardMessage();
setCommon(m);
m.setReplaceName(this.replaceName);
return m;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,8 @@

import com.thoughtworks.xstream.XStream;
import me.chanjar.weixin.common.util.xml.XStreamInitializer;
import me.chanjar.weixin.cp.bean.message.WxCpTpXmlMessage;
import me.chanjar.weixin.cp.bean.message.*;
import me.chanjar.weixin.cp.bean.WxCpTpXmlPackage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutImageMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutNewsMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTextMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutVideoMessage;
import me.chanjar.weixin.cp.bean.message.WxCpXmlOutVoiceMessage;

public class XStreamTransformer {

Expand Down Expand Up @@ -60,6 +53,7 @@ private static Map<Class, XStream> configXStreamInstance() {
map.put(WxCpXmlOutImageMessage.class, configWxCpXmlOutImageMessage());
map.put(WxCpXmlOutVideoMessage.class, configWxCpXmlOutVideoMessage());
map.put(WxCpXmlOutVoiceMessage.class, configWxCpXmlOutVoiceMessage());
map.put(WxCpXmlOutTaskCardMessage.class, configWxCpXmlOutTaskCardMessage());
map.put(WxCpTpXmlPackage.class, configWxCpTpXmlPackage());
map.put(WxCpTpXmlMessage.class, configWxCpTpXmlMessage());
return map;
Expand Down Expand Up @@ -118,6 +112,14 @@ private static XStream configWxCpXmlOutVoiceMessage() {
return xstream;
}

private static XStream configWxCpXmlOutTaskCardMessage() {
XStream xstream = XStreamInitializer.getInstance();

xstream.processAnnotations(WxCpXmlOutMessage.class);
xstream.processAnnotations(WxCpXmlOutTaskCardMessage.class);
return xstream;
}

private static XStream configWxCpTpXmlPackage() {
XStream xstream = XStreamInitializer.getInstance();
xstream.processAnnotations(WxCpTpXmlPackage.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package me.chanjar.weixin.cp.bean.message;

import me.chanjar.weixin.cp.bean.message.WxCpXmlOutTaskCardMessage;
import org.testng.Assert;
import org.testng.annotations.Test;

@Test
public class WxCpXmlOutTaskCardMessageTest {

public void test() {
WxCpXmlOutTaskCardMessage m = new WxCpXmlOutTaskCardMessage();
m.setReplaceName("已驳回");
m.setCreateTime(1122L);
m.setFromUserName("from");
m.setToUserName("to");

String expected = "<xml>"
+ "<ToUserName><![CDATA[to]]></ToUserName>"
+ "<FromUserName><![CDATA[from]]></FromUserName>"
+ "<CreateTime>1122</CreateTime>"
+ "<MsgType><![CDATA[update_taskcard]]></MsgType>"
+ "<TaskCard><ReplaceName><![CDATA[已驳回]]></ReplaceName></TaskCard>"
+ "</xml>";
System.out.println(m.toXml());
Assert.assertEquals(m.toXml().replaceAll("\\s", ""), expected.replaceAll("\\s", ""));
}

public void testBuild() {
WxCpXmlOutTaskCardMessage m = WxCpXmlOutMessage.TASK_CARD().replaceName("已驳回").fromUser("from").toUser("to").build();
String expected = "<xml>"
+ "<ToUserName><![CDATA[to]]></ToUserName>"
+ "<FromUserName><![CDATA[from]]></FromUserName>"
+ "<CreateTime>1122</CreateTime>"
+ "<MsgType><![CDATA[update_taskcard]]></MsgType>"
+ "<TaskCard><ReplaceName><![CDATA[已驳回]]></ReplaceName></TaskCard>"
+ "</xml>";
System.out.println(m.toXml());
Assert.assertEquals(
m
.toXml()
.replaceAll("\\s", "")
.replaceAll("<CreateTime>.*?</CreateTime>", ""),
expected
.replaceAll("\\s", "")
.replaceAll("<CreateTime>.*?</CreateTime>", "")
);
}

}

0 comments on commit 1905871

Please sign in to comment.