🎨 #1497 微信公众号群发图文消息接口参数优化,保持跟普通发送接口一致,减少重复代码

This commit is contained in:
Binary Wang
2020-04-12 11:23:13 +08:00
parent 1487ce3e29
commit 3af0a2beba
12 changed files with 130 additions and 247 deletions

View File

@@ -5,6 +5,7 @@ import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
/**
@@ -16,9 +17,9 @@ import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
public class WxMpMassNews implements Serializable {
private static final long serialVersionUID = 565937155013581016L;
private List<WxMpMassNewsArticle> articles = new ArrayList<>();
private List<WxMpNewsArticle> articles = new ArrayList<>();
public void addArticle(WxMpMassNewsArticle article) {
public void addArticle(WxMpNewsArticle article) {
this.articles.add(article);
}
@@ -35,54 +36,4 @@ public class WxMpMassNews implements Serializable {
return WxMpGsonBuilder.create().toJson(this);
}
/**
* <pre>
* 群发图文消息article.
* 1. thumbMediaId (必填) 图文消息缩略图的media_id可以在基础支持-上传多媒体文件接口中获得
* 2. author 图文消息的作者
* 3. title (必填) 图文消息的标题
* 4. contentSourceUrl 在图文消息页面点击“阅读原文”后的页面链接
* 5. content (必填) 图文消息页面的内容支持HTML标签
* 6. digest 图文消息的描述
* 7, showCoverPic 是否显示封面true为显示false为不显示
* </pre>
*
* @author chanjarster
*/
@Data
public static class WxMpMassNewsArticle {
/**
* (必填) 图文消息缩略图的media_id可以在基础支持-上传多媒体文件接口中获得.
*/
private String thumbMediaId;
/**
* 图文消息的作者.
*/
private String author;
/**
* (必填) 图文消息的标题.
*/
private String title;
/**
* 在图文消息页面点击“阅读原文”后的页面链接.
*/
private String contentSourceUrl;
/**
* (必填) 图文消息页面的内容支持HTML标签.
*/
private String content;
/**
* 图文消息的描述.
*/
private String digest;
/**
* 是否显示封面true为显示false为不显示.
*/
private boolean showCoverPic;
@Override
public String toString() {
return WxMpGsonBuilder.create().toJson(this);
}
}
}

View File

@@ -11,7 +11,7 @@ public class WxMpMaterialArticleUpdate implements Serializable {
private String mediaId;
private int index;
private WxMpMaterialNews.WxMpMaterialNewsArticle articles;
private WxMpNewsArticle articles;
public String toJson() {
return WxMpGsonBuilder.create().toJson(this);

View File

@@ -20,13 +20,13 @@ public class WxMpMaterialNews implements Serializable {
private Date createTime;
private Date updateTime;
private List<WxMpMaterialNewsArticle> articles = new ArrayList<>();
private List<WxMpNewsArticle> articles = new ArrayList<>();
public List<WxMpMaterialNewsArticle> getArticles() {
public List<WxMpNewsArticle> getArticles() {
return this.articles;
}
public void addArticle(WxMpMaterialNewsArticle article) {
public void addArticle(WxMpNewsArticle article) {
this.articles.add(article);
}
@@ -43,80 +43,4 @@ public class WxMpMaterialNews implements Serializable {
return this.toJson();
}
/**
* <pre>
* 群发图文消息article.
* 1. thumbMediaId (必填) 图文消息的封面图片素材id必须是永久mediaID
* 2. author 图文消息的作者
* 3. title (必填) 图文消息的标题
* 4. contentSourceUrl 在图文消息页面点击“阅读原文”后的页面链接
* 5. content (必填) 图文消息页面的内容支持HTML标签
* 6. digest 图文消息的描述
* 7. showCoverPic 是否显示封面true为显示false为不显示
* 8. url 点击图文消息跳转链接
* 9. need_open_comment新增字段 否 Uint32 是否打开评论0不打开1打开
* 10. only_fans_can_comment新增字段 否 Uint32 是否粉丝才可评论0所有人可评论1粉丝才可评论
* </pre>
*
* @author chanjarster
*/
@Data
public static class WxMpMaterialNewsArticle implements Serializable{
private static final long serialVersionUID = -635384661692321171L;
/**
* (必填) 图文消息缩略图的media_id可以在基础支持-上传多媒体文件接口中获得.
*/
private String thumbMediaId;
/**
* 图文消息的封面url.
*/
private String thumbUrl;
/**
* 图文消息的作者.
*/
private String author;
/**
* (必填) 图文消息的标题.
*/
private String title;
/**
* 在图文消息页面点击“阅读原文”后的页面链接.
*/
private String contentSourceUrl;
/**
* (必填) 图文消息页面的内容支持HTML标签.
*/
private String content;
/**
* 图文消息的描述.
*/
private String digest;
/**
* 是否显示封面true为显示false为不显示.
*/
private boolean showCoverPic;
/**
* 点击图文消息跳转链接.
*/
private String url;
/**
* need_open_comment
* 是否打开评论0不打开1打开.
*/
private Boolean needOpenComment;
/**
* only_fans_can_comment
* 是否粉丝才可评论0所有人可评论1粉丝才可评论.
*/
private Boolean onlyFansCanComment;
@Override
public String toString() {
return WxMpGsonBuilder.create().toJson(this);
}
}
}

View File

@@ -0,0 +1,82 @@
package me.chanjar.weixin.mp.bean.material;
import lombok.Data;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;
import java.io.Serializable;
/**
* <pre>
* 图文消息article.
* 1. thumbMediaId (必填) 图文消息的封面图片素材id必须是永久mediaID
* 2. author 图文消息的作者
* 3. title (必填) 图文消息的标题
* 4. contentSourceUrl 在图文消息页面点击“阅读原文”后的页面链接
* 5. content (必填) 图文消息页面的内容支持HTML标签
* 6. digest 图文消息的描述
* 7. showCoverPic 是否显示封面true为显示false为不显示
* 8. url 点击图文消息跳转链接
* 9. need_open_comment新增字段 否 Uint32 是否打开评论0不打开1打开
* 10. only_fans_can_comment新增字段 否 Uint32 是否粉丝才可评论0所有人可评论1粉丝才可评论
* </pre>
*
* @author chanjarster
*/
@Data
public class WxMpNewsArticle implements Serializable {
private static final long serialVersionUID = -635384661692321171L;
/**
* (必填) 图文消息缩略图的media_id可以在基础支持-上传多媒体文件接口中获得.
*/
private String thumbMediaId;
/**
* 图文消息的封面url.
*/
private String thumbUrl;
/**
* 图文消息的作者.
*/
private String author;
/**
* (必填) 图文消息的标题.
*/
private String title;
/**
* 在图文消息页面点击“阅读原文”后的页面链接.
*/
private String contentSourceUrl;
/**
* (必填) 图文消息页面的内容支持HTML标签.
*/
private String content;
/**
* 图文消息的描述.
*/
private String digest;
/**
* 是否显示封面true为显示false为不显示.
*/
private boolean showCoverPic;
/**
* 点击图文消息跳转链接.
*/
private String url;
/**
* need_open_comment
* 是否打开评论0不打开1打开.
*/
private Boolean needOpenComment;
/**
* only_fans_can_comment
* 是否粉丝才可评论0所有人可评论1粉丝才可评论.
*/
private Boolean onlyFansCanComment;
@Override
public String toString() {
return WxMpGsonBuilder.create().toJson(this);
}
}

View File

@@ -8,10 +8,10 @@ import me.chanjar.weixin.mp.bean.card.WxMpCardResult;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserCumulate;
import me.chanjar.weixin.mp.bean.datacube.WxDataCubeUserSummary;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardActivateTempInfoResult;
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUpdateResult;
import me.chanjar.weixin.mp.bean.membercard.WxMpMemberCardUserInfoResult;
import me.chanjar.weixin.mp.bean.material.*;
import me.chanjar.weixin.mp.bean.result.*;
import me.chanjar.weixin.mp.bean.subscribe.WxMpSubscribeMessage;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateIndustry;
@@ -45,11 +45,10 @@ public class WxMpGsonBuilder {
INSTANCE.registerTypeAdapter(WxDataCubeUserCumulate.class, new WxMpUserCumulateGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialUploadResult.class, new WxMpMaterialUploadResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialVideoInfoResult.class, new WxMpMaterialVideoInfoResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMassNews.WxMpMassNewsArticle.class, new WxMpMassNewsArticleGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialArticleUpdate.class, new WxMpMaterialArticleUpdateGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialCountResult.class, new WxMpMaterialCountResultAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNews.class, new WxMpMaterialNewsGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNews.WxMpMaterialNewsArticle.class, new WxMpMaterialNewsArticleGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpNewsArticle.class, new WxMpNewsArticleGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.class, new WxMpMaterialNewsBatchGetGsonAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialNewsBatchGetResult.WxMaterialNewsBatchGetNewsItem.class, new WxMpMaterialNewsBatchGetGsonItemAdapter());
INSTANCE.registerTypeAdapter(WxMpMaterialFileBatchGetResult.class, new WxMpMaterialFileBatchGetGsonAdapter());

View File

@@ -1,70 +0,0 @@
package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import org.apache.commons.lang3.BooleanUtils;
import java.lang.reflect.Type;
/**
* @author codepiano
*/
public class WxMpMassNewsArticleGsonAdapter implements JsonSerializer<WxMpMassNews.WxMpMassNewsArticle>, JsonDeserializer<WxMpMassNews.WxMpMassNewsArticle> {
@Override
public JsonElement serialize(WxMpMassNews.WxMpMassNewsArticle article, Type typeOfSrc, JsonSerializationContext context) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("thumb_media_id", article.getThumbMediaId());
articleJson.addProperty("title", article.getTitle());
articleJson.addProperty("content", article.getContent());
if (null != article.getAuthor()) {
articleJson.addProperty("author", article.getAuthor());
}
if (null != article.getContentSourceUrl()) {
articleJson.addProperty("content_source_url", article.getContentSourceUrl());
}
if (null != article.getDigest()) {
articleJson.addProperty("digest", article.getDigest());
}
articleJson.addProperty("show_cover_pic", article.isShowCoverPic() ? "1" : "0");
return articleJson;
}
@Override
public WxMpMassNews.WxMpMassNewsArticle deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject articleInfo = jsonElement.getAsJsonObject();
WxMpMassNews.WxMpMassNewsArticle article = new WxMpMassNews.WxMpMassNewsArticle();
JsonElement title = articleInfo.get("title");
if (title != null && !title.isJsonNull()) {
article.setTitle(GsonHelper.getAsString(title));
}
JsonElement content = articleInfo.get("content");
if (content != null && !content.isJsonNull()) {
article.setContent(GsonHelper.getAsString(content));
}
JsonElement contentSourceUrl = articleInfo.get("content_source_url");
if (contentSourceUrl != null && !contentSourceUrl.isJsonNull()) {
article.setContentSourceUrl(GsonHelper.getAsString(contentSourceUrl));
}
JsonElement author = articleInfo.get("author");
if (author != null && !author.isJsonNull()) {
article.setAuthor(GsonHelper.getAsString(author));
}
JsonElement digest = articleInfo.get("digest");
if (digest != null && !digest.isJsonNull()) {
article.setDigest(GsonHelper.getAsString(digest));
}
JsonElement thumbMediaId = articleInfo.get("thumb_media_id");
if (thumbMediaId != null && !thumbMediaId.isJsonNull()) {
article.setThumbMediaId(GsonHelper.getAsString(thumbMediaId));
}
JsonElement showCoverPic = articleInfo.get("show_cover_pic");
if (showCoverPic != null && !showCoverPic.isJsonNull()) {
article.setShowCoverPic(BooleanUtils.toBoolean(showCoverPic.getAsInt()));
}
return article;
}
}

View File

@@ -2,6 +2,7 @@ package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import java.lang.reflect.Type;
@@ -12,8 +13,8 @@ public class WxMpMassNewsGsonAdapter implements JsonSerializer<WxMpMassNews>, Js
JsonObject newsJson = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (WxMpMassNews.WxMpMassNewsArticle article : message.getArticles()) {
JsonObject articleJson = WxMpGsonBuilder.create().toJsonTree(article, WxMpMassNews.WxMpMassNewsArticle.class).getAsJsonObject();
for (WxMpNewsArticle article : message.getArticles()) {
JsonObject articleJson = WxMpGsonBuilder.create().toJsonTree(article, WxMpNewsArticle.class).getAsJsonObject();
articleJsonArray.add(articleJson);
}
newsJson.add("articles", articleJsonArray);
@@ -29,7 +30,7 @@ public class WxMpMassNewsGsonAdapter implements JsonSerializer<WxMpMassNews>, Js
JsonArray articles = json.getAsJsonArray("articles");
for (JsonElement article1 : articles) {
JsonObject articleInfo = article1.getAsJsonObject();
WxMpMassNews.WxMpMassNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMassNews.WxMpMassNewsArticle.class);
WxMpNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpNewsArticle.class);
wxMpMassNews.addArticle(article);
}
}

View File

@@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialArticleUpdate;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import java.lang.reflect.Type;
@@ -16,7 +16,7 @@ public class WxMpMaterialArticleUpdateGsonAdapter implements JsonSerializer<WxMp
JsonObject articleUpdateJson = new JsonObject();
articleUpdateJson.addProperty("media_id", wxMpMaterialArticleUpdate.getMediaId());
articleUpdateJson.addProperty("index", wxMpMaterialArticleUpdate.getIndex());
articleUpdateJson.add("articles", WxMpGsonBuilder.create().toJsonTree(wxMpMaterialArticleUpdate.getArticles(), WxMpMaterialNews.WxMpMaterialNewsArticle.class));
articleUpdateJson.add("articles", WxMpGsonBuilder.create().toJsonTree(wxMpMaterialArticleUpdate.getArticles(), WxMpNewsArticle.class));
return articleUpdateJson;
}

View File

@@ -3,6 +3,7 @@ package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
@@ -15,8 +16,8 @@ public class WxMpMaterialNewsGsonAdapter implements JsonSerializer<WxMpMaterialN
JsonObject newsJson = new JsonObject();
JsonArray articleJsonArray = new JsonArray();
for (WxMpMaterialNews.WxMpMaterialNewsArticle article : wxMpMaterialNews.getArticles()) {
JsonObject articleJson = WxMpGsonBuilder.create().toJsonTree(article, WxMpMaterialNews.WxMpMaterialNewsArticle.class).getAsJsonObject();
for (WxMpNewsArticle article : wxMpMaterialNews.getArticles()) {
JsonObject articleJson = WxMpGsonBuilder.create().toJsonTree(article, WxMpNewsArticle.class).getAsJsonObject();
articleJsonArray.add(articleJson);
}
newsJson.add("articles", articleJsonArray);
@@ -42,7 +43,7 @@ public class WxMpMaterialNewsGsonAdapter implements JsonSerializer<WxMpMaterialN
JsonArray articles = json.getAsJsonArray("news_item");
for (JsonElement article1 : articles) {
JsonObject articleInfo = article1.getAsJsonObject();
WxMpMaterialNews.WxMpMaterialNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpMaterialNews.WxMpMaterialNewsArticle.class);
WxMpNewsArticle article = WxMpGsonBuilder.create().fromJson(articleInfo, WxMpNewsArticle.class);
wxMpMaterialNews.addArticle(article);
}
}

View File

@@ -2,7 +2,7 @@ package me.chanjar.weixin.mp.util.json;
import com.google.gson.*;
import me.chanjar.weixin.common.util.json.GsonHelper;
import me.chanjar.weixin.mp.bean.material.WxMpMaterialNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import org.apache.commons.lang3.BooleanUtils;
import java.lang.reflect.Type;
@@ -10,10 +10,9 @@ import java.lang.reflect.Type;
/**
* @author codepiano
*/
public class WxMpMaterialNewsArticleGsonAdapter implements JsonSerializer<WxMpMaterialNews.WxMpMaterialNewsArticle>, JsonDeserializer<WxMpMaterialNews.WxMpMaterialNewsArticle> {
public class WxMpNewsArticleGsonAdapter implements JsonSerializer<WxMpNewsArticle>, JsonDeserializer<WxMpNewsArticle> {
@Override
public JsonElement serialize(WxMpMaterialNews.WxMpMaterialNewsArticle article, Type typeOfSrc, JsonSerializationContext context) {
public JsonElement serialize(WxMpNewsArticle article, Type typeOfSrc, JsonSerializationContext context) {
JsonObject articleJson = new JsonObject();
articleJson.addProperty("thumb_media_id", article.getThumbMediaId());
@@ -47,9 +46,9 @@ public class WxMpMaterialNewsArticleGsonAdapter implements JsonSerializer<WxMpMa
}
@Override
public WxMpMaterialNews.WxMpMaterialNewsArticle deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public WxMpNewsArticle deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject articleInfo = jsonElement.getAsJsonObject();
WxMpMaterialNews.WxMpMaterialNewsArticle article = new WxMpMaterialNews.WxMpMaterialNewsArticle();
WxMpNewsArticle article = new WxMpNewsArticle();
JsonElement title = articleInfo.get("title");
if (title != null && !title.isJsonNull()) {