Skip to content

Commit

Permalink
binarywang#1429 增加小程序直播类相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
yjwang committed Apr 5, 2020
1 parent 7e0bfb2 commit cacd99c
Show file tree
Hide file tree
Showing 6 changed files with 273 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 .
*/
List<WxMaGetLiveInfo.RoomInfo> getLiveInfo(Integer start, Integer limit) throws WxErrorException;

/**
*
* 获取直播房间回放数据信息.
*
* @param action 获取回放
* @param room_id 直播间 id
* @param start 起始拉取视频,start = 0 表示从第 1 个视频片段开始拉取
* @param limit 每次拉取的个数上限,不要设置过大,建议 100 以内
* @return
* @throws WxErrorException
*/
List<WxMaGetLiveInfo.LiveReplay> 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
*/
List<WxMaGetLiveInfo.LiveReplay> getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -287,4 +287,11 @@ public interface WxMaService {
* @return .
*/
WxMaCloudService getCloudService();

/**
* 获取直播接口服务对象
*
* @return .
*/
WxMaLiveService getLiveService();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
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 com.google.gson.reflect.TypeToken;
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.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 List<WxMaGetLiveInfo.RoomInfo> getLiveInfo(Integer start, Integer limit) throws WxErrorException {
JsonObject jsonObject = getJsonObject(start, limit, null);
return WxMaGsonBuilder.create().fromJson(jsonObject.get("room_info").toString(), new TypeToken<List<WxMaGetLiveInfo.RoomInfo>>() {
}.getType());
}

@Override
public List<WxMaGetLiveInfo.LiveReplay> 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 WxMaGsonBuilder.create().fromJson(jsonObject.get("live_replay").toString(), new TypeToken<List<WxMaGetLiveInfo.LiveReplay>>() {
}.getType());
}

@Override
public List<WxMaGetLiveInfo.LiveReplay> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public class WxMaServiceImpl implements WxMaService, RequestHttp<CloseableHttpCl
private WxMaExpressService expressService = new WxMaExpressServiceImpl(this);
private WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(this);
private WxMaCloudService cloudService = new WxMaCloudServiceImpl(this);
private WxMaLiveService liveService = new WxMaLiveServiceImpl(this);

private int retrySleepMillis = 1000;
private int maxRetryTimes = 5;
Expand Down Expand Up @@ -415,4 +416,9 @@ public WxMaExpressService getExpressService() {
public WxMaCloudService getCloudService() {
return this.cloudService;
}

@Override
public WxMaLiveService getLiveService() {
return this.liveService;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package cn.binarywang.wx.miniapp.bean;

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;

/**
* 直播列表
*/
@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")

This comment has been minimized.

Copy link
@gguan

gguan Apr 15, 2020

拼写错误

This comment has been minimized.

Copy link
@binarywang

binarywang Apr 17, 2020

拟修复

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;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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 {
List<WxMaGetLiveInfo.RoomInfo> list = this.wxService.getLiveService().getLiveInfo(0,10);
assertNotNull(list);
System.out.println(list.stream().map(e -> e.getRoomid()).collect(Collectors.toList()).toString());
}

@Test
public void getLiveReplay() throws Exception {
// [12, 11, 10, 9, 8, 7, 6, 5, 3, 2]
List<WxMaGetLiveInfo.LiveReplay> list = this.wxService.getLiveService().getLiveReplay(11,0,10);
assertNotNull(list);
System.out.println(list.toString());
}
}

0 comments on commit cacd99c

Please sign in to comment.