调整下图文消息格式
This commit is contained in:
parent
ba5d2da3a1
commit
624d2fac8f
@ -46,13 +46,11 @@ public class MpArticle implements Serializable {
|
||||
*/
|
||||
@JSONField(name = "show_cover_pic")
|
||||
private String showCoverPic;
|
||||
|
||||
/**
|
||||
* 正文的URL 可为空
|
||||
*/
|
||||
@JSONField(name = "content_url")
|
||||
private String contentUrl;
|
||||
|
||||
/**
|
||||
* 封面图片的URL 可为空
|
||||
*/
|
||||
@ -65,10 +63,6 @@ public class MpArticle implements Serializable {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public MpArticle() {
|
||||
|
||||
}
|
||||
|
||||
public String getThumbMediaId() {
|
||||
return thumbMediaId;
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
package com.foxinmy.weixin4j.tuple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
@ -38,9 +38,9 @@ public class MpNews implements MassTuple, NotifyTuple {
|
||||
/**
|
||||
* 图文列表
|
||||
*/
|
||||
@JSONField(name = "articles")
|
||||
@JSONField(serialize = false)
|
||||
@XmlTransient
|
||||
private List<MpArticle> articles;
|
||||
private LinkedList<MpArticle> articles;
|
||||
|
||||
public MpNews() {
|
||||
this(null);
|
||||
@ -48,34 +48,38 @@ public class MpNews implements MassTuple, NotifyTuple {
|
||||
|
||||
public MpNews(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
this.articles = new ArrayList<MpArticle>();
|
||||
this.articles = new LinkedList<MpArticle>();
|
||||
}
|
||||
|
||||
public void pushArticle(String thumbMediaId, String title, String content) {
|
||||
articles.add(new MpArticle(thumbMediaId, title, content));
|
||||
public MpNews addArticle(String thumbMediaId, String title, String content) {
|
||||
return addArticle(new MpArticle(thumbMediaId, title, content));
|
||||
}
|
||||
|
||||
public void pushFirstArticle(String thumbMediaId, String title,
|
||||
String content) {
|
||||
articles.add(0, new MpArticle(thumbMediaId, title, content));
|
||||
public MpNews addArticle(MpArticle... articles) {
|
||||
for (MpArticle article : articles) {
|
||||
this.articles.add(article);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public void pushLastArticle(String thumbMediaId, String title,
|
||||
String content) {
|
||||
articles.add(articles.size(), new MpArticle(thumbMediaId, title,
|
||||
content));
|
||||
public MpNews addFirstArticle(MpArticle article) {
|
||||
articles.addFirst(article);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MpArticle removeLastArticle() {
|
||||
return articles.remove(articles.size() - 1);
|
||||
public MpNews addLastArticle(MpArticle article) {
|
||||
articles.addLast(article);
|
||||
return this;
|
||||
}
|
||||
|
||||
public MpArticle removeFirstArticle() {
|
||||
return articles.remove(0);
|
||||
public MpNews removeFirstArticle() {
|
||||
articles.removeFirst();
|
||||
return this;
|
||||
}
|
||||
|
||||
public void setArticles(List<MpArticle> articles) {
|
||||
this.articles = articles;
|
||||
public MpNews removeLastArticle() {
|
||||
articles.removeLast();
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<MpArticle> getArticles() {
|
||||
@ -86,10 +90,6 @@ public class MpNews implements MassTuple, NotifyTuple {
|
||||
return mediaId;
|
||||
}
|
||||
|
||||
public void setMediaId(String mediaId) {
|
||||
this.mediaId = mediaId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "MpNews [articles=" + articles + ", mediaId=" + mediaId + "]";
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
package com.foxinmy.weixin4j.tuple;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import javax.xml.bind.annotation.XmlElement;
|
||||
import javax.xml.bind.annotation.XmlRootElement;
|
||||
import javax.xml.bind.annotation.XmlTransient;
|
||||
|
||||
import com.alibaba.fastjson.annotation.JSONField;
|
||||
|
||||
@ -20,7 +20,6 @@ import com.alibaba.fastjson.annotation.JSONField;
|
||||
* @since JDK 1.7
|
||||
* @see
|
||||
*/
|
||||
@XmlRootElement(name = "Articles")
|
||||
public class News implements NotifyTuple {
|
||||
|
||||
private static final long serialVersionUID = 3348756809039388415L;
|
||||
@ -39,50 +38,57 @@ public class News implements NotifyTuple {
|
||||
*/
|
||||
@JSONField(name = "articles")
|
||||
@XmlElement(name = "Articles")
|
||||
private List<Article> articles;
|
||||
private LinkedList<Article> articles;
|
||||
|
||||
public News() {
|
||||
this.articles = new ArrayList<Article>();
|
||||
this.articles = new LinkedList<Article>();
|
||||
}
|
||||
|
||||
public void pushArticle(String title, String desc, String picUrl, String url) {
|
||||
if ((articles.size() + 1) > MAX_ARTICLE_COUNT) {
|
||||
return;
|
||||
public News addArticle(String title, String desc, String picUrl, String url) {
|
||||
return addArticle(new Article(title, desc, picUrl, url));
|
||||
}
|
||||
|
||||
public News addArticle(Article... articles) {
|
||||
for (Article article : articles) {
|
||||
this.articles.add(article);
|
||||
}
|
||||
articles.add(new Article(title, desc, picUrl, url));
|
||||
return this;
|
||||
}
|
||||
|
||||
public void pushFirstArticle(String title, String desc, String picUrl,
|
||||
String url) {
|
||||
articles.add(0, new Article(title, desc, picUrl, url));
|
||||
public News addFirstArticle(Article article) {
|
||||
articles.addFirst(article);
|
||||
return this;
|
||||
}
|
||||
|
||||
public void pushLastArticle(String title, String desc, String picUrl,
|
||||
String url) {
|
||||
articles.add(articles.size(), new Article(title, desc, picUrl, url));
|
||||
public void addLastArticle(Article article) {
|
||||
articles.addLast(article);
|
||||
}
|
||||
|
||||
public Article removeLastArticle() {
|
||||
return articles.remove(articles.size() - 1);
|
||||
public News removeFirstArticle() {
|
||||
articles.removeFirst();
|
||||
return this;
|
||||
}
|
||||
|
||||
public Article removeFirstArticle() {
|
||||
return articles.remove(0);
|
||||
public News removeLastArticle() {
|
||||
articles.removeLast();
|
||||
return this;
|
||||
}
|
||||
|
||||
@JSONField(serialize = false)
|
||||
@XmlTransient
|
||||
public boolean isMaxCount() {
|
||||
return this.articles.size() == MAX_ARTICLE_COUNT;
|
||||
}
|
||||
|
||||
public void setArticles(List<Article> articles) {
|
||||
if (articles.size() > MAX_ARTICLE_COUNT) {
|
||||
articles = articles.subList(0, MAX_ARTICLE_COUNT);
|
||||
}
|
||||
this.articles = articles;
|
||||
return articles.size() == MAX_ARTICLE_COUNT;
|
||||
}
|
||||
|
||||
public List<Article> getArticles() {
|
||||
if (articles.size() > MAX_ARTICLE_COUNT) {
|
||||
return articles.subList(0, MAX_ARTICLE_COUNT);
|
||||
} else {
|
||||
return articles;
|
||||
}
|
||||
}
|
||||
|
||||
public List<Article> getFullArticles() {
|
||||
return articles;
|
||||
}
|
||||
|
||||
|
||||
@ -133,12 +133,13 @@ public class MassApi extends MpApi {
|
||||
if (tuple instanceof MpNews) {
|
||||
MpNews _news = (MpNews) tuple;
|
||||
List<MpArticle> _articles = _news.getArticles();
|
||||
if (StringUtil.isBlank(_news.getMediaId())
|
||||
&& (_articles == null || _articles.isEmpty())) {
|
||||
throw new WeixinException(
|
||||
"mass fail:mediaId or articles is required");
|
||||
if (StringUtil.isBlank(_news.getMediaId())) {
|
||||
if (_articles.isEmpty()) {
|
||||
throw new WeixinException(
|
||||
"mass fail:mediaId or articles is required");
|
||||
}
|
||||
tuple = new MpNews(uploadArticle(_articles));
|
||||
}
|
||||
tuple = new MpNews(uploadArticle(_articles));
|
||||
}
|
||||
String msgtype = tuple.getMessageType();
|
||||
JSONObject obj = new JSONObject();
|
||||
|
||||
@ -73,8 +73,8 @@ public class NotifyTest extends TokenTest {
|
||||
public void news() throws WeixinException {
|
||||
News news = new News();
|
||||
NotifyMessage notify = new NotifyMessage("to", news);
|
||||
news.pushArticle("title1", "desc1", "picUrl1", "url1");
|
||||
news.pushArticle("title2", "desc2", "picUrl2", "url2");
|
||||
news.addArticle("title1", "desc1", "picUrl1", "url1");
|
||||
news.addArticle("title2", "desc2", "picUrl2", "url2");
|
||||
System.out.println(notifyApi.sendNotify(notify));
|
||||
}
|
||||
|
||||
|
||||
@ -70,8 +70,8 @@ public class NotifyMsgTest extends TokenTest {
|
||||
public void news() throws WeixinException {
|
||||
News news = new News();
|
||||
NotifyMessage notify = new NotifyMessage(news, 0);
|
||||
news.pushArticle("title1", "desc1", "picUrl1", "url1");
|
||||
news.pushArticle("title2", "desc2", "picUrl2", "url2");
|
||||
news.addArticle("title1", "desc1", "picUrl1", "url1");
|
||||
news.addArticle("title2", "desc2", "picUrl2", "url2");
|
||||
System.out.println(notifyApi.sendNotify(notify));
|
||||
}
|
||||
|
||||
@ -79,8 +79,8 @@ public class NotifyMsgTest extends TokenTest {
|
||||
public void mpnews() throws WeixinException {
|
||||
MpNews news = new MpNews();
|
||||
NotifyMessage notify = new NotifyMessage(news, 0);
|
||||
news.pushArticle("thumbMediaId1", "title1", "content1");
|
||||
news.pushArticle("thumbMediaId2", "title1", "content2");
|
||||
news.addArticle("thumbMediaId1", "title1", "content1");
|
||||
news.addArticle("thumbMediaId2", "title1", "content2");
|
||||
System.out.println(notifyApi.sendNotify(notify));
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user