1
0
mirror of synced 2025-12-23 18:48:00 +08:00

优化代码

This commit is contained in:
Binary Wang
2019-01-27 19:12:36 +08:00
parent 014fb28354
commit 343ed7a708
6 changed files with 262 additions and 251 deletions

View File

@@ -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);
}
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);
}
if (WxConsts.KefuMsgType.MPNEWS.equals(this.getMsgType())) {
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);
}
this.handleMsgType(messageJson);
return messageJson.toString();
}
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 (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 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
}
}
}
}

View File

@@ -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 {
/**
* <pre>
* 请使用
* {@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}
* </pre>
*
* @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
}
}
}
}

View File

@@ -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());

View File

@@ -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<WxCpMessage> {
@Override
public JsonElement serialize(WxCpMessage message, Type typeOfSrc, JsonSerializationContext context) {
JsonObject messageJson = new JsonObject();
messageJson.addProperty("agentid", message.getAgentId());
if (StringUtils.isNotBlank(message.getToUser())) {
messageJson.addProperty("touser", message.getToUser());
}
messageJson.addProperty("msgtype", message.getMsgType());
if (StringUtils.isNotBlank(message.getToParty())) {
messageJson.addProperty("toparty", message.getToParty());
}
if (StringUtils.isNotBlank(message.getToTag())) {
messageJson.addProperty("totag", message.getToTag());
}
if (WxConsts.KefuMsgType.TEXT.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("text", text);
}
if (WxConsts.KefuMsgType.MARKDOWN.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("content", message.getContent());
messageJson.add("markdown", text);
}
if (WxConsts.KefuMsgType.TEXTCARD.equals(message.getMsgType())) {
JsonObject text = new JsonObject();
text.addProperty("title", message.getTitle());
text.addProperty("description", message.getDescription());
text.addProperty("url", message.getUrl());
text.addProperty("btntxt", message.getBtnTxt());
messageJson.add("textcard", text);
}
if (WxConsts.KefuMsgType.IMAGE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("image", image);
}
if (WxConsts.KefuMsgType.FILE.equals(message.getMsgType())) {
JsonObject image = new JsonObject();
image.addProperty("media_id", message.getMediaId());
messageJson.add("file", image);
}
if (WxConsts.KefuMsgType.VOICE.equals(message.getMsgType())) {
JsonObject voice = new JsonObject();
voice.addProperty("media_id", message.getMediaId());
messageJson.add("voice", voice);
}
if (StringUtils.isNotBlank(message.getSafe())) {
messageJson.addProperty("safe", message.getSafe());
}
if (WxConsts.KefuMsgType.VIDEO.equals(message.getMsgType())) {
JsonObject video = new JsonObject();
video.addProperty("media_id", message.getMediaId());
video.addProperty("thumb_media_id", message.getThumbMediaId());
video.addProperty("title", message.getTitle());
video.addProperty("description", message.getDescription());
messageJson.add("video", video);
}
if (WxConsts.KefuMsgType.NEWS.equals(message.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (NewArticle 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);
}
newsJsonObject.add("articles", articleJsonArray);
messageJson.add("news", newsJsonObject);
}
if (WxConsts.KefuMsgType.MPNEWS.equals(message.getMsgType())) {
JsonObject newsJsonObject = new JsonObject();
if (message.getMediaId() != null) {
newsJsonObject.addProperty("media_id", message.getMediaId());
} else {
JsonArray articleJsonArray = new JsonArray();
for (MpnewsArticle article : message.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);
}
return messageJson;
}
}