RoomController.php 12 KB
<?php
namespace soa\controllers\v1;

use Yii;
use soa\controllers\BaseController;
use common\models\RoomNums;
use common\models\Video;
use common\models\Room;
use common\models\RoomsNumsReport;
/**
 * Room controller
 */
class RoomController extends BaseController
{

    /**
     * 弹幕服务器上传房间一些数目参数
     * @return mixed
     */
    public function actionSetnums()
    {
        //http://newboys.test.yoho.cn/yohoboyins/v4/qcloud/setLiveNums?parameters={"room":"2","likeNums":0,"onlineNums":0,"audienceNums":"11369"}
        $gets = $this->requests();

        $room_id = $gets['room'];
        if ($room_id) {
            //插入统计表
            $roomReportModel = new RoomsNumsReport();
            $roomReportModel->create($gets);
            //插入or更新数据表
            if (!$model = RoomNums::findOne(['room_id' => $room_id])) {
                $model = new RoomNums();
                $model->room_id = $room_id;
            }
            if (isset($gets['likeNums'])) {
                $model->like_num = $gets['likeNums'];
            }
            if (isset($gets['onlineNums'])) {
                $model->online_num = $gets['onlineNums'];
            }
            if (isset($gets['audienceNums'])) {
                $model->audience_num = $gets['audienceNums'];
            }
            if (isset($gets['vestOnlineNums'])) {
                $model->vest_online_num = $gets['vestOnlineNums'];
            }
            if ($model->save()) {
                $retArr = [
                    'code'     => Yii::$app->params['success_code'],
                    'messsage' => '保存成功'
                ];
            } else {
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '保存失败'
                ];
            }
        } else {
            $retArr = [
                'code'     => Yii::$app->params['failed_code'],
                'messsage' => '更新失败,缺少房间号'
            ];
        }
        $this->renderJson($retArr['code'], $retArr['messsage']);
    }

    /**
     * 弹幕服务器上传房间一些数目参数
     * @return mixed
     */
    public function actionSetvideoargs()
    {
        $gets = $this->requests();

        $room_id = $gets['room'];
        if ($room_id) {
            $model = new Video();
            $video = $model->getCurrentLivingVideo($room_id);

            if (isset($gets['likeNums'])) {
                $video->like_num = $gets['likeNums'];
            }
            if (isset($gets['audienceNums'])) {
                $video->audience_num = $gets['audienceNums'];
            }

            if ($video->save()) {
                $retArr = [
                    'code'     => Yii::$app->params['success_code'],
                    'messsage' => '保存成功'
                ];
            } else {
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '保存失败'
                ];
            }
        } else {
            $retArr = [
                'code'     => Yii::$app->params['failed_code'],
                'messsage' => '更新失败,缺少房间号'
            ];
        }
        $this->renderJson($retArr['code'], $retArr['messsage']);
    }

    /**
     * 弹幕服务器上报弹幕消息
     * @author yaoxiaofeng
     */
    public function actionSetlivecomment()
    {
        $gets = $this->requests();
        $datas = [];
        $room_id = $gets['room'];
        $datas['room_id'] = $room_id;
        $datas['uid'] = $gets['uid'];
        $datas['comment'] = isset($gets['msg']) ? $gets['msg'] : '';
        $datas['name'] = $gets['name'];
        $datas['avatar'] = $gets['avatar'];
        $datas['cmd'] = $gets['cmd'];
        $datas['create_time'] = $gets['create_time'];
        try {
            $result = Yii::$app->db->createCommand()->insert($this->getTableName($room_id), $datas)->execute();
            if ($result) {
                $retArr = [
                    'code'     => Yii::$app->params['success_code'],
                    'messsage' => '插入成功'
                ];
            } else {
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '插入失败'
                ];
            }
        } catch (\yii\db\Exception $e) {
            $retArr = [
                'code'     => Yii::$app->params['exception_code'],
                'messsage' => $e->getMessage()
            ];
        }
        $this->renderJson($retArr['code'], $retArr['messsage']);
    }

    public function actionGetlivingroomid()
    {
        return [10000, 10001];
    }

    /**
     * 获取禁言用户列表
     */
    public function actionGetforbiddenuids()
    {
        $forbidedUids = [];
        $rooms = (new \yii\db\Query())
            ->select(['room_id'])
            ->from('tbl_room')
            ->where(['status' => 1])
            ->limit(100)
            ->all();
        if ($rooms) {
            foreach ($rooms as $room) {
                $room_id = $room['room_id'];
                $uids = (new \yii\db\Query())
                    ->select(['uid'])
                    ->from('tbl_forbidden')
                    ->where(['room_id' => $room_id,'status'=>Yii::$app->params['config']['is_forbid_stat']])
                    ->all();
                if ($uids) {
                    foreach ($uids as $v) {
                        $forbidedUids[$room_id][] = $v['uid'];
                    }
                }
            }
        }
        $this->renderJson(Yii::$app->params['success_code'], '', $forbidedUids);
    }

    /**
     * 获取推流地址
     * @author yaoxiaofeng
     */
    public function actionGetpushflow()
    {
        $requests = $this->requests();
//        $room_id = $requests['room_id'];
        $result = (new \yii\db\Query())
            ->select('a.room_id,a.title,a.living,a.secret,b.channel_id,b.upstream_address')
            ->from('tbl_room a')
            ->leftJoin('tbl_room_qchannel b', 'a.room_id=b.room_id')
            ->where(['a.living' => [0,2]])
            ->all();
        if ($result) {
            foreach($result as $flow){
                $data[] = [
                    'url'        => $flow['upstream_address'].'&record=flv',
                    'name'       => $flow['title'],
                    'channel_id' => $flow['channel_id'],
                    'secret'     => $flow['secret'],
                    'cid'        => '',
                    'app'        => '1',
                    'id'         => $flow['room_id'],
                    'type'       => $flow['living'],
                ];
            }
        } else {
            $data = [];
        }
        $this->renderJson(Yii::$app->params['success_code'], '', $data);
    }

    /**
     * 设置直播开始结束状态
     * @author yaoxiaofeng
     */
    public function actionSetstat()
    {
        $requests = $this->requests();
        $status = $requests['living_stat'];
        $room_id = $requests['room_id'];
        $status_valid = [0,1];
        if (in_array($status, $status_valid) && $room_id) {
            try {
                $model = new Video();
                if($status==1){ //开始
                    $result = $model->setVideoStart($room_id);
                }elseif($status==0){//结束
                    $result = $model->setVideoStop($room_id);
                }
                if ($result) {
                    $retArr = [
                        'code'     => Yii::$app->params['success_code'],
                        'messsage' => '设置成功'
                    ];
                } else {
                    $retArr = [
                        'code'     => Yii::$app->params['failed_code'],
                        'messsage' => '设置失败'
                    ];
                }
            } catch (\Exception $e) {
                $retArr = [
                    'code'     => Yii::$app->params['exception_code'],
                    'messsage' => $e->getMessage()
                ];
            }
        } else {
            $retArr = [
                'code'     => Yii::$app->params['failed_code'],
                'messsage' => '请检查传参是否正确'
            ];
        }
        $this->renderJson($retArr['code'], $retArr['messsage']);
    }

    /**
     * 主播选择推流房间验证接口
     * @author yaoxiaofeng
     */
    public function actionValidmaster(){
        $requests = $this->requests();
        $secret = $requests['secret'];
        $room_id = $requests['room_id'];

        if($model = Room::findOne(['room_id'=>$room_id])){
            if($model->secret == $secret){
                $retArr = [
                    'code'     => Yii::$app->params['success_code'],
                    'messsage' => '验证成功'
                ];
            }else{
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '验证失败'
                ];
            }
        }else{
            $retArr = [
                'code'     => Yii::$app->params['failed_code'],
                'messsage' => '房间不存在'
            ];
        }
        $this->renderJson($retArr['code'], $retArr['messsage']);
    }

    /**
     * 直播中的所有房间
     */
    public function actionListing()
    {
        $list = Room::find()->where(['and',['living'=>1],['status'=>1]])->all();
        
        foreach ($list as $k=>$model){
            $row = [
                'room_id'=>(int)$model->room_id,
                'vest'=>$model->vest,
                'is_addup'=>$model->is_addup
            ];
            $list[$k] = $row;
        }
        
        $this->renderJson(Yii::$app->params['success_code'],'',$list);
    }

    /**
     * 获取推流房间
     * -二期推流需求
     * @return [type] [description]
     */
    public function actionGetpushroom()
    {
        $requests = $this->requests();
        $secret = $requests['secret'];
        do{
            if(empty(trim($requests['secret']))){
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '缺少密钥'
                ];
                break;
            }
            if($model = Room::findOne(['secret'=>$secret])){
                if($model->living==1){
                    $retArr = [
                        'code'     => Yii::$app->params['failed_code'],
                        'messsage' => '该房间正在直播中'
                    ];
                    break;
                }
                $channel_id = $upstream_address = '';
                if($model->roomQchannel){
                    $channel_id = $model->roomQchannel->channel_id;
                    $upstream_address = $model->roomQchannel->upstream_address;
                }
                $retArr = [
                    'code'     => Yii::$app->params['success_code'],
                    'messsage' => '',
                    'data'     => [
                        'url'        => $upstream_address.'&record=flv',
                        'name'       => $model->title,
                        'channel_id' => $channel_id,
                        'secret'     => $model->secret,
                        'cid'        => '',
                        'app'        => '1',
                        'id'         => $model->room_id,
                        'type'       => $model->living,
                    ]
                ];
            }else{
                $retArr = [
                    'code'     => Yii::$app->params['failed_code'],
                    'messsage' => '房间不存在'
                ];
            }
            break;
        }while (false);
        
        $this->renderJson($retArr['code'], $retArr['messsage'], isset($retArr['data']) ? $retArr['data']:'');
    }

    /**
     * 获取评论表名称
     * @param $room_id
     * @return string
     */
    private function getTableName($room_id)
    {
        return 'tbl_live_comment' . str_pad((int)($room_id % 10), 2, '0', STR_PAD_LEFT);
    }
}