forked from binarywang/WxJava
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
yjwang
committed
Apr 5, 2020
1 parent
7e0bfb2
commit cacd99c
Showing
6 changed files
with
273 additions
and
0 deletions.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/WxMaGetLiveInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong. |
||
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; | ||
} | ||
|
||
|
||
} |
48 changes: 48 additions & 0 deletions
48
...java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
拼写错误