weixin4j-[mp|qy]:媒体接口类(MediaApi)查询素材接口调整:去掉offset,count替换为Pageable类

This commit is contained in:
jinyu 2015-08-13 13:20:25 +08:00
parent d4da1096a8
commit 1a041e0ea4
12 changed files with 116 additions and 51 deletions

View File

@ -444,3 +444,5 @@
* 2015-08-13
+ `release`: weixin4j-[mp|qy] upgrade to 1.5.3,weixin4j-server upgrade to 1.0.5
+ **weixin4j-[mp|qy]**: 媒体接口类(MediaApi)查询素材接口调整:去掉offset,count替换为Pageable类

View File

@ -32,7 +32,7 @@ public class MediaRecord implements Serializable {
/**
* 媒体类型
*/
@JSONField(serialize = false)
@JSONField(serialize = false, deserialize = false)
private MediaType mediaType;
/**
* 媒体信息
@ -40,6 +40,12 @@ public class MediaRecord implements Serializable {
@JSONField(name = "item")
private List<MediaItem> items;
/**
* 分页信息
*/
@JSONField(serialize = false, deserialize = false)
private Pageable pageable;
public int getTotalCount() {
return totalCount;
}
@ -72,10 +78,23 @@ public class MediaRecord implements Serializable {
this.items = items;
}
public Pageable getPageable() {
return pageable;
}
public void setPageable(Pageable pageable) {
this.pageable = pageable;
}
@JSONField(serialize = false)
public Pagedata<MediaItem> getPagedata() {
return new Pagedata<MediaItem>(pageable, totalCount, items);
}
@Override
public String toString() {
return "MediaRecord [totalCount=" + totalCount + ", itemCount="
+ itemCount + ", mediaType=" + mediaType + ", items=" + items
+ "]";
+ ", pageable=" + pageable + "]";
}
}

View File

@ -5,11 +5,12 @@ import java.io.Serializable;
import com.foxinmy.weixin4j.model.Sort.Direction;
/**
* 分页数据(页码从1开始
*
* @className Pageable
* @author jy
* @date 2014年12月27日
* @since JDK 1.7
* @see com.foxinmy.weixin4j.model.springframework.data.domain.Pageable
*/
public class Pageable implements Serializable {
@ -38,6 +39,7 @@ public class Pageable implements Serializable {
this.page = page;
this.size = size;
}
public Pageable(int page, int size, Direction direction,
String... properties) {
this(page, size, new Sort(direction, properties));
@ -49,6 +51,15 @@ public class Pageable implements Serializable {
this.sort = sort;
}
/**
* page=1,size=20
*
* @return
*/
public static Pageable get() {
return new Pageable(1, 20);
}
public int getPageSize() {
return size;
}
@ -56,12 +67,15 @@ public class Pageable implements Serializable {
public int getPageNumber() {
return page;
}
public Sort getSort() {
return sort;
}
public void setSort(Sort sort) {
this.sort = sort;
}
public int getOffset() {
return (page - 1) * size;
}
@ -82,6 +96,7 @@ public class Pageable implements Serializable {
public Pageable first() {
return new Pageable(0, getPageSize(), getSort());
}
@Override
public String toString() {
return "Pageable [page=" + page + ", size=" + size + ", sort=" + sort

View File

@ -1,7 +1,7 @@
package com.foxinmy.weixin4j.tuple;
/**
* 聊天消息元件
* 企业号会话消息元件
*
* @className ChatTuple
* @author jy

View File

@ -58,15 +58,33 @@ public class MpArticle implements Serializable {
@JSONField(name = "cover_url")
private String coverUrl;
@JSONCreator
public MpArticle(@JSONField(name = "thumb_media_id") String thumbMediaId,
@JSONField(name = "title") String title,
@JSONField(name = "content") String content) {
public MpArticle(String thumbMediaId, String title, String content) {
this.thumbMediaId = thumbMediaId;
this.title = title;
this.content = content;
}
@JSONCreator
public MpArticle(@JSONField(name = "thumbMediaId") String thumbMediaId,
@JSONField(name = "author") String author,
@JSONField(name = "title") String title,
@JSONField(name = "sourceUrl") String sourceUrl,
@JSONField(name = "content") String content,
@JSONField(name = "digest") String digest,
@JSONField(name = "showCoverPic") String showCoverPic,
@JSONField(name = "contentUrl") String contentUrl,
@JSONField(name = "coverUrl") String coverUrl) {
this.thumbMediaId = thumbMediaId;
this.author = author;
this.title = title;
this.sourceUrl = sourceUrl;
this.content = content;
this.digest = digest;
this.showCoverPic = showCoverPic;
this.contentUrl = contentUrl;
this.coverUrl = coverUrl;
}
public String getThumbMediaId() {
return thumbMediaId;
}

View File

@ -146,4 +146,6 @@
* 2015-08-13
+ version upgrade to 1.5.3
+ version upgrade to 1.5.3
+ 媒体接口类(MediaApi)查询素材接口调整:去掉offset,count替换为Pageable类

View File

@ -14,6 +14,7 @@ import com.foxinmy.weixin4j.model.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Pageable;
import com.foxinmy.weixin4j.mp.api.CustomApi;
import com.foxinmy.weixin4j.mp.api.DataApi;
import com.foxinmy.weixin4j.mp.api.GroupApi;
@ -352,22 +353,23 @@ public class WeixinProxy {
*
* @param mediaType
* 素材的类型图片image视频video语音 voice图文news
* @param offset
* 从全部素材的该偏移位置开始返回0表示从第一个素材 返回
* @param count
* 返回素材的数量取值在1到20之间
* @param pageable
* 分页数据
* @return 媒体素材的记录对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.mp.api.MediaApi
* @see com.foxinmy.weixin4j.mp.model.MediaRecord
* @see com.foxinmy.weixin4j.type.MediaType
* @see com.foxinmy.weixin4j.mp.model.MediaItem
* @see com.foxinmy.weixin4j.model.MediaItem
* @see com.foxinmy.weixin4j.model.Pageable
* @see com.foxinmy.weixin4j.model.Pagedata
* @see <a
* href="http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html">获取素材列表</a>
*/
public MediaRecord listMaterialMedia(MediaType mediaType, int offset,
int count) throws WeixinException {
return mediaApi.listMaterialMedia(mediaType, offset, count);
public MediaRecord listMaterialMedia(MediaType mediaType, Pageable pageable)
throws WeixinException {
return mediaApi.listMaterialMedia(mediaType, pageable);
}
/**
@ -1312,6 +1314,6 @@ public class WeixinProxy {
throws WeixinException {
return dataApi.datacube(datacubeType, date);
}
public final static String VERSION = "1.5.2";
}

View File

@ -35,6 +35,7 @@ import com.foxinmy.weixin4j.model.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Pageable;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.token.TokenHolder;
import com.foxinmy.weixin4j.tuple.MpArticle;
@ -513,26 +514,26 @@ public class MediaApi extends MpApi {
*
* @param mediaType
* 素材的类型图片image视频video语音 voice图文news
* @param offset
* 从全部素材的该偏移位置开始返回0表示从第一个素材返回
* @param count
* 返回素材的数量取值在1到20之间
* @param pageable
* 分页数据
* @return 媒体素材的记录对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.model.MediaRecord
* @see com.foxinmy.weixin4j.type.MediaType
* @see com.foxinmy.weixin4j.model.MediaItem
* @see com.foxinmy.weixin4j.model.Pageable
* @see com.foxinmy.weixin4j.model.Pagedata
* @see <a
* href="http://mp.weixin.qq.com/wiki/12/2108cd7aafff7f388f41f37efa710204.html">获取素材列表</a>
*/
public MediaRecord listMaterialMedia(MediaType mediaType, int offset,
int count) throws WeixinException {
public MediaRecord listMaterialMedia(MediaType mediaType, Pageable pageable)
throws WeixinException {
Token token = tokenHolder.getToken();
String material_media_list_uri = getRequestUri("material_media_list_uri");
JSONObject obj = new JSONObject();
obj.put("type", mediaType.name());
obj.put("offset", offset);
obj.put("count", count);
obj.put("offset", pageable.getOffset());
obj.put("count", pageable.getPageSize());
WeixinResponse response = weixinClient.post(
String.format(material_media_list_uri, token.getAccessToken()),
obj.toJSONString());
@ -557,6 +558,7 @@ public class MediaApi extends MpApi {
});
}
mediaRecord.setMediaType(mediaType);
mediaRecord.setPageable(pageable);
return mediaRecord;
}
@ -571,17 +573,16 @@ public class MediaApi extends MpApi {
*/
public List<MediaItem> listAllMaterialMedia(MediaType mediaType)
throws WeixinException {
int offset = 0;
int count = 20;
Pageable pageable = new Pageable(1, 20);
List<MediaItem> mediaList = new ArrayList<MediaItem>();
MediaRecord mediaRecord = null;
for (;;) {
mediaRecord = listMaterialMedia(mediaType, offset, count);
mediaRecord = listMaterialMedia(mediaType, pageable);
mediaList.addAll(mediaRecord.getItems());
if (offset >= mediaRecord.getTotalCount()) {
if (pageable.getOffset() >= mediaRecord.getTotalCount()) {
break;
}
offset += count;
pageable = pageable.next();
}
return mediaList;
}

View File

@ -18,6 +18,7 @@ import com.foxinmy.weixin4j.model.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Pageable;
import com.foxinmy.weixin4j.mp.api.MediaApi;
import com.foxinmy.weixin4j.tuple.MpArticle;
import com.foxinmy.weixin4j.tuple.MpVideo;
@ -130,8 +131,9 @@ public class MediaTest extends TokenTest {
@Test
public void listMaterialMedia() throws WeixinException {
MediaRecord mediaRecord = mediaApi.listMaterialMedia(MediaType.news, 0,
20);
Pageable pageable = Pageable.get();
MediaRecord mediaRecord = mediaApi.listMaterialMedia(MediaType.news,
pageable);
System.err.println(mediaRecord);
}

View File

@ -102,4 +102,6 @@
* 2015-08-13
+ version upgrade to 1.5.3
+ version upgrade to 1.5.3
+ 媒体接口类(MediaApi)查询素材接口调整:去掉offset,count替换为Pageable类

View File

@ -13,6 +13,7 @@ import com.foxinmy.weixin4j.model.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Pageable;
import com.foxinmy.weixin4j.qy.api.AgentApi;
import com.foxinmy.weixin4j.qy.api.BatchApi;
import com.foxinmy.weixin4j.qy.api.ChatApi;
@ -383,22 +384,22 @@ public class WeixinProxy {
* 企业应用ID
* @param mediaType
* 素材的类型图片image视频video语音 voice图文news文件file
* @param offset
* 从全部素材的该偏移位置开始返回0表示从第一个素材返回
* @param count
* 返回素材的数量取值在1到20之间
* @param pageable
* 分页数据
* @return 媒体素材的记录对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.qy.api.MediaApi
* @see com.foxinmy.weixin4j.model.MediaRecord
* @see com.foxinmy.weixin4j.type.MediaType
* @see com.foxinmy.weixin4j.model.MediaItem
* @see com.foxinmy.weixin4j.model.Pageable
* @see com.foxinmy.weixin4j.model.Pagedata
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8">获取素材列表</a>
*/
public MediaRecord listMaterialMedia(int agentid, MediaType mediaType,
int offset, int count) throws WeixinException {
return mediaApi.listMaterialMedia(agentid, mediaType, offset, count);
Pageable pageable) throws WeixinException {
return mediaApi.listMaterialMedia(agentid, mediaType, pageable);
}
/**

View File

@ -35,6 +35,7 @@ import com.foxinmy.weixin4j.model.MediaDownloadResult;
import com.foxinmy.weixin4j.model.MediaItem;
import com.foxinmy.weixin4j.model.MediaRecord;
import com.foxinmy.weixin4j.model.MediaUploadResult;
import com.foxinmy.weixin4j.model.Pageable;
import com.foxinmy.weixin4j.model.Token;
import com.foxinmy.weixin4j.qy.model.Callback;
import com.foxinmy.weixin4j.qy.model.Party;
@ -410,28 +411,28 @@ public class MediaApi extends QyApi {
* 企业应用ID
* @param mediaType
* 素材的类型图片image视频video语音 voice图文news文件file
* @param offset
* 从全部素材的该偏移位置开始返回0表示从第一个素材返回
* @param count
* 返回素材的数量取值在1到20之间
* @param pageable
* 分页数据
* @return 媒体素材的记录对象
* @throws WeixinException
* @see com.foxinmy.weixin4j.model.MediaRecord
* @see com.foxinmy.weixin4j.type.MediaType
* @see com.foxinmy.weixin4j.model.MediaItem
* @see com.foxinmy.weixin4j.model.Pageable
* @see com.foxinmy.weixin4j.model.Pagedata
* @see <a
* href="http://qydev.weixin.qq.com/wiki/index.php?title=%E8%8E%B7%E5%8F%96%E7%B4%A0%E6%9D%90%E5%88%97%E8%A1%A8">获取素材列表</a>
*/
public MediaRecord listMaterialMedia(int agentid, MediaType mediaType,
int offset, int count) throws WeixinException {
Pageable pageable) throws WeixinException {
Token token = tokenHolder.getToken();
String material_media_list_uri = getRequestUri("material_media_list_uri");
JSONObject obj = new JSONObject();
obj.put("agentid", agentid);
obj.put("type",
mediaType == MediaType.news ? "mpnews" : mediaType.name());
obj.put("offset", offset);
obj.put("count", count);
obj.put("offset", pageable.getOffset());
obj.put("count", pageable.getPageSize());
WeixinResponse response = weixinClient.post(
String.format(material_media_list_uri, token.getAccessToken()),
obj.toJSONString());
@ -443,6 +444,7 @@ public class MediaApi extends QyApi {
MediaItem.class));
}
mediaRecord.setMediaType(mediaType);
mediaRecord.setPageable(pageable);
return mediaRecord;
}
@ -459,17 +461,16 @@ public class MediaApi extends QyApi {
*/
public List<MediaItem> listAllMaterialMedia(int agentid, MediaType mediaType)
throws WeixinException {
int offset = 0;
int count = 20;
Pageable pageable = new Pageable(1, 20);
List<MediaItem> mediaList = new ArrayList<MediaItem>();
MediaRecord mediaRecord = null;
for (;;) {
mediaRecord = listMaterialMedia(agentid, mediaType, offset, count);
mediaRecord = listMaterialMedia(agentid, mediaType, pageable);
mediaList.addAll(mediaRecord.getItems());
if (offset >= mediaRecord.getTotalCount()) {
if (pageable.getOffset() >= mediaRecord.getTotalCount()) {
break;
}
offset += count;
pageable = pageable.next();
}
return mediaList;
}