From b0440e0faade0703859fc913f9585fb0b210c5a7 Mon Sep 17 00:00:00 2001 From: Binary Wang Date: Sun, 29 Nov 2020 23:39:17 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20#1886=20=E3=80=90=E5=B0=8F=E7=A8=8B?= =?UTF-8?q?=E5=BA=8F=E3=80=91=E5=88=9B=E5=BB=BA=E7=9B=B4=E6=92=AD=E9=97=B4?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=A2=9E=E5=8A=A0=E4=BA=8C=E7=BB=B4=E7=A0=81?= =?UTF-8?q?=E5=9C=B0=E5=9D=80=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wx/miniapp/api/WxMaLiveService.java | 7 +-- .../miniapp/api/impl/WxMaLiveServiceImpl.java | 61 ++++++++++--------- .../bean/live/WxMaCreateRoomResult.java | 30 +++++++++ .../api/impl/WxMaLiveServiceImplTest.java | 7 ++- 4 files changed, 68 insertions(+), 37 deletions(-) create mode 100644 weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/live/WxMaCreateRoomResult.java 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 index 96e6d879b..78cb4d497 100644 --- 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 @@ -1,9 +1,6 @@ package cn.binarywang.wx.miniapp.api; -import cn.binarywang.wx.miniapp.bean.live.WxMaAssistantResult; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveAssistantInfo; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveResult; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveRoomInfo; +import cn.binarywang.wx.miniapp.bean.live.*; import me.chanjar.weixin.common.error.WxErrorException; import java.util.List; @@ -41,7 +38,7 @@ public interface WxMaLiveService { * @return . * @throws WxErrorException . */ - Integer createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException; + WxMaCreateRoomResult createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException; /** * 删除直播间 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 index cc3b266dc..1fcd23e51 100644 --- 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 @@ -2,10 +2,7 @@ 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.live.WxMaAssistantResult; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveAssistantInfo; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveResult; -import cn.binarywang.wx.miniapp.bean.live.WxMaLiveRoomInfo; +import cn.binarywang.wx.miniapp.bean.live.*; import cn.binarywang.wx.miniapp.json.WxMaGsonBuilder; import com.google.common.base.Joiner; import com.google.gson.JsonObject; @@ -31,16 +28,19 @@ import java.util.Map; @Slf4j @AllArgsConstructor public class WxMaLiveServiceImpl implements WxMaLiveService { + private static final String ERR_CODE = "errcode"; + private static final String ROOM_ID = "roomId"; private final WxMaService wxMaService; @Override - public Integer createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException { + public WxMaCreateRoomResult createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException { String responseContent = this.wxMaService.post(CREATE_ROOM, WxMaGsonBuilder.create().toJson(roomInfo)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } - return jsonObject.get("roomId").getAsInt(); + + return WxMaGsonBuilder.create().fromJson(responseContent, WxMaCreateRoomResult.class); } @Override @@ -49,7 +49,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { map.put("id", roomId); String responseContent = this.wxMaService.post(DELETE_ROOM, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; @@ -59,7 +59,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { public boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException { String responseContent = this.wxMaService.post(EDIT_ROOM, WxMaGsonBuilder.create().toJson(roomInfo)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; @@ -68,10 +68,10 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { @Override public String getPushUrl(Integer roomId) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); + map.put(ROOM_ID, roomId); String responseContent = this.wxMaService.get(GET_PUSH_URL, Joiner.on("&").withKeyValueSeparator("=").join(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return jsonObject.get("pushAddr").getAsString(); @@ -80,13 +80,13 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { @Override public String getSharedCode(Integer roomId, String params) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); + map.put(ROOM_ID, roomId); if (null != params) { map.put("params", params); } String responseContent = this.wxMaService.get(GET_SHARED_CODE, Joiner.on("&").withKeyValueSeparator("=").join(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return jsonObject.get("cdnUrl").getAsString(); @@ -135,19 +135,21 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { JsonObject jsonObject = getLiveInfo(start, limit, map); return WxMaLiveResult.fromJson(jsonObject.toString()); } + private JsonObject getLiveInfo(Integer start, Integer limit, Map map) throws WxErrorException { if (map == null) { - map = new HashMap(2); + map = new HashMap<>(2); } map.put("start", start); map.put("limit", limit); String responseContent = wxMaService.post(GET_LIVE_INFO, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return jsonObject; } + @Override public WxMaLiveResult getLiveReplay(Integer roomId, Integer start, Integer limit) throws WxErrorException { return getLiveReplay("get_replay", roomId, start, limit); @@ -156,11 +158,11 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { @Override public boolean addGoodsToRoom(Integer roomId, List goodsIds) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); + map.put(ROOM_ID, roomId); map.put("ids", goodsIds); String responseContent = this.wxMaService.post(ADD_GOODS, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; @@ -169,38 +171,38 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { @Override public boolean addAssistant(Integer roomId, List users) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); + map.put(ROOM_ID, roomId); map.put("users", users); String responseContent = this.wxMaService.post(ADD_ASSISTANT, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; } @Override - public boolean modifyAssistant(Integer roomId, String username,String nickname) throws WxErrorException { + public boolean modifyAssistant(Integer roomId, String username, String nickname) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); - map.put("username",username); + map.put(ROOM_ID, roomId); + map.put("username", username); map.put("nickname", nickname); String responseContent = this.wxMaService.post(MODIFY_ASSISTANT, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; } @Override - public boolean removeAssistant(Integer roomId,String username) throws WxErrorException { + public boolean removeAssistant(Integer roomId, String username) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); - map.put("username",username); + map.put(ROOM_ID, roomId); + map.put("username", username); String responseContent = this.wxMaService.post(REMOVE_ASSISTANT, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return true; @@ -209,14 +211,13 @@ public class WxMaLiveServiceImpl implements WxMaLiveService { @Override public List getAssistantList(Integer roomId) throws WxErrorException { Map map = new HashMap<>(2); - map.put("roomId", roomId); + map.put(ROOM_ID, roomId); String responseContent = this.wxMaService.post(GET_ASSISTANT_LIST, WxMaGsonBuilder.create().toJson(map)); JsonObject jsonObject = GsonParser.parse(responseContent); - if (jsonObject.get("errcode").getAsInt() != 0) { + if (jsonObject.get(ERR_CODE).getAsInt() != 0) { throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp)); } return WxMaAssistantResult.fromJson(responseContent).getList(); } - } diff --git a/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/live/WxMaCreateRoomResult.java b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/live/WxMaCreateRoomResult.java new file mode 100644 index 000000000..56b4eb725 --- /dev/null +++ b/weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/live/WxMaCreateRoomResult.java @@ -0,0 +1,30 @@ +package cn.binarywang.wx.miniapp.bean.live; + +import com.google.gson.annotations.SerializedName; +import lombok.Data; + +import java.io.Serializable; + +/** + * 创建直播间接口返回. + * + * @author Binary Wang + * @date 2020-11-29 + */ +@Data +public class WxMaCreateRoomResult implements Serializable { + private static final long serialVersionUID = -335928442728127170L; + + /** + * "小程序直播" 小程序码 + * 当主播微信号没有在 “小程序直播“ 小程序实名认证 返回该字段 + */ + @SerializedName("qrcode_url") + private String qrcodeUrl; + + /** + * 房间ID + */ + @SerializedName("roomId") + private Integer roomId; +} 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 index 1d43b50be..b9a5b9412 100644 --- 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 @@ -1,6 +1,7 @@ package cn.binarywang.wx.miniapp.api.impl; import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.bean.live.WxMaCreateRoomResult; import cn.binarywang.wx.miniapp.bean.live.WxMaLiveResult; import cn.binarywang.wx.miniapp.bean.live.WxMaLiveRoomInfo; import cn.binarywang.wx.miniapp.test.ApiTestModule; @@ -14,6 +15,7 @@ import java.util.Arrays; import java.util.Calendar; import java.util.List; +import static org.assertj.core.api.Assertions.assertThat; import static org.testng.Assert.assertNotNull; /** @@ -50,8 +52,9 @@ public class WxMaLiveServiceImplTest { roomInfo.setCloseLike(0); roomInfo.setCloseGoods(0); roomInfo.setCloseComment(0); - Integer roomId = this.wxService.getLiveService().createRoom(roomInfo); - System.out.println(roomId); + WxMaCreateRoomResult result = this.wxService.getLiveService().createRoom(roomInfo); + assertNotNull(result); + assertThat(result.getRoomId()).isNotNull(); } @Test