🎨 优化规范部分代码

This commit is contained in:
Binary Wang 2020-06-30 16:54:40 +08:00
parent c60fda48d2
commit a00d67a58a
4 changed files with 39 additions and 45 deletions

View File

@ -46,35 +46,35 @@ public interface WxMaLiveService {
/** /**
* 获取所有直播间信息没有分页直接获取全部 * 获取所有直播间信息没有分页直接获取全部
* *
* @return * @return .
* @throws WxErrorException * @throws WxErrorException .
*/ */
List<WxMaLiveResult.RoomInfo> getLiveinfos() throws WxErrorException; List<WxMaLiveResult.RoomInfo> getLiveInfos() throws WxErrorException;
/** /**
* 获取直播房间回放数据信息. * 获取直播房间回放数据信息.
* *
* @param action 获取回放 * @param action 获取回放
* @param room_id 直播间 id * @param roomId 直播间 id
* @param start 起始拉取视频start = 0 表示从第 1 个视频片段开始拉取 * @param start 起始拉取视频start = 0 表示从第 1 个视频片段开始拉取
* @param limit 每次拉取的个数上限不要设置过大建议 100 以内 * @param limit 每次拉取的个数上限不要设置过大建议 100 以内
* @return * @return .
* @throws WxErrorException * @throws WxErrorException .
*/ */
WxMaLiveResult getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException; WxMaLiveResult getLiveReplay(String action, Integer roomId, Integer start, Integer limit) throws WxErrorException;
/** /**
* 获取直播房间回放数据信息. * 获取直播房间回放数据信息.
* <p> * <p>
* 获取回放 默认get_replay * 获取回放 默认get_replay
* *
* @param room_id 直播间 id * @param roomId 直播间 id
* @param start 起始拉取视频start = 0 表示从第 1 个视频片段开始拉取 * @param start 起始拉取视频start = 0 表示从第 1 个视频片段开始拉取
* @param limit 每次拉取的个数上限不要设置过大建议 100 以内 * @param limit 每次拉取的个数上限不要设置过大建议 100 以内
* @return * @return .
* @throws WxErrorException * @throws WxErrorException .
*/ */
WxMaLiveResult getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException; WxMaLiveResult getLiveReplay(Integer roomId, Integer start, Integer limit) throws WxErrorException;
/** /**
* 直播间导入商品 * 直播间导入商品

View File

@ -8,6 +8,7 @@ import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.google.gson.JsonParser; import com.google.gson.JsonParser;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.WxType; import me.chanjar.weixin.common.WxType;
import me.chanjar.weixin.common.error.WxError; import me.chanjar.weixin.common.error.WxError;
import me.chanjar.weixin.common.error.WxErrorException; import me.chanjar.weixin.common.error.WxErrorException;
@ -24,10 +25,11 @@ import java.util.Map;
* *
* @author <a href="https://github.com/yjwang3300300">yjwang</a> * @author <a href="https://github.com/yjwang3300300">yjwang</a>
*/ */
@Slf4j
@AllArgsConstructor @AllArgsConstructor
public class WxMaLiveServiceImpl implements WxMaLiveService { public class WxMaLiveServiceImpl implements WxMaLiveService {
private static final JsonParser JSON_PARSER = new JsonParser(); private static final JsonParser JSON_PARSER = new JsonParser();
private WxMaService wxMaService; private final WxMaService wxMaService;
@Override @Override
public Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorException { public Integer createRoom(WxMaLiveInfo.RoomInfo roomInfo) throws WxErrorException {
@ -41,19 +43,19 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
@Override @Override
public WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException { public WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException {
JsonObject jsonObject = getJsonObject(start, limit, null); JsonObject jsonObject = getLiveInfo(start, limit, null);
return WxMaLiveResult.fromJson(jsonObject.toString()); return WxMaLiveResult.fromJson(jsonObject.toString());
} }
@Override @Override
public List<WxMaLiveResult.RoomInfo> getLiveinfos() throws WxErrorException { public List<WxMaLiveResult.RoomInfo> getLiveInfos() throws WxErrorException {
List<WxMaLiveResult.RoomInfo> results = new ArrayList<>(); List<WxMaLiveResult.RoomInfo> results = new ArrayList<>();
Integer start = 0; int start = 0;
Integer limit = 80; Integer limit = 80;
Integer tatal = 0; Integer total = 0;
WxMaLiveResult liveInfo = null; WxMaLiveResult liveInfo;
do { do {
if (tatal != 0 && tatal <= start) { if (total != 0 && total <= start) {
break; break;
} }
liveInfo = getLiveInfo(start, limit); liveInfo = getLiveInfo(start, limit);
@ -61,29 +63,30 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
return null; return null;
} }
results.addAll(liveInfo.getRoomInfos()); results.addAll(liveInfo.getRoomInfos());
tatal = liveInfo.getTotal(); total = liveInfo.getTotal();
start = results.size(); start = results.size();
try { try {
Thread.sleep(100); Thread.sleep(100);
} catch (InterruptedException e) { } catch (InterruptedException e) {
e.printStackTrace(); log.error("InterruptedException", e);
} }
} while (results.size() <= tatal); } while (results.size() <= total);
return results; return results;
} }
@Override @Override
public WxMaLiveResult getLiveReplay(String action, Integer room_id, Integer start, Integer limit) throws WxErrorException { public WxMaLiveResult getLiveReplay(String action, Integer roomId, Integer start, Integer limit) throws WxErrorException {
Map<String, Object> map = new HashMap(4); Map<String, Object> map = new HashMap<>(4);
map.put("action", action); map.put("action", action);
map.put("room_id", room_id); map.put("room_id", roomId);
JsonObject jsonObject = getJsonObject(start, limit, map); JsonObject jsonObject = getLiveInfo(start, limit, map);
return WxMaLiveResult.fromJson(jsonObject.toString()); return WxMaLiveResult.fromJson(jsonObject.toString());
} }
@Override @Override
public WxMaLiveResult getLiveReplay(Integer room_id, Integer start, Integer limit) throws WxErrorException { public WxMaLiveResult getLiveReplay(Integer roomId, Integer start, Integer limit) throws WxErrorException {
return getLiveReplay("get_replay", room_id, start, limit); return getLiveReplay("get_replay", roomId, start, limit);
} }
@Override @Override
@ -99,18 +102,9 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
return true; return true;
} }
/** private JsonObject getLiveInfo(Integer start, Integer limit, Map<String, Object> map) throws WxErrorException {
* 包装一下
*
* @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) { if (map == null) {
map = new HashMap(2); map = new HashMap<>(2);
} }
map.put("start", start); map.put("start", start);
map.put("limit", limit); map.put("limit", limit);

View File

@ -70,7 +70,7 @@ public class WxMaLiveServiceImplTest {
@Test @Test
public void getLiveinfos() throws Exception { public void getLiveinfos() throws Exception {
List<WxMaLiveResult.RoomInfo> list = this.wxService.getLiveService().getLiveinfos(); List<WxMaLiveResult.RoomInfo> list = this.wxService.getLiveService().getLiveInfos();
assertNotNull(list); assertNotNull(list);
System.out.println(list.toString()); System.out.println(list.toString());
} }