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

use Yii;
use soa\controllers\BaseController;
use common\models\RoomNums;
use common\models\Video;
use common\models\Room;

/**
 * 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) {
            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])
                    ->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])
            ->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,
            ];
            $list[$k] = $row;
        }
        
        $this->renderJson(Yii::$app->params['success_code'],'',$list);
    }

    public function actionLog(){
        $message = "test";
        $level = "";
        Yii::$app->log->getLogger()->log($message, $level);
    }

    public function actionTest(){
//        $re = \common\lib\QcloudApi\Client::self()->CreateRecord("16093425727657864680", date('Y-m-d H:i:s',time()+120), '');
        $re = \common\lib\QcloudApi\Client::self()->DescribeRecordList("16093425727657864680",1,10);

//        $re = \common\lib\QcloudApi\Client::self()->StopRecord("16093425727657524184", "131525");
        var_dump($re);
        //http://200013340.vod.myqcloud.com/200013340_6e79da1e65d140d887cdbb7f3ebadf79.f20.mp4
    }

    /**
     * 获取评论表名称
     * @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);
    }
}