微信小程序房间列表直播状态获取
微信小程序房间列表怎么查询直播状态,这里我直接上小程序js代码
var e = getApp(), t = e.requirejs("core"), i = e.requirejs("foxui"),livePlayer = requirePlugin('live-player-plugin'); Page({ /** * 页面的初始数据 */ data: { navbar:1, roomlist:[], isLogin:1, }, liveStatuObj:{ '101':'直播中', '102':'未开始', '103':'已结束', '104':'禁播', '105':'暂停中', '106':'异常', '107':'已过期' }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { this.getRooms(); }, /** * 获取直播房间 */ getRooms: function(){ var _this = this; //这个接口是请求后台获取直播的房间列表,g.get 网络请求函数,请换上自己的网络请求函数或用小程序官方的 t.get("live/index",'',function(rr){ if( !rr.error ){ let roomList = rr.list; _this.setData({ roomlist:roomList, isLogin:rr.is_login }); //遍历房间列表,查询每个房间的直播状态 for(var ii in roomList) { var roomId = roomList[ii].roomid; //获取直播状态 liveStatus(roomId,ii); } //获取直播状态 function liveStatus(roomId,listKey){ livePlayer.getLiveStatus({ room_id: roomId}) .then(res=>{ var roomStatus = res.liveStatus; console.log( _this.liveStatuObj[roomStatus],roomStatus,'DCR163') roomList[listKey].status_str = _this.liveStatuObj[roomStatus]; _this.setData({ roomlist:roomList }); //更新状态,这里是获取到的房间状态后,再更新给后台,这样下次请求列表时就是正确的房间状态了 t.get('live/upDateStatus',{roomid:roomId,status:roomStatus},function(e){ console.log(e); console.log('12132') }) }) .catch(err => { console.log('get live status', err) }); } } }) }, nav: function() { this.setData({ nav_mask: !this.data.nav_mask }); }, nav2: function() { this.setData({ nav_mask2: !this.data.nav_mask2 }); }, redirectUrl: function(res){ var datas = res.currentTarget.dataset; e.redirectUrl(datas.url); }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh: function () { this.getRooms(); wx.stopPullDownRefresh();//停止当前页面下拉刷新 }, /** * 页面上拉触底事件的处理函数 */ onReachBottom: function () { }, /** * 用户点击右上角分享 */ onShareAppMessage: function (res) { console.log(res) return {'title':'您的好友喊你来看直播啦!'} } })
php后台房间列表接口
/** * dcr163 小程序获取直播房间接口缓存 * @param int $start 起始的房间 start = 0 表示从第 1 个房间开始拉取 * @param int $limit 每次拉取的个数上限 * @return array|void */ public function getRoomList($start=0,$limit=20){ global $_W; $uniacid = $_W['uniacid']; $time = time(); //首先查询是否已过缓存 毕竟坑爹的腾讯 一天的接口调用次数只有 500次 $sql = 'SELECT id,expire_time FROM '.tablename('vcshop_wxapp_live_cache').' WHERE start=:start AND `limit`=:limit AND uniacid=:uniacid'; $info = pdo_fetch($sql,array(':start'=>$start,':limit'=>$limit,':uniacid'=>$uniacid)); $upData = array(); $inData = array(); if( !empty($info) ) { if( $time > $info['expire_time'] ) { $upData = ['expire_time'=>$time+300,'update_time'=>$time]; }else { return $this->getLiveList($start,$limit); } } else { $inData = array( 'start' => $start, 'uniacid' => $uniacid, 'limit' => $limit, 'create_time' => $time, 'expire_time' => $time+300, //5分钟后可以更新缓存获取数据 ); } $accessToken = $this->getAccessToken(); if (is_error($accessToken)) { return error(-1, 'accessToken获取失败'); } //获取直播房间列表接口 $apiUrl = 'http://api.weixin.qq.com/wxa/business/getliveinfo?access_token='.$this->getAccessToken(); $params = array(); $params['start'] = $start; $params['limit'] = $limit; load()->func('communication'); $res = ihttp_post($apiUrl,json_encode($params)); //更新缓存的时间 if( !empty($upData) ) { pdo_update('vcshop_wxapp_live_cache',$upData,['start'=>$start,'limit'=>$limit,'uniacid'=>$uniacid]); } //插入缓存 if( !empty($inData) ) { pdo_insert('vcshop_wxapp_live_cache',$inData); } if (is_error($res)) { return error(-2, $res['message']); } $content = json_decode($res['content'],true); if( $content['errcode'] ) return error(-2,$content['errmsg']); //获取微信接口的列表 $roomList = $content['room_info']; $roomList = array_reverse($roomList); $roomIdArr = []; foreach ( $roomList as &$v ) { $roomIdArr[] = $v['roomid']; if( $v['goods'] ) { $v['goods'] = json_encode($v['goods']); } } unset($v); //获取数据库中和接口一样的条件数据 $lists = $this->getLiveList($start,$limit); if( !empty($lists) ) { $listKeys = []; foreach ( $lists as $v ){ $listKeys[] = $v['roomid']; } //这里拿数据库中的数据和接口查询的数据对比,如果数据中存在,但是接口不存在,则更新数据库为已删除 s $deleteRooms = array_diff($listKeys,$roomIdArr); if( !empty($deleteRooms) ){ $sql = 'UPDATE '.tablename('vcshop_wxapp_live').' SET `is_delete`=1 WHERE uniacid=:uniacid AND `is_delete`=0 AND roomid IN ('.implode(',',$deleteRooms).')'; pdo_query($sql,array(':uniacid'=>$uniacid)); } } //查询是否已经有保存过在数据库中 $roomIdStr = implode(',',$roomIdArr); $sql = 'SELECT id,roomid FROM '.tablename('vcshop_wxapp_live').' WHERE roomid IN ('.$roomIdStr.') AND uniacid=:uniacid'; $existsLists = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid']),'roomid'); $existsRooms = array_keys($existsLists); //已经存在的房间更新房间信息 $upDataRooms = array_intersect($existsRooms,$roomIdArr); if( !empty($upDataRooms) ) { foreach($roomList as $k=>$v){ if( in_array($v['roomid'],$upDataRooms) ) continue; //这里不更新房间状态,因为第一次之后api提供的状态不准确 $srcData = [ 'name'=>$roomList[$k]['name'], 'anchor_name'=>$roomList[$k]['anchor_name'], 'cover_img'=>$roomList[$k]['cover_img'], 'start_time'=>$roomList[$k]['start_time'], 'end_time'=>$roomList[$k]['end_time'], 'share_img'=>$roomList[$k]['share_img'], 'goods'=>$roomList[$k]['goods'], ]; pdo_update('vcshop_wxapp_live',$srcData,['uniacid'=>$_W['uniacid'],'roomid'=>$v['roomid']]); } } //接口中返回的数据不存在数据库中则插入 $inertRooms = array_diff($roomIdArr,$existsRooms); if( !empty($inertRooms) ) { foreach( $roomList as $k => $v ){ if( !in_array($v['roomid'],$inertRooms) ) continue; $roomList[$k]['uniacid'] = $uniacid; pdo_insert('vcshop_wxapp_live',$roomList[$k]); } } return $this->getLiveList($start,$limit); } /** * dcr163获取制定的范围数据 * @param $start 开始数据 * @param $limit 查询条数 * @return array */ public function getLiveList($start,$limit) { global $_W; $sql = 'SELECT * FROM '.tablename('vcshop_wxapp_live').' WHERE is_delete=0 AND uniacid=:uniacid ORDER BY roomid DESC LIMIT '.$start.','.$limit; $lists = pdo_fetchall($sql,array(':uniacid'=>$_W['uniacid'])); return $lists; }
Dcr163的博客
http://dcr163.cn/242.html(转载时请注明本文出处及文章链接)