* #1429 增加小程序直播类相关接口 * 新增:获取所有直播间列表,区分分页 * 优化代码格式 Co-authored-by: yjwang <yjwang@wisu.com.cn>dev1
| @@ -0,0 +1,62 @@ | |||||
| package cn.binarywang.wx.miniapp.api; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo; | |||||
| import me.chanjar.weixin.common.error.WxErrorException; | |||||
| import java.util.List; | |||||
| /** | |||||
| * <pre> | |||||
| * 直播相关操作接口. | |||||
| * Created by yjwang on 2020/4/5. | |||||
| * </pre> | |||||
| * | |||||
| * @author <a href="https://github.com/yjwang3300300">yjwang</a> | |||||
| */ | |||||
| public interface WxMaLiveService { | |||||
| String GET_LIVE_INFO = "http://api.weixin.qq.com/wxa/business/getliveinfo"; | |||||
| /** | |||||
| * 获取直播房间列表.(分页) | |||||
| * | |||||
| * @param start 起始拉取房间,start = 0 表示从第 1 个房间开始拉取 | |||||
| * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 | |||||
| * @return . | |||||
| * @throws WxErrorException . | |||||
| */ | |||||
| WxMaGetLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException; | |||||
| /** | |||||
| * 获取所有直播间信息(没有分页直接获取全部) | |||||
| * @return | |||||
| * @throws WxErrorException | |||||
| */ | |||||
| List<WxMaGetLiveInfo.RoomInfo> getLiveinfos() throws WxErrorException; | |||||
| /** | |||||
| * | |||||
| * 获取直播房间回放数据信息. | |||||
| * | |||||
| * @param action 获取回放 | |||||
| * @param room_id 直播间 id | |||||
| * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 | |||||
| * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 | |||||
| * @return | |||||
| * @throws WxErrorException | |||||
| */ | |||||
| WxMaGetLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; | |||||
| /** | |||||
| * | |||||
| * 获取直播房间回放数据信息. | |||||
| * | |||||
| * 获取回放 (默认:get_replay) | |||||
| * @param room_id 直播间 id | |||||
| * @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取 | |||||
| * @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内 | |||||
| * @return | |||||
| * @throws WxErrorException | |||||
| */ | |||||
| WxMaGetLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; | |||||
| } | |||||
| @@ -287,4 +287,11 @@ public interface WxMaService { | |||||
| * @return . | * @return . | ||||
| */ | */ | ||||
| WxMaCloudService getCloudService(); | WxMaCloudService getCloudService(); | ||||
| /** | |||||
| * 获取直播接口服务对象 | |||||
| * | |||||
| * @return . | |||||
| */ | |||||
| WxMaLiveService getLiveService(); | |||||
| } | } | ||||
| @@ -0,0 +1,100 @@ | |||||
| package cn.binarywang.wx.miniapp.api.impl; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaLiveService; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo; | |||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||||
| import com.google.gson.JsonObject; | |||||
| import com.google.gson.JsonParser; | |||||
| import lombok.AllArgsConstructor; | |||||
| import me.chanjar.weixin.common.WxType; | |||||
| import me.chanjar.weixin.common.error.WxError; | |||||
| import me.chanjar.weixin.common.error.WxErrorException; | |||||
| import java.util.ArrayList; | |||||
| import java.util.HashMap; | |||||
| import java.util.List; | |||||
| import java.util.Map; | |||||
| /** | |||||
| * <pre> | |||||
| * Created by yjwang on 2020/4/5. | |||||
| * </pre> | |||||
| * | |||||
| * @author <a href="https://github.com/yjwang3300300">yjwang</a> | |||||
| */ | |||||
| @AllArgsConstructor | |||||
| public class WxMaLiveServiceImpl implements WxMaLiveService { | |||||
| private static final JsonParser JSON_PARSER = new JsonParser(); | |||||
| private WxMaService service; | |||||
| @Override | |||||
| public WxMaGetLiveInfo getLiveInfo(Integer start, Integer limit) throws WxErrorException { | |||||
| JsonObject jsonObject = getJsonObject(start, limit, null); | |||||
| return WxMaGetLiveInfo.fromJson(jsonObject.toString()); | |||||
| } | |||||
| @Override | |||||
| public List<WxMaGetLiveInfo.RoomInfo> getLiveinfos() throws WxErrorException { | |||||
| List<WxMaGetLiveInfo.RoomInfo> results = new ArrayList<>(); | |||||
| Integer start = 0; | |||||
| Integer limit = 80; | |||||
| Integer tatal = 0; | |||||
| WxMaGetLiveInfo liveInfo = null; | |||||
| do { | |||||
| if (tatal != 0 && tatal <= start) { | |||||
| break; | |||||
| } | |||||
| liveInfo = getLiveInfo(start, limit); | |||||
| if (liveInfo == null) { | |||||
| return null; | |||||
| } | |||||
| results.addAll(liveInfo.getRoomInfos()); | |||||
| tatal = liveInfo.getTotal(); | |||||
| start = results.size(); | |||||
| try { | |||||
| Thread.sleep(100); | |||||
| } catch (InterruptedException e) { | |||||
| e.printStackTrace(); | |||||
| } | |||||
| } while (results.size() <= tatal); | |||||
| return results; | |||||
| } | |||||
| @Override | |||||
| public WxMaGetLiveInfo getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { | |||||
| Map<String, Object> map = new HashMap(4); | |||||
| map.put("action", action); | |||||
| map.put("room_id", room_id); | |||||
| JsonObject jsonObject = getJsonObject(start, limit, map); | |||||
| return WxMaGetLiveInfo.fromJson(jsonObject.toString()); | |||||
| } | |||||
| @Override | |||||
| public WxMaGetLiveInfo getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { | |||||
| return getLiveReplay("get_replay", room_id, start, limit); | |||||
| } | |||||
| /** | |||||
| * 包装一下 | |||||
| * | |||||
| * @param start | |||||
| * @param limit | |||||
| * @param map | |||||
| * @return | |||||
| * @throws WxErrorException | |||||
| */ | |||||
| private JsonObject getJsonObject(Integer start, Integer limit, Map<String, Object> map) throws WxErrorException { | |||||
| if (map == null) { | |||||
| map = new HashMap(2); | |||||
| } | |||||
| map.put("start", start); | |||||
| map.put("limit", limit); | |||||
| String responseContent = service.post(GET_LIVE_INFO, WxMaGsonBuilder.create().toJson(map)); | |||||
| JsonObject jsonObject = JSON_PARSER.parse(responseContent).getAsJsonObject(); | |||||
| if (jsonObject.get("errcode").getAsInt() != 0) { | |||||
| throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); | |||||
| } | |||||
| return jsonObject; | |||||
| } | |||||
| } | |||||
| @@ -60,6 +60,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||||
| private WxMaExpressService expressService = new WxMaExpressServiceImpl(this); | private WxMaExpressService expressService = new WxMaExpressServiceImpl(this); | ||||
| private WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this); | private WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this); | ||||
| private WxMaCloudService cloudService = new WxMaCloudServiceImpl(this); | private WxMaCloudService cloudService = new WxMaCloudServiceImpl(this); | ||||
| private WxMaLiveService liveService = new WxMaLiveServiceImpl(this); | |||||
| private int retrySleepMillis = 1000; | private int retrySleepMillis = 1000; | ||||
| private int maxRetryTimes = 5; | private int maxRetryTimes = 5; | ||||
| @@ -415,4 +416,9 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl | |||||
| public WxMaCloudService getCloudService() { | public WxMaCloudService getCloudService() { | ||||
| return this.cloudService; | return this.cloudService; | ||||
| } | } | ||||
| @Override | |||||
| public WxMaLiveService getLiveService() { | |||||
| return this.liveService; | |||||
| } | |||||
| } | } | ||||
| @@ -0,0 +1,88 @@ | |||||
| package cn.binarywang.wx.miniapp.bean; | |||||
| import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder; | |||||
| import com.google.gson.annotations.SerializedName; | |||||
| import lombok.Data; | |||||
| import java.io.Serializable; | |||||
| import java.util.List; | |||||
| /** | |||||
| * 获取直播房间列表 | |||||
| * | |||||
| * @author yjwang | |||||
| * @date 2020/4/5 | |||||
| */ | |||||
| @Data | |||||
| public class WxMaGetLiveInfo implements Serializable { | |||||
| private static final long serialVersionUID = 7285263767524755887L; | |||||
| private Integer errcode; | |||||
| private String errmsg; | |||||
| private Integer total; | |||||
| /** | |||||
| * 直播间列表 | |||||
| */ | |||||
| @SerializedName("room_info") | |||||
| private List<RoomInfo> roomInfos; | |||||
| /** | |||||
| * 获取回放源视频列表 | |||||
| */ | |||||
| @SerializedName("live_replay") | |||||
| private List<LiveReplay> liveReplay; | |||||
| public static WxMaGetLiveInfo fromJson(String json) { | |||||
| return WxMaGsonBuilder.create().fromJson(json, WxMaGetLiveInfo.class); | |||||
| } | |||||
| /** | |||||
| * 直播列表 | |||||
| */ | |||||
| @Data | |||||
| public static class RoomInfo implements Serializable { | |||||
| private static final long serialVersionUID = 7745775280267417154L; | |||||
| private String name; | |||||
| private Integer roomid; | |||||
| @SerializedName("cover_img") | |||||
| private String coverImg; | |||||
| @SerializedName("live_satus") | |||||
| private Integer liveSatus; | |||||
| @SerializedName("start_time") | |||||
| private Long startTime; | |||||
| @SerializedName("end_time") | |||||
| private Long endTime; | |||||
| @SerializedName("anchor_name") | |||||
| private String anchorName; | |||||
| @SerializedName("anchor_img") | |||||
| private String anchorImg; | |||||
| private List<Goods> goods; | |||||
| } | |||||
| /** | |||||
| * 商品列表 | |||||
| */ | |||||
| @Data | |||||
| public static class Goods implements Serializable { | |||||
| private static final long serialVersionUID = 5769245932149287574L; | |||||
| @SerializedName("cover_img") | |||||
| private String coverImg; | |||||
| private String url; | |||||
| private String price; | |||||
| private String name; | |||||
| } | |||||
| /** | |||||
| * 回放数据列表 | |||||
| */ | |||||
| @Data | |||||
| public static class LiveReplay implements Serializable { | |||||
| private static final long serialVersionUID = 7683927205627536320L; | |||||
| @SerializedName("expire_time") | |||||
| private String expireTime; | |||||
| @SerializedName("create_time") | |||||
| private String createTime; | |||||
| @SerializedName("media_url") | |||||
| private String mediaUrl; | |||||
| } | |||||
| } | |||||
| @@ -0,0 +1,55 @@ | |||||
| package cn.binarywang.wx.miniapp.api.impl; | |||||
| import cn.binarywang.wx.miniapp.api.WxMaService; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaGetLiveInfo; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaPhoneNumberInfo; | |||||
| import cn.binarywang.wx.miniapp.bean.WxMaUserInfo; | |||||
| import cn.binarywang.wx.miniapp.test.ApiTestModule; | |||||
| import cn.binarywang.wx.miniapp.test.TestConfig; | |||||
| import com.google.common.collect.ImmutableMap; | |||||
| import com.google.gson.JsonObject; | |||||
| import com.google.inject.Inject; | |||||
| import me.chanjar.weixin.common.error.WxErrorException; | |||||
| import org.testng.annotations.Guice; | |||||
| import org.testng.annotations.Test; | |||||
| import java.util.List; | |||||
| import java.util.stream.Collectors; | |||||
| import static org.testng.Assert.assertNotNull; | |||||
| import static org.testng.Assert.assertTrue; | |||||
| /** | |||||
| * 测试直播相关的接口 | |||||
| * | |||||
| * @author <a href="https://github.com/yjwang3300300">yjwang</a> | |||||
| */ | |||||
| @Test | |||||
| @Guice(modules = ApiTestModule.class) | |||||
| public class WxMaLiveServiceImplTest { | |||||
| @Inject | |||||
| private WxMaService wxService; | |||||
| @Test | |||||
| public void getLiveInfo() throws Exception { | |||||
| WxMaGetLiveInfo list = this.wxService.getLiveService().getLiveInfo(0,10); | |||||
| assertNotNull(list); | |||||
| System.out.println(list.toString()); | |||||
| } | |||||
| @Test | |||||
| public void getLiveReplay() throws Exception { | |||||
| // [12, 11, 10, 9, 8, 7, 6, 5, 3, 2] | |||||
| WxMaGetLiveInfo list = this.wxService.getLiveService().getLiveReplay(11,0,10); | |||||
| assertNotNull(list); | |||||
| System.out.println(list.toString()); | |||||
| } | |||||
| @Test | |||||
| public void getLiveinfos() throws Exception { | |||||
| List<WxMaGetLiveInfo.RoomInfo> list = this.wxService.getLiveService().getLiveinfos(); | |||||
| assertNotNull(list); | |||||
| System.out.println(list.toString()); | |||||
| } | |||||
| } | |||||