mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-12-21 19:10:01 +08:00
添加客服消息接口
This commit is contained in:
24
src/main/java/chanjarster/weixin/util/AdapterCDATA.java
Normal file
24
src/main/java/chanjarster/weixin/util/AdapterCDATA.java
Normal file
@@ -0,0 +1,24 @@
|
||||
package chanjarster.weixin.util;
|
||||
|
||||
import javax.xml.bind.annotation.adapters.XmlAdapter;
|
||||
|
||||
/**
|
||||
*
|
||||
* http://stackoverflow.com/questions/14193944/jaxb-marshalling-unmarshalling-with-cdata
|
||||
*
|
||||
* @author chanjarster
|
||||
*
|
||||
*/
|
||||
public class AdapterCDATA extends XmlAdapter<String, String> {
|
||||
|
||||
@Override
|
||||
public String marshal(String arg0) throws Exception {
|
||||
return "<![CDATA[" + arg0 + "]]>";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String unmarshal(String arg0) throws Exception {
|
||||
return arg0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
|
||||
*
|
||||
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
|
||||
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
|
||||
* arose from modification of the original source, or other redistribution of this source
|
||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
|
||||
*/
|
||||
package chanjarster.weixin.util;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import chanjarster.weixin.out.WxCustomMessage;
|
||||
import chanjarster.weixin.out.WxCustomMessage.WxArticle;
|
||||
import chanjarster.weixin.service.WxMsgType;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author qianjia
|
||||
*
|
||||
*/
|
||||
public class WxCustomMessageGsonAdapter implements JsonSerializer<WxCustomMessage> {
|
||||
|
||||
public JsonElement serialize(WxCustomMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getTouser());
|
||||
messageJson.addProperty("msgtype", message.getMsgtype());
|
||||
|
||||
if (WxMsgType.TEXT.equals(message.getMsgtype())) {
|
||||
JsonObject text = new JsonObject();
|
||||
text.addProperty("content", message.getContent());
|
||||
messageJson.add("text", text);
|
||||
}
|
||||
|
||||
if (WxMsgType.IMAGE.equals(message.getMsgtype())) {
|
||||
JsonObject image = new JsonObject();
|
||||
image.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add("image", image);
|
||||
}
|
||||
|
||||
if (WxMsgType.VOICE.equals(message.getMsgtype())) {
|
||||
JsonObject voice = new JsonObject();
|
||||
voice.addProperty("media_id", message.getMedia_id());
|
||||
messageJson.add("voice", voice);
|
||||
}
|
||||
|
||||
if (WxMsgType.VIDEO.equals(message.getMsgtype())) {
|
||||
JsonObject video = new JsonObject();
|
||||
video.addProperty("media_id", message.getMedia_id());
|
||||
video.addProperty("thumb_media_id", message.getThumb_media_id());
|
||||
video.addProperty("title", message.getTitle());
|
||||
video.addProperty("description", message.getDescription());
|
||||
messageJson.add("video", video);
|
||||
}
|
||||
|
||||
if (WxMsgType.MUSIC.equals(message.getMsgtype())) {
|
||||
JsonObject music = new JsonObject();
|
||||
music.addProperty("title", message.getTitle());
|
||||
music.addProperty("description", message.getDescription());
|
||||
music.addProperty("thumb_media_id", message.getThumb_media_id());
|
||||
music.addProperty("musicurl", message.getMusicurl());
|
||||
music.addProperty("hqmusicurl", message.getHqmusicurl());
|
||||
messageJson.add("music", music);
|
||||
}
|
||||
|
||||
if (WxMsgType.NEWS.equals(message.getMsgtype())) {
|
||||
JsonArray articleJsonArray = new JsonArray();
|
||||
for (WxArticle article : message.getArticles()) {
|
||||
JsonObject articleJson = new JsonObject();
|
||||
articleJson.addProperty("title", article.getTitle());
|
||||
articleJson.addProperty("description", article.getDescription());
|
||||
articleJson.addProperty("url", article.getUrl());
|
||||
articleJson.addProperty("picurl", article.getPicurl());
|
||||
articleJsonArray.add(articleJson);
|
||||
}
|
||||
messageJson.add("articles", articleJsonArray);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
21
src/main/java/chanjarster/weixin/util/WxGsonBuilder.java
Normal file
21
src/main/java/chanjarster/weixin/util/WxGsonBuilder.java
Normal file
@@ -0,0 +1,21 @@
|
||||
package chanjarster.weixin.util;
|
||||
|
||||
import chanjarster.weixin.out.WxCustomMessage;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
|
||||
public class WxGsonBuilder {
|
||||
|
||||
public static final GsonBuilder INSTANCE = new GsonBuilder();
|
||||
|
||||
static {
|
||||
INSTANCE.disableHtmlEscaping();
|
||||
INSTANCE.registerTypeAdapter(WxCustomMessage.class, new WxCustomMessageGsonAdapter());
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
return INSTANCE.create();
|
||||
}
|
||||
|
||||
}
|
||||
55
src/main/java/chanjarster/weixin/util/WxMenuGsonAdapter.java
Normal file
55
src/main/java/chanjarster/weixin/util/WxMenuGsonAdapter.java
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* KINGSTAR MEDIA SOLUTIONS Co.,LTD. Copyright c 2005-2013. All rights reserved.
|
||||
*
|
||||
* This source code is the property of KINGSTAR MEDIA SOLUTIONS LTD. It is intended
|
||||
* only for the use of KINGSTAR MEDIA application development. Reengineering, reproduction
|
||||
* arose from modification of the original source, or other redistribution of this source
|
||||
* is not permitted without written permission of the KINGSTAR MEDIA SOLUTIONS LTD.
|
||||
*/
|
||||
package chanjarster.weixin.util;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import chanjarster.weixin.out.WxMenu;
|
||||
import chanjarster.weixin.out.WxMenu.WxMenuButton;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonObject;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author qianjia
|
||||
*
|
||||
*/
|
||||
public class WxMenuGsonAdapter implements JsonSerializer<WxMenu> {
|
||||
|
||||
public JsonElement serialize(WxMenu menu, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject json = new JsonObject();
|
||||
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenuButton button : menu.getButton()) {
|
||||
JsonObject buttonJson = serialize(button);
|
||||
buttonArray.add(buttonJson);
|
||||
}
|
||||
json.add("button", buttonArray);
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
protected JsonObject serialize(WxMenuButton button) {
|
||||
JsonObject buttonJson = new JsonObject();
|
||||
buttonJson.addProperty("name", button.getName());
|
||||
// TODO 其他字段
|
||||
if (button.getSub_button() == null || button.getSub_button().size() == 0) {
|
||||
JsonArray buttonArray = new JsonArray();
|
||||
for (WxMenuButton sub_button : button.getSub_button()) {
|
||||
buttonArray.add(serialize(sub_button));
|
||||
}
|
||||
buttonJson.add("sub_button", buttonArray);
|
||||
}
|
||||
return buttonJson;
|
||||
}
|
||||
}
|
||||
67
src/main/java/chanjarster/weixin/util/XmlTransformer.java
Normal file
67
src/main/java/chanjarster/weixin/util/XmlTransformer.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package chanjarster.weixin.util;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.StringReader;
|
||||
import java.io.StringWriter;
|
||||
import java.io.Writer;
|
||||
|
||||
import javax.xml.bind.JAXBContext;
|
||||
import javax.xml.bind.JAXBException;
|
||||
import javax.xml.bind.Marshaller;
|
||||
import javax.xml.bind.Unmarshaller;
|
||||
|
||||
import com.sun.xml.bind.marshaller.CharacterEscapeHandler;
|
||||
|
||||
public class XmlTransformer {
|
||||
|
||||
/**
|
||||
* xml -> pojo
|
||||
* @param clazz
|
||||
* @param object
|
||||
* @return
|
||||
* @throws JAXBException
|
||||
*/
|
||||
public static <T> T fromXml(Class<T> clazz, String xml) throws JAXBException {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
Unmarshaller um = context.createUnmarshaller();
|
||||
T object = (T) um.unmarshal(new StringReader(xml));
|
||||
return object;
|
||||
}
|
||||
|
||||
public static <T> T fromXml(Class<T> clazz, InputStream is) throws JAXBException {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
Unmarshaller um = context.createUnmarshaller();
|
||||
T object = (T) um.unmarshal(is);
|
||||
return object;
|
||||
}
|
||||
|
||||
/**
|
||||
* pojo -> xml
|
||||
* @param clazz
|
||||
* @return
|
||||
* @throws JAXBException
|
||||
*/
|
||||
public static <T> String toXml(Class<T> clazz, T object) throws JAXBException {
|
||||
StringWriter stringWriter = new StringWriter();
|
||||
toXml(clazz, object, stringWriter);
|
||||
return stringWriter.getBuffer().toString();
|
||||
}
|
||||
|
||||
public static <T> void toXml(Class<T> clazz, T object, Writer writer) throws JAXBException {
|
||||
JAXBContext context = JAXBContext.newInstance(clazz);
|
||||
Marshaller m = context.createMarshaller();
|
||||
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
|
||||
m.setProperty(CharacterEscapeHandler.class.getName(), characterUnescapeHandler);
|
||||
m.marshal(object, writer);
|
||||
}
|
||||
|
||||
protected static CharacterEscapeHandler characterUnescapeHandler = new CharacterUnescapeHandler();
|
||||
|
||||
protected static class CharacterUnescapeHandler implements CharacterEscapeHandler {
|
||||
public void escape(char[] ac, int i, int j, boolean flag, Writer writer) throws IOException {
|
||||
writer.write(ac, i, j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user