调整下图文消息格式

This commit is contained in:
jinyu 2015-07-16 17:31:50 +08:00
parent ba5d2da3a1
commit 624d2fac8f
6 changed files with 68 additions and 67 deletions

View File

@ -46,13 +46,11 @@ public class MpArticle implements Serializable {
*/ */
@JSONField(name = "show_cover_pic") @JSONField(name = "show_cover_pic")
private String showCoverPic; private String showCoverPic;
/** /**
* 正文的URL 可为空 * 正文的URL 可为空
*/ */
@JSONField(name = "content_url") @JSONField(name = "content_url")
private String contentUrl; private String contentUrl;
/** /**
* 封面图片的URL 可为空 * 封面图片的URL 可为空
*/ */
@ -65,10 +63,6 @@ public class MpArticle implements Serializable {
this.content = content; this.content = content;
} }
public MpArticle() {
}
public String getThumbMediaId() { public String getThumbMediaId() {
return thumbMediaId; return thumbMediaId;
} }

View File

@ -1,6 +1,6 @@
package com.foxinmy.weixin4j.tuple; package com.foxinmy.weixin4j.tuple;
import java.util.ArrayList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
@ -38,9 +38,9 @@ public class MpNews implements MassTuple, NotifyTuple {
/** /**
* 图文列表 * 图文列表
*/ */
@JSONField(name = "articles") @JSONField(serialize = false)
@XmlTransient @XmlTransient
private List<MpArticle> articles; private LinkedList<MpArticle> articles;
public MpNews() { public MpNews() {
this(null); this(null);
@ -48,34 +48,38 @@ public class MpNews implements MassTuple, NotifyTuple {
public MpNews(String mediaId) { public MpNews(String mediaId) {
this.mediaId = mediaId; this.mediaId = mediaId;
this.articles = new ArrayList<MpArticle>(); this.articles = new LinkedList<MpArticle>();
} }
public void pushArticle(String thumbMediaId, String title, String content) { public MpNews addArticle(String thumbMediaId, String title, String content) {
articles.add(new MpArticle(thumbMediaId, title, content)); return addArticle(new MpArticle(thumbMediaId, title, content));
} }
public void pushFirstArticle(String thumbMediaId, String title, public MpNews addArticle(MpArticle... articles) {
String content) { for (MpArticle article : articles) {
articles.add(0, new MpArticle(thumbMediaId, title, content)); this.articles.add(article);
}
return this;
} }
public void pushLastArticle(String thumbMediaId, String title, public MpNews addFirstArticle(MpArticle article) {
String content) { articles.addFirst(article);
articles.add(articles.size(), new MpArticle(thumbMediaId, title, return this;
content));
} }
public MpArticle removeLastArticle() { public MpNews addLastArticle(MpArticle article) {
return articles.remove(articles.size() - 1); articles.addLast(article);
return this;
} }
public MpArticle removeFirstArticle() { public MpNews removeFirstArticle() {
return articles.remove(0); articles.removeFirst();
return this;
} }
public void setArticles(List<MpArticle> articles) { public MpNews removeLastArticle() {
this.articles = articles; articles.removeLast();
return this;
} }
public List<MpArticle> getArticles() { public List<MpArticle> getArticles() {
@ -86,10 +90,6 @@ public class MpNews implements MassTuple, NotifyTuple {
return mediaId; return mediaId;
} }
public void setMediaId(String mediaId) {
this.mediaId = mediaId;
}
@Override @Override
public String toString() { public String toString() {
return "MpNews [articles=" + articles + ", mediaId=" + mediaId + "]"; return "MpNews [articles=" + articles + ", mediaId=" + mediaId + "]";

View File

@ -1,10 +1,10 @@
package com.foxinmy.weixin4j.tuple; package com.foxinmy.weixin4j.tuple;
import java.util.ArrayList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient;
import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONField;
@ -20,7 +20,6 @@ import com.alibaba.fastjson.annotation.JSONField;
* @since JDK 1.7 * @since JDK 1.7
* @see * @see
*/ */
@XmlRootElement(name = "Articles")
public class News implements NotifyTuple { public class News implements NotifyTuple {
private static final long serialVersionUID = 3348756809039388415L; private static final long serialVersionUID = 3348756809039388415L;
@ -39,50 +38,57 @@ public class News implements NotifyTuple {
*/ */
@JSONField(name = "articles") @JSONField(name = "articles")
@XmlElement(name = "Articles") @XmlElement(name = "Articles")
private List<Article> articles; private LinkedList<Article> articles;
public News() { public News() {
this.articles = new ArrayList<Article>(); this.articles = new LinkedList<Article>();
} }
public void pushArticle(String title, String desc, String picUrl, String url) { public News addArticle(String title, String desc, String picUrl, String url) {
if ((articles.size() + 1) > MAX_ARTICLE_COUNT) { return addArticle(new Article(title, desc, picUrl, url));
return; }
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, public News addFirstArticle(Article article) {
String url) { articles.addFirst(article);
articles.add(0, new Article(title, desc, picUrl, url)); return this;
} }
public void pushLastArticle(String title, String desc, String picUrl, public void addLastArticle(Article article) {
String url) { articles.addLast(article);
articles.add(articles.size(), new Article(title, desc, picUrl, url));
} }
public Article removeLastArticle() { public News removeFirstArticle() {
return articles.remove(articles.size() - 1); articles.removeFirst();
return this;
} }
public Article removeFirstArticle() { public News removeLastArticle() {
return articles.remove(0); articles.removeLast();
return this;
} }
@JSONField(serialize = false) @JSONField(serialize = false)
@XmlTransient
public boolean isMaxCount() { public boolean isMaxCount() {
return this.articles.size() == MAX_ARTICLE_COUNT; return 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;
} }
public List<Article> getArticles() { 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; return articles;
} }

View File

@ -133,12 +133,13 @@ public class MassApi extends MpApi {
if (tuple instanceof MpNews) { if (tuple instanceof MpNews) {
MpNews _news = (MpNews) tuple; MpNews _news = (MpNews) tuple;
List<MpArticle> _articles = _news.getArticles(); List<MpArticle> _articles = _news.getArticles();
if (StringUtil.isBlank(_news.getMediaId()) if (StringUtil.isBlank(_news.getMediaId())) {
&& (_articles == null || _articles.isEmpty())) { if (_articles.isEmpty()) {
throw new WeixinException( throw new WeixinException(
"mass fail:mediaId or articles is required"); "mass fail:mediaId or articles is required");
}
tuple = new MpNews(uploadArticle(_articles));
} }
tuple = new MpNews(uploadArticle(_articles));
} }
String msgtype = tuple.getMessageType(); String msgtype = tuple.getMessageType();
JSONObject obj = new JSONObject(); JSONObject obj = new JSONObject();

View File

@ -73,8 +73,8 @@ public class NotifyTest extends TokenTest {
public void news() throws WeixinException { public void news() throws WeixinException {
News news = new News(); News news = new News();
NotifyMessage notify = new NotifyMessage("to", news); NotifyMessage notify = new NotifyMessage("to", news);
news.pushArticle("title1", "desc1", "picUrl1", "url1"); news.addArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2"); news.addArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notifyApi.sendNotify(notify)); System.out.println(notifyApi.sendNotify(notify));
} }

View File

@ -70,8 +70,8 @@ public class NotifyMsgTest extends TokenTest {
public void news() throws WeixinException { public void news() throws WeixinException {
News news = new News(); News news = new News();
NotifyMessage notify = new NotifyMessage(news, 0); NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("title1", "desc1", "picUrl1", "url1"); news.addArticle("title1", "desc1", "picUrl1", "url1");
news.pushArticle("title2", "desc2", "picUrl2", "url2"); news.addArticle("title2", "desc2", "picUrl2", "url2");
System.out.println(notifyApi.sendNotify(notify)); System.out.println(notifyApi.sendNotify(notify));
} }
@ -79,8 +79,8 @@ public class NotifyMsgTest extends TokenTest {
public void mpnews() throws WeixinException { public void mpnews() throws WeixinException {
MpNews news = new MpNews(); MpNews news = new MpNews();
NotifyMessage notify = new NotifyMessage(news, 0); NotifyMessage notify = new NotifyMessage(news, 0);
news.pushArticle("thumbMediaId1", "title1", "content1"); news.addArticle("thumbMediaId1", "title1", "content1");
news.pushArticle("thumbMediaId2", "title1", "content2"); news.addArticle("thumbMediaId2", "title1", "content2");
System.out.println(notifyApi.sendNotify(notify)); System.out.println(notifyApi.sendNotify(notify));
} }