-
Notifications
You must be signed in to change notification settings - Fork 1
/
ma_live.go
137 lines (118 loc) · 3.79 KB
/
ma_live.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package ma
import (
"encoding/json"
"github.com/cliod/wx-go/common"
"time"
)
type WxMaLiveService interface {
/* 创建直播间
调用此接口创建直播间,创建成功后将在直播间列表展示,调用额度:10000次/一天
文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#1 */
CreateRoom(*WxLiveCreateRoomRequest) (*WxLiveCreateRoomResult, error)
/* 编辑直播间
调用此接口创建直播间,创建成功后将在直播间列表展示,调用额度:10000次/一天
文档地址:https://developers.weixin.qq.com/miniprogram/dev/framework/liveplayer/studio-api.html#6 */
EditRoom(*WxLiveEditRoomRequest) (*WxLiveEditRoomResult, error)
// 获取直播房间列表.(分页)
GetLiveInfos() ([]*WxMaLiveRoomInfosResult, error)
// 获取所有直播间信息(没有分页直接获取全部)
GetLiveInfo(start, limit int) (*WxMaLiveResult, error)
// 获取直播房间回放数据信息.
GetLiveReplay(action string, roomId uint64, start, limit int) (*WxMaLiveResult, error)
/* 直播间导入商品
调用接口往指定直播间导入已入库的商品
调用频率, 调用额度:10000次/一天 */
AddGoodsToRoom(roomId uint64, goodsIds []uint64) error
}
type WxMaLiveServiceImpl struct {
service WxMaService
}
func newWxMaLiveService(service WxMaService) *WxMaLiveServiceImpl {
return &WxMaLiveServiceImpl{
service: service,
}
}
func (l *WxMaLiveServiceImpl) CreateRoom(info *WxLiveCreateRoomRequest) (*WxLiveCreateRoomResult, error) {
url := common.MaCreateRoom
var res WxLiveCreateRoomResult
err := l.service.PostFor(&res, url, common.PostJsonContentType, info)
return &res, err
}
func (l *WxMaLiveServiceImpl) EditRoom(info *WxLiveEditRoomRequest) (*WxLiveEditRoomResult, error) {
url := common.MaEditRoom
var res WxLiveEditRoomResult
err := l.service.PostFor(&res, url, common.PostJsonContentType, info)
return &res, err
}
func (l *WxMaLiveServiceImpl) GetLiveInfos() ([]*WxMaLiveRoomInfosResult, error) {
var arr []*WxMaLiveRoomInfosResult
start := 0
limit := 80
total := uint64(0)
var info WxMaLiveResult
for uint64(len(arr)) <= total {
if total != 0 && total <= uint64(start) {
break
}
lf, err := l.GetLiveInfo(start, limit)
if err != nil {
continue
}
if lf == nil {
return nil, err
}
info = *lf
arr = append(arr, info.RoomInfos...)
total = info.Total
start = len(arr)
time.Sleep(100 * time.Millisecond)
}
return arr, nil
}
func (l *WxMaLiveServiceImpl) GetLiveInfo(start, limit int) (*WxMaLiveResult, error) {
var res *WxMaLiveResult
b, err := l.getLiveInfo(start, limit, nil)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &res)
return res, err
}
func (l *WxMaLiveServiceImpl) GetLiveReplay(action string, roomId uint64, start, limit int) (*WxMaLiveResult, error) {
var res *WxMaLiveResult
param := map[string]interface{}{
"action": action,
"room_id": roomId,
}
b, err := l.getLiveInfo(start, limit, param)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &res)
return res, err
}
func (l *WxMaLiveServiceImpl) AddGoodsToRoom(roomId uint64, goodsIds []uint64) error {
param := map[string]interface{}{
"ids": goodsIds,
"roomId": roomId,
}
url := common.MaAddGoods
var res common.Err
err := l.service.PostFor(&res, url, common.PostJsonContentType, param)
if err != nil {
return err
}
return &res
}
// start 起始房间,0表示从第1个房间开始拉取
// limit 每次拉取的房间数量,建议100以内
func (l *WxMaLiveServiceImpl) getLiveInfo(start, limit int, param map[string]interface{}) ([]byte, error) {
url := common.MaGetLiveInfo
if param == nil {
param = map[string]interface{}{
"start": start,
"limit": limit,
}
}
return l.service.Post(url, common.PostJsonContentType, param)
}