diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java index 65a13bad..a3f9a62d 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpAppChatMessage.java @@ -9,8 +9,7 @@ import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.cp.WxCpConsts; +import me.chanjar.weixin.cp.WxCpConsts.AppChatMsgType; import me.chanjar.weixin.cp.bean.article.MpnewsArticle; import me.chanjar.weixin.cp.bean.article.NewArticle; @@ -46,7 +45,7 @@ public class WxCpAppChatMessage implements Serializable { */ public static WxCpAppChatMessage buildTextMsg(String chatId, String content, boolean safe) { final WxCpAppChatMessage message = new WxCpAppChatMessage(); - message.setMsgType(WxCpConsts.AppChatMsgType.TEXT); + message.setMsgType(AppChatMsgType.TEXT); message.setContent(content); message.setChatId(chatId); message.setSafe(safe); @@ -61,94 +60,104 @@ public class WxCpAppChatMessage implements Serializable { messageJson.addProperty("msgtype", this.getMsgType()); messageJson.addProperty("chatid", this.getChatId()); - if (WxConsts.KefuMsgType.TEXT.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", this.getContent()); - messageJson.add("text", text); - } - - if (WxConsts.KefuMsgType.MARKDOWN.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("content", this.getContent()); - messageJson.add("markdown", text); - } - - if (WxConsts.KefuMsgType.TEXTCARD.equals(this.getMsgType())) { - JsonObject text = new JsonObject(); - text.addProperty("title", this.getTitle()); - text.addProperty("description", this.getDescription()); - text.addProperty("url", this.getUrl()); - text.addProperty("btntxt", this.getBtnTxt()); - messageJson.add("textcard", text); - } - - if (WxConsts.KefuMsgType.IMAGE.equals(this.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", this.getMediaId()); - messageJson.add("image", image); - } - - if (WxConsts.KefuMsgType.FILE.equals(this.getMsgType())) { - JsonObject image = new JsonObject(); - image.addProperty("media_id", this.getMediaId()); - messageJson.add("file", image); - } - - if (WxConsts.KefuMsgType.VOICE.equals(this.getMsgType())) { - JsonObject voice = new JsonObject(); - voice.addProperty("media_id", this.getMediaId()); - messageJson.add("voice", voice); - } - if (this.getSafe() != null && this.getSafe()) { messageJson.addProperty("safe", 1); } - if (WxConsts.KefuMsgType.VIDEO.equals(this.getMsgType())) { - JsonObject video = new JsonObject(); - video.addProperty("media_id", this.getMediaId()); - video.addProperty("title", this.getTitle()); - video.addProperty("description", this.getDescription()); - messageJson.add("video", video); - } + this.handleMsgType(messageJson); - if (WxConsts.KefuMsgType.NEWS.equals(this.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - JsonArray articleJsonArray = new JsonArray(); - for (NewArticle article : this.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); - } - newsJsonObject.add("articles", articleJsonArray); - messageJson.add("news", newsJsonObject); - } + return messageJson.toString(); + } - if (WxConsts.KefuMsgType.MPNEWS.equals(this.getMsgType())) { - JsonObject newsJsonObject = new JsonObject(); - if (this.getMediaId() != null) { - newsJsonObject.addProperty("media_id", this.getMediaId()); - } else { + private void handleMsgType(JsonObject messageJson) { + switch (this.getMsgType()) { + case AppChatMsgType.TEXT: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("text", text); + break; + } + case AppChatMsgType.MARKDOWN: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("markdown", text); + break; + } + case AppChatMsgType.TEXTCARD: { + JsonObject text = new JsonObject(); + text.addProperty("title", this.getTitle()); + text.addProperty("description", this.getDescription()); + text.addProperty("url", this.getUrl()); + text.addProperty("btntxt", this.getBtnTxt()); + messageJson.add("textcard", text); + break; + } + case AppChatMsgType.IMAGE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("image", image); + break; + } + case AppChatMsgType.FILE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("file", image); + break; + } + case AppChatMsgType.VOICE: { + JsonObject voice = new JsonObject(); + voice.addProperty("media_id", this.getMediaId()); + messageJson.add("voice", voice); + break; + } + case AppChatMsgType.VIDEO: { + JsonObject video = new JsonObject(); + video.addProperty("media_id", this.getMediaId()); + video.addProperty("title", this.getTitle()); + video.addProperty("description", this.getDescription()); + messageJson.add("video", video); + break; + } + case AppChatMsgType.NEWS: { + JsonObject newsJsonObject = new JsonObject(); JsonArray articleJsonArray = new JsonArray(); - for (MpnewsArticle article : this.getMpnewsArticles()) { + for (NewArticle article : this.getArticles()) { JsonObject articleJson = new JsonObject(); articleJson.addProperty("title", article.getTitle()); - articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); - articleJson.addProperty("author", article.getAuthor()); - articleJson.addProperty("content_source_url", article.getContentSourceUrl()); - articleJson.addProperty("content", article.getContent()); - articleJson.addProperty("digest", article.getDigest()); + articleJson.addProperty("description", article.getDescription()); + articleJson.addProperty("url", article.getUrl()); + articleJson.addProperty("picurl", article.getPicUrl()); articleJsonArray.add(articleJson); } - newsJsonObject.add("articles", articleJsonArray); + messageJson.add("news", newsJsonObject); + break; + } + case AppChatMsgType.MPNEWS: { + JsonObject newsJsonObject = new JsonObject(); + if (this.getMediaId() != null) { + newsJsonObject.addProperty("media_id", this.getMediaId()); + } else { + JsonArray articleJsonArray = new JsonArray(); + for (MpnewsArticle article : this.getMpnewsArticles()) { + JsonObject articleJson = new JsonObject(); + articleJson.addProperty("title", article.getTitle()); + articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); + articleJson.addProperty("author", article.getAuthor()); + articleJson.addProperty("content_source_url", article.getContentSourceUrl()); + articleJson.addProperty("content", article.getContent()); + articleJson.addProperty("digest", article.getDigest()); + articleJsonArray.add(articleJson); + } + + newsJsonObject.add("articles", articleJsonArray); + } + messageJson.add("mpnews", newsJsonObject); + break; + } + default: { + //do nothing } - messageJson.add("mpnews", newsJsonObject); } - - return messageJson.toString(); } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java index cd455d88..df90775b 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/bean/WxCpMessage.java @@ -4,8 +4,12 @@ import java.io.Serializable; import java.util.ArrayList; import java.util.List; +import org.apache.commons.lang3.StringUtils; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; import lombok.Data; -import me.chanjar.weixin.common.api.WxConsts; +import me.chanjar.weixin.common.api.WxConsts.KefuMsgType; import me.chanjar.weixin.cp.bean.article.MpnewsArticle; import me.chanjar.weixin.cp.bean.article.NewArticle; import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder; @@ -17,7 +21,6 @@ import me.chanjar.weixin.cp.bean.messagebuilder.TextBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.TextCardBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder; import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder; -import me.chanjar.weixin.cp.util.json.WxCpGsonBuilder; /** * 消息. @@ -113,14 +116,14 @@ public class WxCpMessage implements Serializable { /** *
* 请使用 - * {@link WxConsts.KefuMsgType#TEXT} - * {@link WxConsts.KefuMsgType#IMAGE} - * {@link WxConsts.KefuMsgType#VOICE} - * {@link WxConsts.KefuMsgType#MUSIC} - * {@link WxConsts.KefuMsgType#VIDEO} - * {@link WxConsts.KefuMsgType#NEWS} - * {@link WxConsts.KefuMsgType#MPNEWS} - * {@link WxConsts.KefuMsgType#MARKDOWN} + * {@link KefuMsgType#TEXT} + * {@link KefuMsgType#IMAGE} + * {@link KefuMsgType#VOICE} + * {@link KefuMsgType#MUSIC} + * {@link KefuMsgType#VIDEO} + * {@link KefuMsgType#NEWS} + * {@link KefuMsgType#MPNEWS} + * {@link KefuMsgType#MARKDOWN} ** * @param msgType 消息类型 @@ -130,7 +133,126 @@ public class WxCpMessage implements Serializable { } public String toJson() { - return WxCpGsonBuilder.create().toJson(this); + JsonObject messageJson = new JsonObject(); + if (this.getAgentId() != null) { + messageJson.addProperty("agentid", this.getAgentId()); + } + + if (StringUtils.isNotBlank(this.getToUser())) { + messageJson.addProperty("touser", this.getToUser()); + } + + messageJson.addProperty("msgtype", this.getMsgType()); + + if (StringUtils.isNotBlank(this.getToParty())) { + messageJson.addProperty("toparty", this.getToParty()); + } + + if (StringUtils.isNotBlank(this.getToTag())) { + messageJson.addProperty("totag", this.getToTag()); + } + + this.handleMsgType(messageJson); + + if (StringUtils.isNotBlank(this.getSafe())) { + messageJson.addProperty("safe", this.getSafe()); + } + + return messageJson.toString(); + } + + private void handleMsgType(JsonObject messageJson) { + switch (this.getMsgType()) { + case KefuMsgType.TEXT: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("text", text); + break; + } + case KefuMsgType.MARKDOWN: { + JsonObject text = new JsonObject(); + text.addProperty("content", this.getContent()); + messageJson.add("markdown", text); + break; + } + case KefuMsgType.TEXTCARD: { + JsonObject text = new JsonObject(); + text.addProperty("title", this.getTitle()); + text.addProperty("description", this.getDescription()); + text.addProperty("url", this.getUrl()); + text.addProperty("btntxt", this.getBtnTxt()); + messageJson.add("textcard", text); + break; + } + case KefuMsgType.IMAGE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("image", image); + break; + } + case KefuMsgType.FILE: { + JsonObject image = new JsonObject(); + image.addProperty("media_id", this.getMediaId()); + messageJson.add("file", image); + break; + } + case KefuMsgType.VOICE: { + JsonObject voice = new JsonObject(); + voice.addProperty("media_id", this.getMediaId()); + messageJson.add("voice", voice); + break; + } + case KefuMsgType.VIDEO: { + JsonObject video = new JsonObject(); + video.addProperty("media_id", this.getMediaId()); + video.addProperty("thumb_media_id", this.getThumbMediaId()); + video.addProperty("title", this.getTitle()); + video.addProperty("description", this.getDescription()); + messageJson.add("video", video); + break; + } + case KefuMsgType.NEWS: { + JsonObject newsJsonObject = new JsonObject(); + JsonArray articleJsonArray = new JsonArray(); + for (NewArticle article : this.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); + } + newsJsonObject.add("articles", articleJsonArray); + messageJson.add("news", newsJsonObject); + break; + } + case KefuMsgType.MPNEWS: { + JsonObject newsJsonObject = new JsonObject(); + if (this.getMediaId() != null) { + newsJsonObject.addProperty("media_id", this.getMediaId()); + } else { + JsonArray articleJsonArray = new JsonArray(); + for (MpnewsArticle article : this.getMpnewsArticles()) { + JsonObject articleJson = new JsonObject(); + articleJson.addProperty("title", article.getTitle()); + articleJson.addProperty("thumb_media_id", article.getThumbMediaId()); + articleJson.addProperty("author", article.getAuthor()); + articleJson.addProperty("content_source_url", article.getContentSourceUrl()); + articleJson.addProperty("content", article.getContent()); + articleJson.addProperty("digest", article.getDigest()); + articleJson.addProperty("show_cover_pic", article.getShowCoverPic()); + articleJsonArray.add(articleJson); + } + + newsJsonObject.add("articles", articleJsonArray); + } + messageJson.add("mpnews", newsJsonObject); + break; + } + default: { + // do nothing + } + } } } diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java index 654d5511..16f0108e 100644 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java +++ b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpGsonBuilder.java @@ -7,7 +7,6 @@ import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.util.json.WxErrorAdapter; import me.chanjar.weixin.cp.bean.WxCpChat; import me.chanjar.weixin.cp.bean.WxCpDepart; -import me.chanjar.weixin.cp.bean.WxCpMessage; import me.chanjar.weixin.cp.bean.WxCpTag; import me.chanjar.weixin.cp.bean.WxCpUser; @@ -20,7 +19,6 @@ public class WxCpGsonBuilder { static { INSTANCE.disableHtmlEscaping(); - INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter()); INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter()); diff --git a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java b/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java deleted file mode 100644 index fbf70bb5..00000000 --- a/weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/json/WxCpMessageGsonAdapter.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * 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.cp.util.json; - -import java.lang.reflect.Type; - -import org.apache.commons.lang3.StringUtils; - -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; -import me.chanjar.weixin.common.api.WxConsts; -import me.chanjar.weixin.cp.bean.WxCpMessage; -import me.chanjar.weixin.cp.bean.article.MpnewsArticle; -import me.chanjar.weixin.cp.bean.article.NewArticle; - -/** - * @author Daniel Qian - */ -public class WxCpMessageGsonAdapter implements JsonSerializer