Skip to content

Commit

Permalink
🎨 #1497 微信公众号群发图文消息接口参数优化,保持跟普通发送接口一致,减少重复代码
Browse files Browse the repository at this point in the history
  • Loading branch information
binarywang committed Apr 12, 2020
1 parent 1487ce3 commit 3af0a2b
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 247 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.List;

import lombok.Data;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;
import me.chanjar.weixin.mp.util.json.WxMpGsonBuilder;

/**
Expand All @@ -16,9 +17,9 @@
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);
}

Expand All @@ -35,54 +36,4 @@ public String toString() {
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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand All @@ -43,80 +43,4 @@ public String toString() {
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);
}
}
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
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;
Expand Down Expand Up @@ -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());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.*;
import me.chanjar.weixin.mp.bean.WxMpMassNews;
import me.chanjar.weixin.mp.bean.material.WxMpNewsArticle;

import java.lang.reflect.Type;

Expand All @@ -12,8 +13,8 @@ public JsonElement serialize(WxMpMassNews message, Type typeOfSrc, JsonSerializa
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);
Expand All @@ -29,7 +30,7 @@ public WxMpMassNews deserialize(JsonElement jsonElement, Type type, JsonDeserial
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);
}
}
Expand Down
Loading

0 comments on commit 3af0a2b

Please sign in to comment.