mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2026-03-10 00:13:40 +08:00
issue #24 模板消息接口
This commit is contained in:
@@ -22,6 +22,7 @@ public class WxMpGsonBuilder {
|
||||
INSTANCE.registerTypeAdapter(WxMpMassSendResult.class, new WxMpMassSendResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpMassUploadResult.class, new WxMpMassUploadResultAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpQrCodeTicket.class, new WxQrCodeTicketAdapter());
|
||||
INSTANCE.registerTypeAdapter(WxMpTemplateMessage.class, new WxMpTemplateMessageGsonAdapter());
|
||||
}
|
||||
|
||||
public static Gson create() {
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 me.chanjar.weixin.mp.util.json;
|
||||
|
||||
import com.google.gson.*;
|
||||
import me.chanjar.weixin.common.api.WxConsts;
|
||||
import me.chanjar.weixin.mp.bean.WxMpCustomMessage;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateData;
|
||||
import me.chanjar.weixin.mp.bean.WxMpTemplateMessage;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
/**
|
||||
* @author qianjia
|
||||
*/
|
||||
public class WxMpTemplateMessageGsonAdapter implements JsonSerializer<WxMpTemplateMessage> {
|
||||
|
||||
public JsonElement serialize(WxMpTemplateMessage message, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject messageJson = new JsonObject();
|
||||
messageJson.addProperty("touser", message.getToUser());
|
||||
messageJson.addProperty("template_id", message.getTemplateId());
|
||||
if (message.getUrl() != null) {
|
||||
messageJson.addProperty("url", message.getUrl());
|
||||
}
|
||||
if (message.getTopColor() != null) {
|
||||
messageJson.addProperty("topcolor", message.getTopColor());
|
||||
}
|
||||
|
||||
JsonObject datas = new JsonObject();
|
||||
messageJson.add("data", datas);
|
||||
|
||||
for (WxMpTemplateData data : message.getDatas()) {
|
||||
JsonObject dataJson = new JsonObject();
|
||||
dataJson.addProperty("value", data.getValue());
|
||||
if (data.getColor() != null) {
|
||||
dataJson.addProperty("color", data.getColor());
|
||||
}
|
||||
datas.add(data.getName(), dataJson);
|
||||
}
|
||||
|
||||
return messageJson;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user