LiveController.php 11 KB
<?php
namespace backend\controllers;

use Yii;
use app\models\Master;
use app\models\Room;
use backend\components\Pagination;
use backend\widgets\Filter;
use backend\widgets\YHGImage\Common\Images as CommonImages;
use app\models\Video;

/**
 * Live controller
 */
class LiveController extends BaseController
{

    /**
     * 直播间
     * @return type
     */
    public function actionRoom()
    {
        
        $filter = Filter::self()->livingStatus()->keyword()->submit()->reset();
        if ($condition = $filter->get_where(true)){
            $model = \app\models\Room::condition($condition);
        }else{
            $model = \app\models\Room::find();
        }
        //var_dump($condition);
        
        $count = clone $model;
        $pagination = new Pagination(['totalCount' =>$count->count()]);
        $list = $model
            ->with('master')
            ->offset($pagination->offset)->limit($pagination->limit)
            ->orderBy(['create_time'=>SORT_DESC,'update_time'=>SORT_DESC])
            ->all();
        //var_dump($list);
        
        return $this->render('room',[
            'filter'=>$filter,
            'pagination'=>$pagination,
            'list'=>$list,
        ]);
    }
    
    /**
     * 主播
     * @return type
     */
    public function actionMaster()
    {
        $filter = Filter::self()->masterName()->submit()->reset();
        if ($condition = $filter->get_where(true)){
            $model = Master::condition($condition);
        }else{
            $model = Master::find();
        }

        $count = clone $model;
        $pagination = new Pagination([
            'totalCount' => $count->count(),
        ]);
        $masters = $model
            ->orderBy(['create_time'=>SORT_DESC,'update_time'=>SORT_DESC])
            ->offset($pagination->offset)->limit($pagination->limit)
            ->all();
        return $this->render('master',[
            'filter'=>$filter,
            'pagination'=>$pagination,
            'list'=>$masters,
        ]);
    }
    
    /**
     * 视频
     * @return type
     */
    public function actionVideo()
    {
    	$condition = Yii::$app->request->get();
    	$query = Video::condition($condition);
    	$pagination = new Pagination(['totalCount'=> $query->count()]);
    	$videoList = $query->offset($pagination->offset)
            ->limit($pagination->limit)->all();
    	$liveMasterIds = [];
    	$masterList = new \stdClass();
		foreach($videoList as $video) {
			if(!empty($video->master_id)) {
				$liveMasterIds[] = $video->master_id;
			}
		}
		if(!empty($liveMasterIds)){
			$masterList = Master::find()->where(['in', 'master_id', $liveMasterIds])->all();
		}
        return $this->render('video', ['videoList' => $videoList,'condition'=> $condition, 'masterList' => $masterList, 'pagination' => $pagination]);
    }

    /**
     * 视频编辑
     * 
     * @return
     */
    public function actionVideoedit()
    {
    	if($posts = Yii::$app->request->Post())
    	{
    		$model = Video::findOne($posts['id']);
    		if(!empty($model))
    		{
    			$model->pic = $posts['pic'];
    			$model->url = $posts['url'];
    			$model->title = $posts['title'];
    			if($model->save())
    			{
    				Yii::$app->session->setFlash('success', '编辑成功。');
    			}
    		}
    		else 
    		{
    			Yii::$app->session->setFlash('error', '保存失败。');
    		}
    		return $this->refresh();
    	}
    	else 
    	{
    		$id = Yii::$app->request->get('id', 0);
    		$video = Video::findOne(['id'=> $id]);
    		if(empty($video))
    		{
    			Yii::$app->session->setFlash('error', '找不到该视频');
    			return $this->redirect($this->_refer);
    		}
    		//图片上传组件
    		$uploader = \backend\widgets\UploadImage::self()->logo('pic', $video->pic);
    		return $this->render('videoedit', ['video' => $video, 'uploader' => $uploader]);
    	}
    }
    
    /**
     * 视频状态设置
     * @return 
     */
    public function actionVideosetstate()
    {
    	if($posts = Yii::$app->request->post()) {
    		$status = Video::updateAll(['status' => intval($posts['status']),'update_time'=>time()], ['id' => $posts['videoId']]);
    		if($status)
    		{
    			$this->renderJson(200, '设置成功');
    		}
    		else
    		{
    			$this->renderJson(403,'设置失败');
    		}
    	}
    	else
    	{
    		$this->renderJson(403,'post方式');
    	}
    }
    
    /**
     * 添加主播
     * @return type
     */
    public function actionAddmaster()
    {
        $model = new Master();

        if ($posts = Yii::$app->request->post()) {
            if(empty($posts['pic']) || empty($posts['name'])){
                Yii::$app->session->setFlash('warning', '昵称或头像不能为空');
                return $this->redirect('/live/addmaster');
            }
            $master = Master::findOne(['name'=>$posts['name']]);
            if(!empty($master)){
                Yii::$app->session->setFlash('warning', '主播名称已存在');
                return $this->redirect('/live/addmaster');
            }
            $model->name = trim($posts['name']);
            $model->meta = trim($posts['meta']);
            $model->pic = $posts['pic'];
            $model->create_time = $model->update_time = time();
            if($model->save()){
                Yii::$app->session->setFlash('success', '保存成功。');
            }else{
                Yii::$app->session->setFlash('error', '保存失败。');
            }
            return $this->redirect('/live/master');
        }
        
        //图片上传组件
        $uploader = \backend\widgets\UploadImage::self()->logo('pic');
        
        return $this->render('add_master',[
            'row'=> $model,
            'uploader'=>$uploader,
            'action' => '/live/addmaster'
        ]);
    }

    /**
     * 编辑主播
     * @return type
     */
    public function actionEditmaster()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }

        $model = Master::findOne($id);
        if (!$model){
            Yii::$app->session->setFlash('error', '找不到该主播');
            return $this->redirect($this->_refer);
        }

        if(Yii::$app->request->isPost){
            $posts = Yii::$app->request->post();
            $model->name = $posts['name'];
            $model->meta = $posts['meta'];
            $model->pic = $posts['pic'];

            if($model->save()){
                Yii::$app->session->setFlash('success', '编辑成功。');
            }else{
                Yii::$app->session->setFlash('error', '编辑失败。');
            }
            return $this->refresh();
        }
        //图片上传组件
        $uploader = \backend\widgets\UploadImage::self()->logo('pic',$model->pic);

        return $this->render('add_master',[
            'row'=>$model,
            'uploader'=>$uploader,
            'action'=>'/live/editmaster/'.$id
        ]);
    }

    /**
     * 删除主播
     * @return type
     */
    public function actionDelmaster(){
        
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        
        $model = new Master;
        if (!$model = $model->findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该主播');
            return $this->redirect($this->_refer);
        }
        
        if($model->delete()){
            Yii::$app->session->setFlash('success', '删除成功');
        }else{
            Yii::$app->session->setFlash('error', '删除失败。');
        }
            
        return $this->redirect($this->_refer);
    }
    
    /**
     * 创建直播间
     * @return type
     */
    public function actionRoomcreate()
    {        
        $model = new \app\models\Room;
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            if ($model->create($post)){
                Yii::$app->session->setFlash('success', '创建成功。');
                return $this->redirect('/live/room');
            }else{
                Yii::$app->session->setFlash('error', '创建失败。');
                $model->setAttributes($post);
            }
        }
        
        //所有主播
        $masters = \app\models\Master::getId2name();
        $masters = [''=>'0 无主播']+$masters;
        
        //图片上传组件
        $uploader = \backend\widgets\UploadImage::self()->logo('pic');
        
        return $this->render('roomedit',[
            'model'=> $model,
            'uploader'=>$uploader,
            'masters'=>$masters,
        ]);
    }
    
    /**
     * 编辑直播间
     * @return type
     */
    public function actionRoomedit()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        
        $model = new \app\models\Room;
        if (!$model = $model->findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该直播间');
            return $this->redirect($this->_refer);
        }
        
        if (Yii::$app->getRequest()->isPost){
            $post = Yii::$app->getRequest()->post();
            if ($model->edit($post)){
                Yii::$app->session->setFlash('success', '编辑成功。');
                $model->refresh();
            }else{
                Yii::$app->session->setFlash('error', '编辑失败。');
            }
        }
        
        //所有主播
        $masters = \app\models\Master::getId2name();
        $masters = [''=>'0 无主播']+$masters;
        
        //图片上传组件
        $uploader = \backend\widgets\UploadImage::self()->logo('pic',$model->pic);
        
        return $this->render('roomedit',[
            'model'=> $model,
            'uploader'=>$uploader,
            'masters'=>$masters,
        ]);
    }
    
    /**
     * 直播间操作
     * @return type
     */
    public function actionRoomoperate()
    {
        if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
            return $this->redirect($this->_refer);
        }
        if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
            return $this->redirect($this->_refer);
        }
        
        if (!$model = Room::findOne($id)){
            Yii::$app->session->setFlash('error', '找不到该直播间');
            return $this->redirect($this->_refer);
        }
        
        switch ($type){
            case 'del'://删除直播间
                $r = $model->del();
                break;
            case 'disable'://关闭直播间
                $r = $model->status(0);
                break;
            case 'enable'://启用直播间
                $r = $model->status(1);
                break;
            default:
                break;
        }
        if (!empty($r)){
            Yii::$app->session->setFlash('success', '操作成功');
        }else{
            Yii::$app->session->setFlash('error', '操作失败');
        }
        
        return $this->redirect($this->_refer);
    }
}