ForbiddenController.php
4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
namespace backend\controllers;
use Yii;
use backend\components\Pagination;
use app\models\Forbidden;
use app\models\Room;
use yii\helpers\ArrayHelper;
/**
* Live controller
*/
class ForbiddenController extends BaseController
{
/**
* 禁言列表
* @return type
*/
public function actionIndex()
{
$model = Forbidden::find();
$filter = Yii::$app->request->get();
$filter = ArrayHelper::filter($filter,['room_id','name','uid']);
$room_id = $filter['room_id'];
if($room_id){
//获取房间数据
$room = Room::findOne(['room_id'=>$room_id])->toArray();
if(!$room){
die('房间不存在');
}
if ($filter) {
foreach ($filter as $field => $value) {
if ($field == 'name') {
if (!empty($value)) {
$model->andWhere(['like', 'name', "{$value}"]);
}
} else {
if($value){
$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,
'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 = ['room_id'=>$room_id,'cmd' => 5];
$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);
}
}