ForbiddenController.php 4.97 KB
<?php
namespace backend\controllers;

use Yii;
use backend\components\Pagination;
use app\models\Forbidden;
use app\models\Room;
/**
 * Live controller
 */
class ForbiddenController extends BaseController
{

    /**
     * 禁言列表
     * @return type
     */
    public function actionIndex()
    {
        $model = Forbidden::find();
        $filter = Yii::$app->request->get();
        $room_id = $filter['room_id'];
        if($room_id){
            //获取房间数据
            $room = Room::findOne(['room_id'=>$room_id])->toArray();
            if(!$room){
                die('房间不存在');
            }
            if ($filter) {
//            var_dump($filter);
                foreach ($filter as $field => $value) {
                    if ($field == 'name') {
                        if (!empty($value)) {
                            $model->andWhere(['like', 'name', "{$value}"]);
                        }
                    } else {
                        if($value){
                            $model->andWhere([$field => $value]);
                        }
                    }
                }
            }
            //获取房间数据
            $room = Room::findOne(['room_id'=>$room_id]);

            $count = clone $model;
            $pagination = new Pagination(['totalCount' => $count->count()]);

            $list = $model->offset($pagination->offset)->limit($pagination->limit)->orderBy(['create_time' => SORT_DESC])->all();
            return $this->render('index', [
                'filter'     => $filter ?: [],
                'pagination' => $pagination,
                'list'       => $list,
                'room'       => $room
            ]);
        }else{
            die('缺少room_id');
        }

    }

    /**
     * 新增禁言用户
     * @return \yii\web\Response
     */
    public function actionCreate()
    {
        $model = new Forbidden();
        if ($posts = Yii::$app->request->post()) {
            do {
                $model->room_id = $posts['room_id'];
                $model->uid = $posts['uid'];
                $model->name = $posts['name'];

                if (!$model->uid || !$model->name) {
                    $code = 400;
                    $code_str = 'error';
                    $msg = '用户id、昵称必填!';
                    break;
                }
                $model->status = 1;
                $model->create_time = time();
                if (!$model->save()) {
//                var_dump($model->errors);
                    $code = 400;
                    $code_str = 'error';
                    $msg = '添加失败。'.current($model->getFirstErrors());
                } else {
                    $code = 200;
                    $code_str = 'success';
                    $msg = '添加成功。';
                }
            }
            while(false);

            if(Yii::$app->request->isAjax){
                $this->renderJson($code,$msg);
            }else{
                Yii::$app->session->setFlash($code_str, $msg);
                return $this->redirect('forbidden/index?room_id='.$posts['room_id']);
            }
        }
    }

    public function actionSetStatus()
    {
        $id = Yii::$app->request->get('id', 0);
        if ($id) {
            $status = Yii::$app->request->get('status', 0);
            $model = Forbidden::findOne($id);
            $model->status = $status;
            if (!$model->save()) {
                $ret = [
                    'code' => 400,
                    'msg'  => '保存失败'
                ];
            } else {
                $ret = [
                    'code' => 200,
                    'msg'  => '保存成功'
                ];
            }
            echo json_encode($ret);
            exit;
        }
    }

    public function actionGetcomments()
    {
//        $this->layout = false;
        $gets = Yii::$app->request->get();
        $room_id = $gets['room_id'];
        if($room_id) {
            //$sqlwhere  = ['cmd' => 5];
            $sqlwhere = [];
            $count = (new \yii\db\Query())
                ->from($this->getTableName($room_id))
                ->where($sqlwhere)
                ->count('*');
            $pagination = new Pagination(['defaultPageSize' => 50,'totalCount' => $count]);

            $list = (new \yii\db\Query())
                ->select('*')
                ->from($this->getTableName($room_id))
                ->where($sqlwhere)
                ->orderBy(['id'=>SORT_DESC])
                ->offset($pagination->offset)
                ->limit($pagination->limit)
                ->all();
//            var_dump($pagination);exit;
            return $this->render('comment',[
                'list'=>$list,
                'pagination' => $pagination,
            ]);
        }
    }

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