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

use Yii;
use backend\components\Pagination;
use app\models\Forbidden;

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

    /**
     * 禁言列表
     * @return type
     */
    public function actionIndex()
    {
        $model = Forbidden::find();
        //$room_id = Yii::$app->request->get('room_id',0);
        if ($filter = Yii::$app->request->get()) {
//            var_dump($filter);
            foreach ($filter as $field => $value) {
                if ($field == 'keyword') {
                    if (!empty($value)) {
                        $model->andWhere(['like', 'title', "%{$value}%"])
                            ->orWhere(['room_id' => $value])
                            ->orWhere(['master_id' => $value]);
                    }
                } else {
                    $model->andWhere([$field => $value]);
                }
            }
        }

        $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
        ]);

    }

    /**
     * 新增禁言用户
     * @return \yii\web\Response
     */
    public function actionCreate()
    {
        $model = new Forbidden();
        if ($posts = Yii::$app->request->post()) {
            $model->room_id = $posts['room_id'];
            $model->uid = $posts['uid'];
            $model->name = $posts['name'];
            $model->status = 1;
            $model->create_time = time();
            if (!$model->save()) {
//                var_dump($model->errors);
                Yii::$app->session->setFlash('error', '添加失败。');
            } else {
                Yii::$app->session->setFlash('success', '添加成功。');
            }
            return $this->redirect(['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;
        }
    }

}