mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-03 21:59:05 +08:00
优化代码
This commit is contained in:
@ -9,8 +9,7 @@ import lombok.AllArgsConstructor;
|
|||||||
import lombok.Builder;
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NoArgsConstructor;
|
import lombok.NoArgsConstructor;
|
||||||
import me.chanjar.weixin.common.api.WxConsts;
|
import me.chanjar.weixin.cp.WxCpConsts.AppChatMsgType;
|
||||||
import me.chanjar.weixin.cp.WxCpConsts;
|
|
||||||
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
||||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
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) {
|
public static WxCpAppChatMessage buildTextMsg(String chatId, String content, boolean safe) {
|
||||||
final WxCpAppChatMessage message = new WxCpAppChatMessage();
|
final WxCpAppChatMessage message = new WxCpAppChatMessage();
|
||||||
message.setMsgType(WxCpConsts.AppChatMsgType.TEXT);
|
message.setMsgType(AppChatMsgType.TEXT);
|
||||||
message.setContent(content);
|
message.setContent(content);
|
||||||
message.setChatId(chatId);
|
message.setChatId(chatId);
|
||||||
message.setSafe(safe);
|
message.setSafe(safe);
|
||||||
@ -61,58 +60,65 @@ public class WxCpAppChatMessage implements Serializable {
|
|||||||
messageJson.addProperty("msgtype", this.getMsgType());
|
messageJson.addProperty("msgtype", this.getMsgType());
|
||||||
messageJson.addProperty("chatid", this.getChatId());
|
messageJson.addProperty("chatid", this.getChatId());
|
||||||
|
|
||||||
if (WxConsts.KefuMsgType.TEXT.equals(this.getMsgType())) {
|
if (this.getSafe() != null && this.getSafe()) {
|
||||||
|
messageJson.addProperty("safe", 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.handleMsgType(messageJson);
|
||||||
|
|
||||||
|
return messageJson.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void handleMsgType(JsonObject messageJson) {
|
||||||
|
switch (this.getMsgType()) {
|
||||||
|
case AppChatMsgType.TEXT: {
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("content", this.getContent());
|
text.addProperty("content", this.getContent());
|
||||||
messageJson.add("text", text);
|
messageJson.add("text", text);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.MARKDOWN: {
|
||||||
if (WxConsts.KefuMsgType.MARKDOWN.equals(this.getMsgType())) {
|
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("content", this.getContent());
|
text.addProperty("content", this.getContent());
|
||||||
messageJson.add("markdown", text);
|
messageJson.add("markdown", text);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.TEXTCARD: {
|
||||||
if (WxConsts.KefuMsgType.TEXTCARD.equals(this.getMsgType())) {
|
|
||||||
JsonObject text = new JsonObject();
|
JsonObject text = new JsonObject();
|
||||||
text.addProperty("title", this.getTitle());
|
text.addProperty("title", this.getTitle());
|
||||||
text.addProperty("description", this.getDescription());
|
text.addProperty("description", this.getDescription());
|
||||||
text.addProperty("url", this.getUrl());
|
text.addProperty("url", this.getUrl());
|
||||||
text.addProperty("btntxt", this.getBtnTxt());
|
text.addProperty("btntxt", this.getBtnTxt());
|
||||||
messageJson.add("textcard", text);
|
messageJson.add("textcard", text);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.IMAGE: {
|
||||||
if (WxConsts.KefuMsgType.IMAGE.equals(this.getMsgType())) {
|
|
||||||
JsonObject image = new JsonObject();
|
JsonObject image = new JsonObject();
|
||||||
image.addProperty("media_id", this.getMediaId());
|
image.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("image", image);
|
messageJson.add("image", image);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.FILE: {
|
||||||
if (WxConsts.KefuMsgType.FILE.equals(this.getMsgType())) {
|
|
||||||
JsonObject image = new JsonObject();
|
JsonObject image = new JsonObject();
|
||||||
image.addProperty("media_id", this.getMediaId());
|
image.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("file", image);
|
messageJson.add("file", image);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.VOICE: {
|
||||||
if (WxConsts.KefuMsgType.VOICE.equals(this.getMsgType())) {
|
|
||||||
JsonObject voice = new JsonObject();
|
JsonObject voice = new JsonObject();
|
||||||
voice.addProperty("media_id", this.getMediaId());
|
voice.addProperty("media_id", this.getMediaId());
|
||||||
messageJson.add("voice", voice);
|
messageJson.add("voice", voice);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.VIDEO: {
|
||||||
if (this.getSafe() != null && this.getSafe()) {
|
|
||||||
messageJson.addProperty("safe", 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (WxConsts.KefuMsgType.VIDEO.equals(this.getMsgType())) {
|
|
||||||
JsonObject video = new JsonObject();
|
JsonObject video = new JsonObject();
|
||||||
video.addProperty("media_id", this.getMediaId());
|
video.addProperty("media_id", this.getMediaId());
|
||||||
video.addProperty("title", this.getTitle());
|
video.addProperty("title", this.getTitle());
|
||||||
video.addProperty("description", this.getDescription());
|
video.addProperty("description", this.getDescription());
|
||||||
messageJson.add("video", video);
|
messageJson.add("video", video);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.NEWS: {
|
||||||
if (WxConsts.KefuMsgType.NEWS.equals(this.getMsgType())) {
|
|
||||||
JsonObject newsJsonObject = new JsonObject();
|
JsonObject newsJsonObject = new JsonObject();
|
||||||
JsonArray articleJsonArray = new JsonArray();
|
JsonArray articleJsonArray = new JsonArray();
|
||||||
for (NewArticle article : this.getArticles()) {
|
for (NewArticle article : this.getArticles()) {
|
||||||
@ -125,9 +131,9 @@ public class WxCpAppChatMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
newsJsonObject.add("articles", articleJsonArray);
|
newsJsonObject.add("articles", articleJsonArray);
|
||||||
messageJson.add("news", newsJsonObject);
|
messageJson.add("news", newsJsonObject);
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
case AppChatMsgType.MPNEWS: {
|
||||||
if (WxConsts.KefuMsgType.MPNEWS.equals(this.getMsgType())) {
|
|
||||||
JsonObject newsJsonObject = new JsonObject();
|
JsonObject newsJsonObject = new JsonObject();
|
||||||
if (this.getMediaId() != null) {
|
if (this.getMediaId() != null) {
|
||||||
newsJsonObject.addProperty("media_id", this.getMediaId());
|
newsJsonObject.addProperty("media_id", this.getMediaId());
|
||||||
@ -147,8 +153,11 @@ public class WxCpAppChatMessage implements Serializable {
|
|||||||
newsJsonObject.add("articles", articleJsonArray);
|
newsJsonObject.add("articles", articleJsonArray);
|
||||||
}
|
}
|
||||||
messageJson.add("mpnews", newsJsonObject);
|
messageJson.add("mpnews", newsJsonObject);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default: {
|
||||||
|
//do nothing
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return messageJson.toString();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,8 +4,12 @@ import java.io.Serializable;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
|
||||||
|
import com.google.gson.JsonArray;
|
||||||
|
import com.google.gson.JsonObject;
|
||||||
import lombok.Data;
|
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.MpnewsArticle;
|
||||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||||
import me.chanjar.weixin.cp.bean.messagebuilder.FileBuilder;
|
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.TextCardBuilder;
|
||||||
import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder;
|
import me.chanjar.weixin.cp.bean.messagebuilder.VideoBuilder;
|
||||||
import me.chanjar.weixin.cp.bean.messagebuilder.VoiceBuilder;
|
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>
|
* <pre>
|
||||||
* 请使用
|
* 请使用
|
||||||
* {@link WxConsts.KefuMsgType#TEXT}
|
* {@link KefuMsgType#TEXT}
|
||||||
* {@link WxConsts.KefuMsgType#IMAGE}
|
* {@link KefuMsgType#IMAGE}
|
||||||
* {@link WxConsts.KefuMsgType#VOICE}
|
* {@link KefuMsgType#VOICE}
|
||||||
* {@link WxConsts.KefuMsgType#MUSIC}
|
* {@link KefuMsgType#MUSIC}
|
||||||
* {@link WxConsts.KefuMsgType#VIDEO}
|
* {@link KefuMsgType#VIDEO}
|
||||||
* {@link WxConsts.KefuMsgType#NEWS}
|
* {@link KefuMsgType#NEWS}
|
||||||
* {@link WxConsts.KefuMsgType#MPNEWS}
|
* {@link KefuMsgType#MPNEWS}
|
||||||
* {@link WxConsts.KefuMsgType#MARKDOWN}
|
* {@link KefuMsgType#MARKDOWN}
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param msgType 消息类型
|
* @param msgType 消息类型
|
||||||
@ -130,7 +133,126 @@ public class WxCpMessage implements Serializable {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public String toJson() {
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,7 +7,6 @@ import me.chanjar.weixin.common.error.WxError;
|
|||||||
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
|
import me.chanjar.weixin.common.util.json.WxErrorAdapter;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpChat;
|
import me.chanjar.weixin.cp.bean.WxCpChat;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpDepart;
|
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.WxCpTag;
|
||||||
import me.chanjar.weixin.cp.bean.WxCpUser;
|
import me.chanjar.weixin.cp.bean.WxCpUser;
|
||||||
|
|
||||||
@ -20,7 +19,6 @@ public class WxCpGsonBuilder {
|
|||||||
|
|
||||||
static {
|
static {
|
||||||
INSTANCE.disableHtmlEscaping();
|
INSTANCE.disableHtmlEscaping();
|
||||||
INSTANCE.registerTypeAdapter(WxCpMessage.class, new WxCpMessageGsonAdapter());
|
|
||||||
INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter());
|
INSTANCE.registerTypeAdapter(WxCpChat.class, new WxCpChatGsonAdapter());
|
||||||
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
|
INSTANCE.registerTypeAdapter(WxCpDepart.class, new WxCpDepartGsonAdapter());
|
||||||
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
|
INSTANCE.registerTypeAdapter(WxCpUser.class, new WxCpUserGsonAdapter());
|
||||||
|
|||||||
@ -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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@ -1,7 +1,7 @@
|
|||||||
package me.chanjar.weixin.cp.bean;
|
package me.chanjar.weixin.cp.bean;
|
||||||
|
|
||||||
import org.testng.Assert;
|
import org.testng.*;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.*;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Created by huansinho on 2018/4/13.
|
* Created by huansinho on 2018/4/13.
|
||||||
@ -10,7 +10,13 @@ import org.testng.annotations.Test;
|
|||||||
public class WxCpAgentTest {
|
public class WxCpAgentTest {
|
||||||
|
|
||||||
public void testDeserialize() {
|
public void testDeserialize() {
|
||||||
String json = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\",\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\",\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}, {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]},\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]},\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0,\"isreportenter\": 0,\"home_url\": \"\"}";
|
String json = "{\"errcode\": 0,\"errmsg\": \"ok\",\"agentid\": 9,\"name\": \"测试应用\"," +
|
||||||
|
"\"square_logo_url\": \"http://wx.qlogo.cn/mmhead/alksjf;lasdjf;lasjfuodiuj3rj2o34j/0\"," +
|
||||||
|
"\"description\": \"这是一个企业号应用\",\"allow_userinfos\": {\"user\": [{\"userid\": \"0009854\"}," +
|
||||||
|
" {\"userid\": \"1723\"}, {\"userid\": \"5625\"}]},\"allow_partys\": {\"partyid\": [42762742]}," +
|
||||||
|
"\"allow_tags\": {\"tagid\": [23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7]}," +
|
||||||
|
"\"close\": 0,\"redirect_domain\": \"weixin.com.cn\",\"report_location_flag\": 0," +
|
||||||
|
"\"isreportenter\": 0,\"home_url\": \"\"}";
|
||||||
|
|
||||||
WxCpAgent wxCpAgent = WxCpAgent.fromJson(json);
|
WxCpAgent wxCpAgent = WxCpAgent.fromJson(json);
|
||||||
|
|
||||||
@ -18,7 +24,8 @@ public class WxCpAgentTest {
|
|||||||
|
|
||||||
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
|
Assert.assertEquals(new Integer[]{42762742}, wxCpAgent.getAllowParties().getPartyIds().toArray());
|
||||||
|
|
||||||
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7}, wxCpAgent.getAllowTags().getTagIds().toArray());
|
Assert.assertEquals(new Integer[]{23, 22, 35, 19, 32, 125, 133, 46, 150, 38, 183, 9, 7},
|
||||||
|
wxCpAgent.getAllowTags().getTagIds().toArray());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,11 +1,11 @@
|
|||||||
package me.chanjar.weixin.cp.bean;
|
package me.chanjar.weixin.cp.bean;
|
||||||
|
|
||||||
|
import org.testng.annotations.*;
|
||||||
|
|
||||||
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
import me.chanjar.weixin.cp.bean.article.MpnewsArticle;
|
||||||
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
import me.chanjar.weixin.cp.bean.article.NewArticle;
|
||||||
import org.testng.annotations.Test;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.testng.Assert.assertEquals;
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public class WxCpMessageTest {
|
public class WxCpMessageTest {
|
||||||
@ -19,12 +19,16 @@ public class WxCpMessageTest {
|
|||||||
public void testTextCardBuild() {
|
public void testTextCardBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.TEXTCARD().toUser("OPENID")
|
WxCpMessage reply = WxCpMessage.TEXTCARD().toUser("OPENID")
|
||||||
.title("领奖通知")
|
.title("领奖通知")
|
||||||
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
|
.description("<div class=\"gray\">2016年9月26日</div> <div class=\"normal\">恭喜你抽中iPhone 7一台," +
|
||||||
|
"领奖码:xxxx</div><div class=\"highlight\">请于2016年10月10日前联系行政同事领取</div>")
|
||||||
.url("http://www.qq.com")
|
.url("http://www.qq.com")
|
||||||
.btnTxt("更多")
|
.btnTxt("更多")
|
||||||
.build();
|
.build();
|
||||||
assertThat(reply.toJson())
|
assertThat(reply.toJson())
|
||||||
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\",\"description\":\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\",\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}");
|
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"textcard\",\"textcard\":{\"title\":\"领奖通知\"," +
|
||||||
|
"\"description\":\"<div class=\\\"gray\\\">2016年9月26日</div> <div class=\\\"normal\\\">" +
|
||||||
|
"恭喜你抽中iPhone 7一台,领奖码:xxxx</div><div class=\\\"highlight\\\">请于2016年10月10日前联系行政同事领取</div>\"," +
|
||||||
|
"\"url\":\"http://www.qq.com\",\"btntxt\":\"更多\"},\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testImageBuild() {
|
public void testImageBuild() {
|
||||||
@ -40,9 +44,11 @@ public class WxCpMessageTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void testVideoBuild() {
|
public void testVideoBuild() {
|
||||||
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID").description("DESCRIPTION").build();
|
WxCpMessage reply = WxCpMessage.VIDEO().toUser("OPENID").title("TITLE").mediaId("MEDIA_ID").thumbMediaId("MEDIA_ID")
|
||||||
|
.description("DESCRIPTION").build();
|
||||||
assertThat(reply.toJson())
|
assertThat(reply.toJson())
|
||||||
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"safe\":\"0\",\"video\":{\"media_id\":\"MEDIA_ID\",\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"}}");
|
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"video\",\"video\":{\"media_id\":\"MEDIA_ID\"," +
|
||||||
|
"\"thumb_media_id\":\"MEDIA_ID\",\"title\":\"TITLE\",\"description\":\"DESCRIPTION\"},\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testNewsBuild() {
|
public void testNewsBuild() {
|
||||||
@ -61,7 +67,10 @@ public class WxCpMessageTest {
|
|||||||
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
WxCpMessage reply = WxCpMessage.NEWS().toUser("OPENID").addArticle(article1).addArticle(article2).build();
|
||||||
|
|
||||||
assertThat(reply.toJson())
|
assertThat(reply.toJson())
|
||||||
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"safe\":\"0\",\"news\":{\"articles\":[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"},{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}}");
|
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"news\",\"news\":{\"articles\":" +
|
||||||
|
"[{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}," +
|
||||||
|
"{\"title\":\"Happy Day\",\"description\":\"Is Really A Happy Day\",\"url\":\"URL\",\"picurl\":\"PIC_URL\"}]}," +
|
||||||
|
"\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMpnewsBuild_with_articles() {
|
public void testMpnewsBuild_with_articles() {
|
||||||
@ -88,14 +97,19 @@ public class WxCpMessageTest {
|
|||||||
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").addArticle(article1, article2).build();
|
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").addArticle(article1, article2).build();
|
||||||
|
|
||||||
assertThat(reply.toJson())
|
assertThat(reply.toJson())
|
||||||
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"safe\":\"0\",\"mpnews\":{\"articles\":[{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\",\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"},{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\",\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}]}}");
|
.isEqualTo( "{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"mpnews\":{\"articles\":" +
|
||||||
|
"[{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\"," +
|
||||||
|
"\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}" +
|
||||||
|
",{\"title\":\"Happy Day\",\"thumb_media_id\":\"thumb\",\"author\":\"aaaaaa\"," +
|
||||||
|
"\"content_source_url\":\"nice url\",\"content\":\"hahaha\",\"digest\":\"digest\",\"show_cover_pic\":\"heihei\"}]}," +
|
||||||
|
"\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testMpnewsBuild_with_media_id() {
|
public void testMpnewsBuild_with_media_id() {
|
||||||
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").mediaId("mmm").build();
|
WxCpMessage reply = WxCpMessage.MPNEWS().toUser("OPENID").mediaId("mmm").build();
|
||||||
|
|
||||||
assertThat(reply.toJson())
|
assertThat(reply.toJson())
|
||||||
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"safe\":\"0\",\"mpnews\":{\"media_id\":\"mmm\"}}");
|
.isEqualTo("{\"touser\":\"OPENID\",\"msgtype\":\"mpnews\",\"mpnews\":{\"media_id\":\"mmm\"},\"safe\":\"0\"}");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user