Authored by xiaofeng.yao@yoho.cn

禁言功能开发

<?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;
}
}
}
... ...
... ... @@ -96,10 +96,11 @@ class LiveController extends BaseController
$model->pic = $posts['pic'];
$model->create_time = $model->update_time = time();
if($model->save()){
return $this->redirect(['master']);
Yii::$app->session->setFlash('success', '保存成功。');
}else{
die("保存失败");
Yii::$app->session->setFlash('error', '保存失败。');
}
return $this->redirect(['master']);
} else {
return $this->render('add_master',['action'=>'/live/addmaster']);
}
... ... @@ -118,10 +119,12 @@ class LiveController extends BaseController
$model->meta = $posts['meta'];
$model->pic = $posts['pic'];
if($model->save()){
return $this->redirect(['editmaster','id' => $model->master_id]);
//return $this->redirect(['editmaster','id' => $model->master_id]);
Yii::$app->session->setFlash('success', '编辑成功。');
}else{
die("保存失败");
Yii::$app->session->setFlash('error', '编辑失败。');
}
return $this->refresh();
}else{
$row = Master::find()->where(['master_id'=>$id])->one();
return $this->render('add_master',[
... ... @@ -140,7 +143,7 @@ class LiveController extends BaseController
if($model->delete()){
return $this->redirect(['master']);
}else{
echo "删除失败";exit;
Yii::$app->session->setFlash('error', '删除失败。');
}
}
}
... ...
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "{{%forbidden}}".
*
* @property integer $id
* @property integer $room_id
* @property integer $uid
* @property string $name
* @property integer $status
*/
class Forbidden extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%forbidden}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id', 'uid', 'status'], 'integer'],
[['name'], 'string', 'max' => 50],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'room_id' => 'Room ID',
'uid' => 'Uid',
'name' => 'Name',
'status' => 'Status',
];
}
}
... ...
<div>
<p class="lead">
<span class="mr10">房间号:<?php echo $filter['room_id']?></span>
<span>直播状态:直播中</span>
</p>
</div>
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-body">
<form class="form-inline" method="post" action="/forbidden/create">
<div>
<div class="form-group">
<label class="control-label">用户UID:</label>
<input class="form-control" name="uid" value="">
</div>
<div class="form-group">
<label class="control-label">用户昵称:</label>
<input class="form-control" name="name" value="">
</div>
<input type="hidden" name="room_id" value="<?php echo $filter['room_id']?>">
<button type="submit" class="btn btn-primary mr5">+添加</button>
</div>
</form>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<form class="form-inline" method="get" >
<div>
<div class="form-group">
<label class="control-label">用户UID:</label>
<input class="form-control" name="uid" value="<?php echo $condition['uid']?>">
</div>
<div class="form-group">
<label class="control-label">用户昵称:</label>
<input class="form-control" name="name" value="<?php echo $condition['name']?>">
</div>
<input type="hidden" name="room_id" value="<?php echo $filter['room_id']?>">
<button type="submit" class="btn btn-primary mr5">查询</button>
</div>
</form>
</div>
</div>
<!-- <div class="form-group">-->
<!-- <label>-->
<!-- <a href="/live/addmaster" class="btn btn-primary">+ 添加主播</a>-->
<!-- </label>-->
<!-- </div>-->
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-dark mb30">
<thead>
<tr>
<th>用户UID</th>
<th>用户昵称</th>
<th>最后一次发言时间</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<?php foreach($list as $v):?>
<tr>
<td><?php echo $v->uid;?></td>
<td><?php echo $v->name;?></td>
<td></td>
<td>
<?php if($v->status==Yii::$app->params['config']['is_forbid_stat']){?>
<a class="btn btn-info set-status" unqid="<?php echo $v->id;?>" stat="<?php echo (1-$v->status);?>">取消禁言</a>
<?php }else{?>
<a class="btn btn-danger set-status" unqid="<?php echo $v->id;?>" stat="<?php echo (1-$v->status);?>">禁言</a>
<?php }?>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
</div>
<!-- table-responsive -->
</div>
<!-- col-md-12 -->
</div>
</div>
<div class="col-md-6">
<iframe width="100%">
</iframe>
</div>
<script type="text/javascript">
$(function(){
$('.set-status').click(function(){
var stat = $(this).attr('stat');
var obj = $(this);
$.get("/forbidden/set-status",{id:$(this).attr('unqid'),status:stat},function(data){
// alert(stat);return;
if(data.code==200){
if(stat==0){
obj.html('禁言');
obj.attr('stat',1).attr('class','btn btn-danger set-status');
// obj.className = "btn btn-info set-status";
}
if(stat==1){
obj.html('取消禁言');
obj.attr('stat',0).attr('class','btn btn-info set-status');
}
}else{
alert('设置失败');
}
},'json');
});
});
</script>
\ No newline at end of file
... ...
... ... @@ -11,7 +11,11 @@
</form>
</div>
</div>
<div><a class="btn btn-primary" href="/live/addmaster">添加主播</a></div>
<div class="form-group">
<label>
<a href="/live/addmaster" class="btn btn-primary">+ 添加主播</a>
</label>
</div>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
... ...
... ... @@ -72,7 +72,7 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
<a href="/live/roomedit/<?=$row['id']?>" class="btn btn-default btn-white">编辑</a>
<a href="javascript:void(0)" class="btn btn-default btn-white">禁言</a>
<a href="/forbidden/index?room_id=<?=$row['id']?>" class="btn btn-default btn-white">禁言</a>
<span type="status">
<button type="disable" onclick="javascript:void(0)" class="btn btn-danger btn-metro">删除</button>
... ...
... ... @@ -20,6 +20,9 @@ return [
0=>'未开始',
1=>'直播中',
],
'un_forbid_stat'=>0,
'is_forbid_stat'=>1,
],
];
... ...