Skip to content

Commit edc04cb

Browse files
authored
🆕 #1863 【小程序】增加删除直播间、编辑直播间、获取直播间推流地址、获取直播间分享二维码等接口
1 parent f301912 commit edc04cb

File tree

4 files changed

+162
-2
lines changed

4 files changed

+162
-2
lines changed

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/api/WxMaLiveService.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ public interface WxMaLiveService {
1818
String GET_LIVE_INFO = "https://api.weixin.qq.com/wxa/business/getliveinfo";
1919
String CREATE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/create";
2020
String ADD_GOODS = "https://api.weixin.qq.com/wxaapi/broadcast/room/addgoods";
21+
String DELETE_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom";
22+
String EDIT_ROOM = "https://api.weixin.qq.com/wxaapi/broadcast/room/editroom";
23+
String GET_PUSH_URL = "https://api.weixin.qq.com/wxaapi/broadcast/room/getpushurl";
24+
String GET_SHARED_CODE = "https://api.weixin.qq.com/wxaapi/broadcast/room/getsharedcode";
2125

2226
/**
2327
* 创建直播间
@@ -33,6 +37,61 @@ public interface WxMaLiveService {
3337
*/
3438
Integer createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException;
3539

40+
/**
41+
* 删除直播间
42+
* <pre>
43+
* 调用额度:10000次/一天
44+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#5
45+
* http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/room/deleteroom?access_token=ACCESS_TOKEN
46+
* </pre>
47+
*
48+
* @param roomId 直播间id
49+
* @return .
50+
* @throws WxErrorException .
51+
*/
52+
boolean deleteRoom(Integer roomId) throws WxErrorException;
53+
54+
/**
55+
* 编辑直播间
56+
* <pre>
57+
* 调用此接口编辑直播间,调用额度:10000次/一天
58+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#6
59+
* http请求方式:POST https://api.weixin.qq.com/wxaapi/broadcast/room/editroom?access_token=ACCESS_TOKEN
60+
* </pre>
61+
*
62+
* @param roomInfo 直播间信息
63+
* @return .
64+
* @throws WxErrorException .
65+
*/
66+
boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException;
67+
68+
/**
69+
* 获取直播间推流地址
70+
* <pre>
71+
* 调用额度:10000次/一天
72+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#7
73+
* http请求方式:GET https://api.weixin.qq.com/wxaapi/broadcast/room/getpushurl?access_token=ACCESS_TOKEN
74+
* </pre>
75+
*
76+
* @param roomId 直播间id
77+
* @return .
78+
* @throws WxErrorException .
79+
*/
80+
String getPushUrl(Integer roomId) throws WxErrorException;
81+
82+
/**
83+
* 获取直播间分享二维码
84+
* <pre>
85+
* 调用额度:10000次/一天
86+
* 文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#8
87+
* http请求方式:GET https://api.weixin.qq.com/wxaapi/broadcast/room/getsharedcode?access_token=ACCESS_TOKEN
88+
* </pre>
89+
*
90+
* @param roomId 直播间id
91+
* @return .
92+
* @throws WxErrorException .
93+
*/
94+
String getSharedCode(Integer roomId, String params) throws WxErrorException;
3695
/**
3796
* 获取直播房间列表.(分页)
3897
*

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import cn.binarywang.wx.miniapp.bean.live.WxMaLiveResult;
66
import cn.binarywang.wx.miniapp.bean.live.WxMaLiveRoomInfo;
77
import cn.binarywang.wx.miniapp.util.json.WxMaGsonBuilder;
8+
import com.google.common.base.Joiner;
89
import com.google.gson.JsonObject;
910
import lombok.AllArgsConstructor;
1011
import lombok.extern.slf4j.Slf4j;
@@ -40,6 +41,55 @@ public Integer createRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException {
4041
return jsonObject.get("roomId").getAsInt();
4142
}
4243

44+
@Override
45+
public boolean deleteRoom(Integer roomId) throws WxErrorException {
46+
Map<String, Object> map = new HashMap<>(2);
47+
map.put("id", roomId);
48+
String responseContent = this.wxMaService.post(DELETE_ROOM, WxMaGsonBuilder.create().toJson(map));
49+
JsonObject jsonObject = GsonParser.parse(responseContent);
50+
if (jsonObject.get("errcode").getAsInt() != 0) {
51+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
52+
}
53+
return true;
54+
}
55+
56+
@Override
57+
public boolean editRoom(WxMaLiveRoomInfo roomInfo) throws WxErrorException {
58+
String responseContent = this.wxMaService.post(EDIT_ROOM, WxMaGsonBuilder.create().toJson(roomInfo));
59+
JsonObject jsonObject = GsonParser.parse(responseContent);
60+
if (jsonObject.get("errcode").getAsInt() != 0) {
61+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
62+
}
63+
return true;
64+
}
65+
66+
@Override
67+
public String getPushUrl(Integer roomId) throws WxErrorException {
68+
Map<String, Object> map = new HashMap<>(2);
69+
map.put("roomId", roomId);
70+
String responseContent = this.wxMaService.get(GET_PUSH_URL, Joiner.on("&").withKeyValueSeparator("=").join(map));
71+
JsonObject jsonObject = GsonParser.parse(responseContent);
72+
if (jsonObject.get("errcode").getAsInt() != 0) {
73+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
74+
}
75+
return jsonObject.get("pushAddr").getAsString();
76+
}
77+
78+
@Override
79+
public String getSharedCode(Integer roomId, String params) throws WxErrorException {
80+
Map<String, Object> map = new HashMap<>(2);
81+
map.put("roomId", roomId);
82+
if (null != params) {
83+
map.put("params", params);
84+
}
85+
String responseContent = this.wxMaService.get(GET_SHARED_CODE, Joiner.on("&").withKeyValueSeparator("=").join(map));
86+
JsonObject jsonObject = GsonParser.parse(responseContent);
87+
if (jsonObject.get("errcode").getAsInt() != 0) {
88+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.MiniApp));
89+
}
90+
return jsonObject.get("cdnUrl").getAsString();
91+
}
92+
4393
@Override
4494
public WxMaLiveResult getLiveInfo(Integer start, Integer limit) throws WxErrorException {
4595
JsonObject jsonObject = getLiveInfo(start, limit, null);

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/bean/live/WxMaLiveRoomInfo.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
public class WxMaLiveRoomInfo implements Serializable {
1212
private static final long serialVersionUID = 7745775280267417154L;
1313

14+
/**
15+
* 直播间ID
16+
*/
17+
private Integer id;
1418
/**
1519
* 直播间名字,最短3个汉字,最长17个汉字,1个汉字相当于2个字符
1620
**/
@@ -39,6 +43,10 @@ public class WxMaLiveRoomInfo implements Serializable {
3943
* 主播副号微信号,如果未实名认证,需要先前往“小程序直播”小程序进行实名验证, 小程序二维码链接:https://res.wx.qq.com/op_res/BbVNeczA1XudfjVqCVoKgfuWe7e3aUhokktRVOqf_F0IqS6kYR--atCpVNUUC3zr
4044
**/
4145
private String subAnchorWechat;
46+
/**
47+
* 创建者微信号,不传入则此直播间所有成员可见。传入则此房间仅创建者、管理员、超管、直播间主播可见
48+
**/
49+
private String createrWechat;
4250
/**
4351
* 分享图,填入mediaID(mediaID获取后,三天内有效);图片mediaID的获取,请参考以下文档: https://developers.weixin.qq.com/doc/offiaccount/Asset_Management/New_temporary_materials.html;直播间分享图,图片规则:建议像素800*640,大小不超过1M;
4452
**/

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaLiveServiceImplTest.java

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,13 @@ public void createRoom() throws Exception {
3737
roomInfo.setName("订阅通知直播间");
3838
roomInfo.setCoverImg(mediaUpload.getMediaId());
3939
Calendar c = Calendar.getInstance();
40-
c.set(2020, Calendar.SEPTEMBER, 10, 8, 0);
40+
c.set(2020, Calendar.DECEMBER, 10, 8, 0);
4141
roomInfo.setStartTime(c.getTimeInMillis() / 1000);
42-
c.set(2020, Calendar.SEPTEMBER, 10, 12, 0);
42+
c.set(2020, Calendar.DECEMBER, 10, 12, 0);
4343
roomInfo.setEndTime(c.getTimeInMillis() / 1000);
4444
roomInfo.setAnchorName("鹏军_专业小程序开发");
4545
roomInfo.setAnchorWechat("pengjun939961241");
46+
roomInfo.setCreaterWechat("pengjun939961241");
4647
roomInfo.setShareImg(mediaUpload.getMediaId());
4748
roomInfo.setType(1);
4849
roomInfo.setScreenType(1);
@@ -53,6 +54,48 @@ public void createRoom() throws Exception {
5354
System.out.println(roomId);
5455
}
5556

57+
@Test
58+
public void deletRoom() throws Exception {
59+
this.wxService.getLiveService().deleteRoom(29);
60+
}
61+
62+
@Test
63+
public void editRoom() throws Exception {
64+
//上传临时素材
65+
// WxMediaUploadResult mediaUpload = this.wxService.getMediaService().uploadMedia("image", new File("E:\\1.png"));
66+
67+
WxMaLiveRoomInfo roomInfo = new WxMaLiveRoomInfo();
68+
roomInfo.setId(39);
69+
roomInfo.setName("修改订阅通知直播间");
70+
roomInfo.setCoverImg("http://mmbiz.qpic.cn/mmbiz_png/omYktZNGamuBLBYlP2FjpIL2AHoiayH8HXeZRibtXDMesHn5aevEaM4etUVwfnX1HHqrXBDY3KPgT8MIlqbtqX8Q/0");
71+
Calendar c = Calendar.getInstance();
72+
c.set(2021, Calendar.SEPTEMBER, 10, 8, 0);
73+
roomInfo.setStartTime(c.getTimeInMillis() / 1000);
74+
c.set(2021, Calendar.SEPTEMBER, 10, 12, 0);
75+
roomInfo.setEndTime(c.getTimeInMillis() / 1000);
76+
roomInfo.setAnchorName("鹏军_专业小程序开发");
77+
roomInfo.setAnchorWechat("pengjun939961241");
78+
roomInfo.setShareImg("http://mmbiz.qpic.cn/mmbiz_png/omYktZNGamuBLBYlP2FjpIL2AHoiayH8HXeZRibtXDMesHn5aevEaM4etUVwfnX1HHqrXBDY3KPgT8MIlqbtqX8Q/0");
79+
roomInfo.setType(1);
80+
roomInfo.setScreenType(1);
81+
roomInfo.setCloseLike(0);
82+
roomInfo.setCloseGoods(0);
83+
roomInfo.setCloseComment(0);
84+
boolean editRoom = this.wxService.getLiveService().editRoom(roomInfo);
85+
System.out.println(editRoom);
86+
}
87+
@Test
88+
public void getPushUrl() throws Exception {
89+
String result = this.wxService.getLiveService().getPushUrl(39);
90+
System.out.println(result);
91+
}
92+
93+
@Test
94+
public void getSharedCode() throws Exception {
95+
String result = this.wxService.getLiveService().getSharedCode(39, null);
96+
System.out.println(result);
97+
}
98+
5699
@Test
57100
public void getLiveInfo() throws Exception {
58101
WxMaLiveResult list = this.wxService.getLiveService().getLiveInfo(0, 10);

0 commit comments

Comments
 (0)