Browse Source

:new: #1895 【小程序】直播增加成员管理相关接口

Binary Wang 4 years ago
parent
commit
13356d1cd5

+ 55 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveMemberService.java

@@ -0,0 +1,55 @@
+package cn.binarywang.wx.miniapp.api;
+
+import com.google.gson.JsonArray;
+import me.chanjar.weixin.common.error.WxErrorException;
+
+
+/**
+ * 【小程序直播】成员管理接口.
+ * https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/role-manage.html
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2021 -02-15
+ */
+public interface WxMaLiveMemberService {
+  /**
+   * 1.设置成员角色
+   * 调用此接口设置小程序直播成员的管理员、运营者和主播角色
+   * 调用额度:10000次/一天
+   * 请求URL : https://api.weixin.qq.com/wxaapi/broadcast/role/addrole?access_token=
+   *
+   * @param username 用户的微信号
+   * @param role     设置用户的角色,取值[1-管理员,2-主播,3-运营者],设置超级管理员将无效
+   * @return the string
+   * @throws WxErrorException the wx error exception
+   */
+  String addRole(String username, int role) throws WxErrorException;
+
+  /**
+   * 2.解除成员角色
+   * 调用此接口移除小程序直播成员的管理员、运营者和主播角色
+   * 调用额度:10000次/一天
+   * 请求URL:https://api.weixin.qq.com/wxaapi/broadcast/role/deleterole?access_token=
+   *
+   * @param username 用户的微信号
+   * @param role     设置用户的角色,取值[1-管理员,2-主播,3-运营者],设置超级管理员将无效
+   * @return the string
+   * @throws WxErrorException the wx error exception
+   */
+  String deleteRole(String username, int role) throws WxErrorException;
+
+  /**
+   * 3.查询成员列表
+   * 调用此接口查询小程序直播成员列表
+   * 调用额度:10000次/一天
+   * 请求URL:https://api.weixin.qq.com/wxaapi/broadcast/role/getrolelist?access_token=
+   *
+   * @param role    查询的用户角色,取值 [-1-所有成员, 0-超级管理员,1-管理员,2-主播,3-运营者],默认-1
+   * @param offset  起始偏移量, 默认0
+   * @param limit   查询个数,最大30,默认10
+   * @param keyword 搜索的微信号或昵称,不传则返回全部
+   * @return . json array
+   * @throws WxErrorException the wx error exception
+   */
+  JsonArray listByRole(Integer role, Integer offset, Integer limit, String keyword) throws WxErrorException;
+}

+ 7 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaService.java

@@ -357,6 +357,13 @@ public interface WxMaService extends WxService {
   WxMaLiveGoodsService getLiveGoodsService();
 
   /**
+   * 获取直播成员管理接口服务对象
+   *
+   * @return . live service
+   */
+  WxMaLiveMemberService getLiveMemberService();
+
+  /**
    * 获取ocr实现接口服务对象
    *
    * @return 。

+ 6 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/BaseWxMaServiceImpl.java

@@ -60,6 +60,7 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
   private final WxMaCloudService cloudService = new WxMaCloudServiceImpl(this);
   private final WxMaLiveService liveService = new WxMaLiveServiceImpl(this);
   private final WxMaLiveGoodsService liveGoodsService = new WxMaLiveGoodsServiceImpl(this);
+  private final WxMaLiveMemberService liveMemberService = new WxMaLiveMemberServiceImpl(this);
   private final WxOcrService ocrService = new WxMaOcrServiceImpl(this);
   private final WxImgProcService imgProcService = new WxMaImgProcServiceImpl(this);
   private Map<String, WxMaConfig> configMap;
@@ -484,6 +485,11 @@ public abstract class BaseWxMaServiceImpl<H, P> implements WxMaService, RequestH
   }
 
   @Override
+  public WxMaLiveMemberService getLiveMemberService() {
+    return this.liveMemberService;
+  }
+
+  @Override
   public WxOcrService getOcrService() {
     return this.ocrService;
   }

+ 9 - 14
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveGoodsServiceImpl.java

@@ -11,6 +11,7 @@ import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import lombok.RequiredArgsConstructor;
 import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.util.json.GsonHelper;
 import me.chanjar.weixin.common.util.json.GsonParser;
 
 import java.io.Serializable;
@@ -18,7 +19,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.LiveGoods.*;
+import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Broadcast.Goods.*;
 
 /**
  * <pre>
@@ -34,32 +35,26 @@ public class WxMaLiveGoodsServiceImpl implements WxMaLiveGoodsService {
   @Override
   public WxMaLiveResult addGoods(WxMaLiveGoodInfo goods) throws WxErrorException {
     return WxMaLiveResult.fromJson(this.wxMaService.post(ADD_GOODS,
-      WxMaGsonBuilder.create().toJson(ImmutableMap.of("goodsInfo", goods))));
+      GsonHelper.buildJsonObject("goodsInfo", goods)));
   }
 
   @Override
   public boolean resetAudit(Integer auditId, Integer goodsId) throws WxErrorException {
-    Map<String, Integer> map = new HashMap<>(4);
-    map.put("auditId", auditId);
-    map.put("goodsId", goodsId);
-    this.wxMaService.post(RESET_AUDIT_GOODS, WxMaGsonBuilder.create().toJson(map));
+    this.wxMaService.post(RESET_AUDIT_GOODS,
+      GsonHelper.buildJsonObject("auditId", auditId, "goodsId", goodsId));
     return true;
   }
 
   @Override
   public String auditGoods(Integer goodsId) throws WxErrorException {
-    Map<String, Integer> map = new HashMap<>(2);
-    map.put("goodsId", goodsId);
-    String responseContent = this.wxMaService.post(AUDIT_GOODS, WxMaGsonBuilder.create().toJson(map));
-    JsonObject jsonObject = GsonParser.parse(responseContent);
-    return jsonObject.get("auditId").getAsString();
+    String responseContent = this.wxMaService.post(AUDIT_GOODS,
+      GsonHelper.buildJsonObject("goodsId", goodsId));
+    return GsonParser.parse(responseContent).get("auditId").getAsString();
   }
 
   @Override
   public boolean deleteGoods(Integer goodsId) throws WxErrorException {
-    Map<String, Integer> map = new HashMap<>(2);
-    map.put("goodsId", goodsId);
-    this.wxMaService.post(DELETE_GOODS, WxMaGsonBuilder.create().toJson(map));
+    this.wxMaService.post(DELETE_GOODS, GsonHelper.buildJsonObject("goodsId", goodsId));
     return true;
   }
 

+ 42 - 0
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveMemberServiceImpl.java

@@ -0,0 +1,42 @@
+package cn.binarywang.wx.miniapp.api.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaLiveMemberService;
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Broadcast.Role;
+import com.google.gson.JsonArray;
+import lombok.RequiredArgsConstructor;
+import me.chanjar.weixin.common.error.WxErrorException;
+import me.chanjar.weixin.common.util.json.GsonHelper;
+import me.chanjar.weixin.common.util.json.GsonParser;
+
+import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Broadcast.Role.LIST_BY_ROLE;
+
+/**
+ * .
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2021-02-15
+ */
+@RequiredArgsConstructor
+public class WxMaLiveMemberServiceImpl implements WxMaLiveMemberService {
+  private final WxMaService service;
+
+  @Override
+  public String addRole(String username, int role) throws WxErrorException {
+    return this.service.post(Role.ADD_ROLE,
+      GsonHelper.buildJsonObject("username", username, "role", role));
+  }
+
+  @Override
+  public String deleteRole(String username, int role) throws WxErrorException {
+    return this.service.post(Role.DELETE_ROLE,
+      GsonHelper.buildJsonObject("username", username, "role", role));
+  }
+
+  @Override
+  public JsonArray listByRole(Integer role, Integer offset, Integer limit, String keyword) throws WxErrorException {
+    final String response = this.service.get(LIST_BY_ROLE, GsonHelper.buildJsonObject("role", role, "offset", offset,
+      "limit", limit, "keyword", keyword).toString());
+    return GsonParser.parse(response).getAsJsonArray("list");
+  }
+}

+ 11 - 11
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImpl.java

@@ -18,7 +18,7 @@ import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 
-import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Live.*;
+import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.Broadcast.*;
 
 /**
  * <pre>
@@ -36,7 +36,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
 
   @Override
   public WxMaCreateRoomResult createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException {
-    String responseContent = this.wxMaService.post(CREATE_ROOM, WxMaGsonBuilder.create().toJson(roomInfo));
+    String responseContent = this.wxMaService.post(Room.CREATE_ROOM, WxMaGsonBuilder.create().toJson(roomInfo));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -49,7 +49,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
   public boolean deleteRoom(Integer roomId) throws WxErrorException {
     Map<String, Object> map = new HashMap<>(2);
     map.put("id", roomId);
-    String responseContent = this.wxMaService.post(DELETE_ROOM, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.DELETE_ROOM, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -59,7 +59,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
 
   @Override
   public boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException {
-    String responseContent = this.wxMaService.post(EDIT_ROOM, WxMaGsonBuilder.create().toJson(roomInfo));
+    String responseContent = this.wxMaService.post(Room.EDIT_ROOM, WxMaGsonBuilder.create().toJson(roomInfo));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -71,7 +71,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
   public String getPushUrl(Integer roomId) throws WxErrorException {
     Map<String, Object> map = new HashMap<>(2);
     map.put(ROOM_ID, roomId);
-    String responseContent = this.wxMaService.get(GET_PUSH_URL, Joiner.on("&").withKeyValueSeparator("=").join(map));
+    String responseContent = this.wxMaService.get(Room.GET_PUSH_URL, Joiner.on("&").withKeyValueSeparator("=").join(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -86,7 +86,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
     if (null != params) {
       map.put("params", params);
     }
-    String responseContent = this.wxMaService.get(GET_SHARED_CODE, Joiner.on("&").withKeyValueSeparator("=").join(map));
+    String responseContent = this.wxMaService.get(Room.GET_SHARED_CODE, Joiner.on("&").withKeyValueSeparator("=").join(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -162,7 +162,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
     Map<String, Object> map = new HashMap<>(2);
     map.put(ROOM_ID, roomId);
     map.put("ids", goodsIds);
-    String responseContent = this.wxMaService.post(ADD_GOODS, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.ADD_GOODS, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -175,7 +175,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
     Map<String, Object> map = new HashMap<>(2);
     map.put(ROOM_ID, roomId);
     map.put("users", users);
-    String responseContent = this.wxMaService.post(ADD_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.ADD_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -189,7 +189,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
     map.put(ROOM_ID, roomId);
     map.put("username", username);
     map.put("nickname", nickname);
-    String responseContent = this.wxMaService.post(MODIFY_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.MODIFY_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -202,7 +202,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
     Map<String, Object> map = new HashMap<>(2);
     map.put(ROOM_ID, roomId);
     map.put("username", username);
-    String responseContent = this.wxMaService.post(REMOVE_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.REMOVE_ASSISTANT, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
@@ -214,7 +214,7 @@ public class WxMaLiveServiceImpl implements WxMaLiveService {
   public List<WxMaAssistantResult.Assistant> getAssistantList(Integer roomId) throws WxErrorException {
     Map<String, Object> map = new HashMap<>(2);
     map.put(ROOM_ID, roomId);
-    String responseContent = this.wxMaService.post(GET_ASSISTANT_LIST, WxMaGsonBuilder.create().toJson(map));
+    String responseContent = this.wxMaService.post(Room.GET_ASSISTANT_LIST, WxMaGsonBuilder.create().toJson(map));
     JsonObject jsonObject = GsonParser.parse(responseContent);
     if (jsonObject.get(ERR_CODE).getAsInt() != 0) {
       throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));

+ 39 - 21
weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/constant/WxMaApiUrlConstants.java

@@ -157,28 +157,46 @@ public class WxMaApiUrlConstants {
     String GET_JSAPI_TICKET_URL = "https://api.weixin.qq.com/cgi-bin/ticket/getticket";
   }
 
-  public interface LiveGoods {
-    String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add";
-    String RESET_AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit";
-    String AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit";
-    String DELETE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete";
-    String UPDATE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update";
-    String GET_GOODS_WARE_HOUSE = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse";
-    String GET_APPROVED_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved";
-  }
-
-  public interface Live {
+  public interface Broadcast {
     String GET_LIVE_INFO = "https://api.weixin.qq.com/wxa/business/getliveinfo";
-    String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create";
-    String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods";
-    String DELETE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom";
-    String EDIT_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/editroom";
-    String GET_PUSH_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/getpushurl";
-    String GET_SHARED_CODE = "https://api.weixin.qq.com/wxaapi/broadcast/room/getsharedcode";
-    String ADD_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/addassistant";
-    String MODIFY_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/modifyassistant";
-    String REMOVE_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/removeassistant";
-    String GET_ASSISTANT_LIST = "https://api.weixin.qq.com/wxaapi/broadcast/room/getassistantlist";
+
+    /**
+     * 直播间管理相关接口
+     */
+    interface Room {
+      String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create";
+      String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods";
+      String DELETE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom";
+      String EDIT_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/editroom";
+      String GET_PUSH_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/getpushurl";
+      String GET_SHARED_CODE = "https://api.weixin.qq.com/wxaapi/broadcast/room/getsharedcode";
+      String ADD_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/addassistant";
+      String MODIFY_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/modifyassistant";
+      String REMOVE_ASSISTANT = "https://api.weixin.qq.com/wxaapi/broadcast/room/removeassistant";
+      String GET_ASSISTANT_LIST = "https://api.weixin.qq.com/wxaapi/broadcast/room/getassistantlist";
+    }
+
+    /**
+     * 直播商品管理相关接口
+     */
+    interface Goods {
+      String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/add";
+      String RESET_AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/resetaudit";
+      String AUDIT_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/audit";
+      String DELETE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/delete";
+      String UPDATE_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/update";
+      String GET_GOODS_WARE_HOUSE = "https://api.weixin.qq.com/wxa/business/getgoodswarehouse";
+      String GET_APPROVED_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/goods/getapproved";
+    }
+
+    /**
+     * 小程序直播成员管理接口
+     */
+    interface Role {
+      String ADD_ROLE = "https://api.weixin.qq.com/wxaapi/broadcast/role/addrole";
+      String DELETE_ROLE = "https://api.weixin.qq.com/wxaapi/broadcast/role/deleterole";
+      String LIST_BY_ROLE = "https://api.weixin.qq.com/wxaapi/broadcast/role/getrolelist";
+    }
   }
 
   public interface Media {

+ 40 - 0
weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveMemberServiceImplTest.java

@@ -0,0 +1,40 @@
+package cn.binarywang.wx.miniapp.api.impl;
+
+import cn.binarywang.wx.miniapp.api.WxMaService;
+import cn.binarywang.wx.miniapp.test.ApiTestModule;
+import com.google.gson.JsonArray;
+import com.google.inject.Inject;
+import me.chanjar.weixin.common.error.WxErrorException;
+import org.testng.annotations.Guice;
+import org.testng.annotations.Test;
+
+/**
+ * 测试.
+ *
+ * @author <a href="https://github.com/binarywang">Binary Wang</a>
+ * @date 2021-02-15
+ */
+@Test
+@Guice(modules = ApiTestModule.class)
+public class WxMaLiveMemberServiceImplTest {
+  @Inject
+  private WxMaService wxService;
+
+  @Test
+  public void testAddRole() throws WxErrorException {
+    final String result = this.wxService.getLiveMemberService().addRole("abc", 1);
+    System.out.println(result);
+  }
+
+  @Test
+  public void testDeleteRole() throws WxErrorException {
+    final String result = this.wxService.getLiveMemberService().deleteRole("abc", 1);
+    System.out.println(result);
+  }
+
+  @Test
+  public void testListByRole() throws WxErrorException {
+    final JsonArray result = this.wxService.getLiveMemberService().listByRole(null, null, null, null);
+    System.out.println(result);
+  }
+}