This is a Lengux blog.

We sat and drank with the sun on our shoulders and felt like free men. We could have been tarring the roof of one of our own houses. We were the lords of all creation. As for Andy,he spent that break hunkered in the shade,a strange little smile on his face,watching us drink his beer.

小程序直播间

uni-app对接微信小程序直播

uni-app对接微信小程序直播

1.登录微信小程序后台-点击>设置->第三方设置->添加直播插件

2.添加直播组件后 >点击<详情>记录这两个参数直播插件的appid和最新版本号

  1. 打开项目,找到manifest.json这个文件

代码:

1
2
3
4
5
6
"plugins" : {
"live-player-plugin" : {
"version" : "1.1.9", //最新直播组件版本号
"provider" : "wx********0" //直播appid
}
}

4.获取直播间列表

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

/**
* 获取直播列表
*/
public function getLive(){
//获取access_token
if($_COOKIE['access_token']){
$token = $_COOKIE['access_token'];
}else{
$token=$this->getAccessToken();
setcookie("access_token", $token, time()+7200);
}
$page = i('page', 0); //当前页码
$rows = i('rows', 10); //每页记录条数
//请求直播间列表接口
$live_list_url='https://api.weixin.qq.com/wxa/business/getliveinfo?access_token='.$token;
//需要传递的参数
$data = array(
"start"=>$page,
"limit"=>$rows
);
$data = json_encode($data);
$res=$this->http_request($live_list_url,$data);
//获得直播间列表
$result = json_decode($res, true);
$this->render('default', $result);
}
/**
* 获取access_token
*/
public function getAccessToken(){
$appid='wx***********25';//小程序appid
$appsecret='6ce*********d';//小程序appsecret
$url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;
$res=$this->http_request($url);
$result = json_decode($res, true);
$access_token=$result['access_token'];
return $access_token;
}
//https请求(支持GET和POST)
function http_request($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if(!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
//var_dump(curl_error($curl));
curl_close($curl);
return $output;
}

5.uni-app(vue.js)请求列表信息:
methods: {

getLiveList: function () {
let that = this;
var params = {page: this.page};
that.$.request({
url: this.Config.URL.get_live_list,
data: params,
success: function (data, status, msg, code) {
if (data.room_info.length > 0) {
for (var r = 0; r < data.room_info.length; r++) {
data.room_info[r]['start_time'] = dateUtil.dateUtils.format(that.$.datetimeFormatter(data.room_info[r]['start_time']));
}
that.setData({
live_rows: that.live_rows.concat(data.room_info),
});

} else {
that.setData({flag: !1, ispage: !1})
}
}
});
},
}
6.获取的列表信息循环列表显示(根据页面需求自己写页面)
需要在列表中加onclick点击事件,把当前房间的roomid传递到点击事件中
7.在点击事件中使用 navigator 组件跳转进入直播间
//点击跳转直播页
liveDetail(roomid){
let roomId = roomid; // 填写具体的房间号,可通过下面【获取直播房间列表】 API 获取
let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/index/live', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数(如示例中的path和pid参数),后续可以在分享卡片链接和跳转至商详页时获取,详见【获取自定义参数】、【直播间到商详页面携带参数】章节(上限600个字符,超过部分会被截断)
wx.navigateTo({
url: `plugin-private://wx2b0*******70/pages/live-player-plugin?room_id=${roomId}&custom_params=${customParams}`
})
},

user@ui-verse:~$
Hey!

In the sea of coding, I am Lengux, pursuing creativity and user experience.

 友情链接
 标签
Made with 💛 by Lengux and some fantastic contributors! hexo blog framework
豫ICP备2022014432号-1