22
33import cn .binarywang .wx .miniapp .api .WxMaLiveService ;
44import cn .binarywang .wx .miniapp .api .WxMaService ;
5- import cn .binarywang .wx .miniapp .bean .live .WxMaAssistantResult ;
6- import cn .binarywang .wx .miniapp .bean .live .WxMaLiveAssistantInfo ;
7- import cn .binarywang .wx .miniapp .bean .live .WxMaLiveResult ;
8- import cn .binarywang .wx .miniapp .bean .live .WxMaLiveRoomInfo ;
5+ import cn .binarywang .wx .miniapp .bean .live .*;
96import cn .binarywang .wx .miniapp .json .WxMaGsonBuilder ;
107import com .google .common .base .Joiner ;
118import com .google .gson .JsonObject ;
3128@ Slf4j
3229@ AllArgsConstructor
3330public class WxMaLiveServiceImpl implements WxMaLiveService {
31+ private static final String ERR_CODE = "errcode" ;
32+ private static final String ROOM_ID = "roomId" ;
3433 private final WxMaService wxMaService ;
3534
3635 @ Override
37- public Integer createRoom (WxMaLiveRoomInfo roomInfo ) throws WxErrorException {
36+ public WxMaCreateRoomResult createRoom (WxMaLiveRoomInfo roomInfo ) throws WxErrorException {
3837 String responseContent = this .wxMaService .post (CREATE_ROOM , WxMaGsonBuilder .create ().toJson (roomInfo ));
3938 JsonObject jsonObject = GsonParser .parse (responseContent );
40- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
39+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
4140 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
4241 }
43- return jsonObject .get ("roomId" ).getAsInt ();
42+
43+ return WxMaGsonBuilder .create ().fromJson (responseContent , WxMaCreateRoomResult .class );
4444 }
4545
4646 @ Override
@@ -49,7 +49,7 @@ public boolean deleteRoom(Integer roomId) throws WxErrorException {
4949 map .put ("id" , roomId );
5050 String responseContent = this .wxMaService .post (DELETE_ROOM , WxMaGsonBuilder .create ().toJson (map ));
5151 JsonObject jsonObject = GsonParser .parse (responseContent );
52- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
52+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
5353 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
5454 }
5555 return true ;
@@ -59,7 +59,7 @@ public boolean deleteRoom(Integer roomId) throws WxErrorException {
5959 public boolean editRoom (WxMaLiveRoomInfo roomInfo ) throws WxErrorException {
6060 String responseContent = this .wxMaService .post (EDIT_ROOM , WxMaGsonBuilder .create ().toJson (roomInfo ));
6161 JsonObject jsonObject = GsonParser .parse (responseContent );
62- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
62+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
6363 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
6464 }
6565 return true ;
@@ -68,10 +68,10 @@ public boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException {
6868 @ Override
6969 public String getPushUrl (Integer roomId ) throws WxErrorException {
7070 Map <String , Object > map = new HashMap <>(2 );
71- map .put ("roomId" , roomId );
71+ map .put (ROOM_ID , roomId );
7272 String responseContent = this .wxMaService .get (GET_PUSH_URL , Joiner .on ("&" ).withKeyValueSeparator ("=" ).join (map ));
7373 JsonObject jsonObject = GsonParser .parse (responseContent );
74- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
74+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
7575 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
7676 }
7777 return jsonObject .get ("pushAddr" ).getAsString ();
@@ -80,13 +80,13 @@ public String getPushUrl(Integer roomId) throws WxErrorException {
8080 @ Override
8181 public String getSharedCode (Integer roomId , String params ) throws WxErrorException {
8282 Map <String , Object > map = new HashMap <>(2 );
83- map .put ("roomId" , roomId );
83+ map .put (ROOM_ID , roomId );
8484 if (null != params ) {
8585 map .put ("params" , params );
8686 }
8787 String responseContent = this .wxMaService .get (GET_SHARED_CODE , Joiner .on ("&" ).withKeyValueSeparator ("=" ).join (map ));
8888 JsonObject jsonObject = GsonParser .parse (responseContent );
89- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
89+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
9090 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
9191 }
9292 return jsonObject .get ("cdnUrl" ).getAsString ();
@@ -135,19 +135,21 @@ public WxMaLiveResult getLiveReplay(String action, Integer roomId, Integer start
135135 JsonObject jsonObject = getLiveInfo (start , limit , map );
136136 return WxMaLiveResult .fromJson (jsonObject .toString ());
137137 }
138+
138139 private JsonObject getLiveInfo (Integer start , Integer limit , Map <String , Object > map ) throws WxErrorException {
139140 if (map == null ) {
140- map = new HashMap (2 );
141+ map = new HashMap <> (2 );
141142 }
142143 map .put ("start" , start );
143144 map .put ("limit" , limit );
144145 String responseContent = wxMaService .post (GET_LIVE_INFO , WxMaGsonBuilder .create ().toJson (map ));
145146 JsonObject jsonObject = GsonParser .parse (responseContent );
146- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
147+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
147148 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
148149 }
149150 return jsonObject ;
150151 }
152+
151153 @ Override
152154 public WxMaLiveResult getLiveReplay (Integer roomId , Integer start , Integer limit ) throws WxErrorException {
153155 return getLiveReplay ("get_replay" , roomId , start , limit );
@@ -156,11 +158,11 @@ public WxMaLiveResult getLiveReplay(Integer roomId, Integer start, Integer limit
156158 @ Override
157159 public boolean addGoodsToRoom (Integer roomId , List <Integer > goodsIds ) throws WxErrorException {
158160 Map <String , Object > map = new HashMap <>(2 );
159- map .put ("roomId" , roomId );
161+ map .put (ROOM_ID , roomId );
160162 map .put ("ids" , goodsIds );
161163 String responseContent = this .wxMaService .post (ADD_GOODS , WxMaGsonBuilder .create ().toJson (map ));
162164 JsonObject jsonObject = GsonParser .parse (responseContent );
163- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
165+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
164166 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
165167 }
166168 return true ;
@@ -169,38 +171,38 @@ public boolean addGoodsToRoom(Integer roomId, List<Integer> goodsIds) throws WxE
169171 @ Override
170172 public boolean addAssistant (Integer roomId , List <WxMaLiveAssistantInfo > users ) throws WxErrorException {
171173 Map <String , Object > map = new HashMap <>(2 );
172- map .put ("roomId" , roomId );
174+ map .put (ROOM_ID , roomId );
173175 map .put ("users" , users );
174176 String responseContent = this .wxMaService .post (ADD_ASSISTANT , WxMaGsonBuilder .create ().toJson (map ));
175177 JsonObject jsonObject = GsonParser .parse (responseContent );
176- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
178+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
177179 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
178180 }
179181 return true ;
180182 }
181183
182184 @ Override
183- public boolean modifyAssistant (Integer roomId , String username ,String nickname ) throws WxErrorException {
185+ public boolean modifyAssistant (Integer roomId , String username , String nickname ) throws WxErrorException {
184186 Map <String , Object > map = new HashMap <>(2 );
185- map .put ("roomId" , roomId );
186- map .put ("username" ,username );
187+ map .put (ROOM_ID , roomId );
188+ map .put ("username" , username );
187189 map .put ("nickname" , nickname );
188190 String responseContent = this .wxMaService .post (MODIFY_ASSISTANT , WxMaGsonBuilder .create ().toJson (map ));
189191 JsonObject jsonObject = GsonParser .parse (responseContent );
190- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
192+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
191193 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
192194 }
193195 return true ;
194196 }
195197
196198 @ Override
197- public boolean removeAssistant (Integer roomId ,String username ) throws WxErrorException {
199+ public boolean removeAssistant (Integer roomId , String username ) throws WxErrorException {
198200 Map <String , Object > map = new HashMap <>(2 );
199- map .put ("roomId" , roomId );
200- map .put ("username" ,username );
201+ map .put (ROOM_ID , roomId );
202+ map .put ("username" , username );
201203 String responseContent = this .wxMaService .post (REMOVE_ASSISTANT , WxMaGsonBuilder .create ().toJson (map ));
202204 JsonObject jsonObject = GsonParser .parse (responseContent );
203- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
205+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
204206 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
205207 }
206208 return true ;
@@ -209,14 +211,13 @@ public boolean removeAssistant(Integer roomId,String username) throws WxErrorExc
209211 @ Override
210212 public List <WxMaAssistantResult .Assistant > getAssistantList (Integer roomId ) throws WxErrorException {
211213 Map <String , Object > map = new HashMap <>(2 );
212- map .put ("roomId" , roomId );
214+ map .put (ROOM_ID , roomId );
213215 String responseContent = this .wxMaService .post (GET_ASSISTANT_LIST , WxMaGsonBuilder .create ().toJson (map ));
214216 JsonObject jsonObject = GsonParser .parse (responseContent );
215- if (jsonObject .get ("errcode" ).getAsInt () != 0 ) {
217+ if (jsonObject .get (ERR_CODE ).getAsInt () != 0 ) {
216218 throw new WxErrorException (WxError .fromJson (responseContent , WxType .MiniApp ));
217219 }
218220 return WxMaAssistantResult .fromJson (responseContent ).getList ();
219221 }
220222
221-
222223}
0 commit comments