diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java new file mode 100644 index 000000000..8a39e2957 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java @@ -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; + +/** + *
+ *  直播相关操作接口.
+ *  Created by yjwang on 2020/4/5.
+ * 
+ * + * @author yjwang + */ +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 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; + +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java index 072907ba1..6b4a71566 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java @@ -287,4 +287,11 @@ public interface WxMaService { * @return . */ WxMaCloudService getCloudService(); + + /** + * 获取直播接口服务对象 + * + * @return . + */ + WxMaLiveService getLiveService(); } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java new file mode 100644 index 000000000..6f1a154bb --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java @@ -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; + +/** + *
+ *  Created by yjwang on 2020/4/5.
+ * 
+ * + * @author yjwang + */ +@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 getLiveinfos() throws WxErrorException { + List 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 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 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; + } +} diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java index a2897a4ac..88984c353 100644 --- a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaServiceImpl.java @@ -60,6 +60,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp roomInfos; + /** + * 获取回放源视频列表 + */ + @SerializedName("live_replay") + private List 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; + } + + /** + * 商品列表 + */ + @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; + } + + +} diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java new file mode 100644 index 000000000..5ccd16a03 --- /dev/null +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java @@ -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 yjwang + */ +@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 list = this.wxService.getLiveService().getLiveinfos(); + assertNotNull(list); + System.out.println(list.toString()); + } +}