Authored by xiaofeng.yao@yoho.cn

禁言功能开发

  1 +<?php
  2 +namespace backend\controllers;
  3 +
  4 +use Yii;
  5 +use backend\components\Pagination;
  6 +use app\models\Forbidden;
  7 +
  8 +/**
  9 + * Live controller
  10 + */
  11 +class ForbiddenController extends BaseController
  12 +{
  13 +
  14 + /**
  15 + * 禁言列表
  16 + * @return type
  17 + */
  18 + public function actionIndex()
  19 + {
  20 + $model = Forbidden::find();
  21 + //$room_id = Yii::$app->request->get('room_id',0);
  22 + if ($filter = Yii::$app->request->get()) {
  23 +// var_dump($filter);
  24 + foreach ($filter as $field => $value) {
  25 + if ($field == 'keyword') {
  26 + if (!empty($value)) {
  27 + $model->andWhere(['like', 'title', "%{$value}%"])
  28 + ->orWhere(['room_id' => $value])
  29 + ->orWhere(['master_id' => $value]);
  30 + }
  31 + } else {
  32 + $model->andWhere([$field => $value]);
  33 + }
  34 + }
  35 + }
  36 +
  37 + $count = clone $model;
  38 + $pagination = new Pagination(['totalCount' => $count->count()]);
  39 +
  40 + $list = $model->offset($pagination->offset)->limit($pagination->limit)->orderBy(['create_time' => SORT_DESC])->all();
  41 + return $this->render('index', [
  42 + 'filter' => $filter ?: [],
  43 + 'pagination' => $pagination,
  44 + 'list' => $list
  45 + ]);
  46 +
  47 + }
  48 +
  49 + /**
  50 + * 新增禁言用户
  51 + * @return \yii\web\Response
  52 + */
  53 + public function actionCreate()
  54 + {
  55 + $model = new Forbidden();
  56 + if ($posts = Yii::$app->request->post()) {
  57 + $model->room_id = $posts['room_id'];
  58 + $model->uid = $posts['uid'];
  59 + $model->name = $posts['name'];
  60 + $model->status = 1;
  61 + $model->create_time = time();
  62 + if (!$model->save()) {
  63 +// var_dump($model->errors);
  64 + Yii::$app->session->setFlash('error', '添加失败。');
  65 + } else {
  66 + Yii::$app->session->setFlash('success', '添加成功。');
  67 + }
  68 + return $this->redirect(['index', 'room_id' => $posts['room_id']]);
  69 + }
  70 + }
  71 +
  72 + public function actionSetStatus()
  73 + {
  74 + $id = Yii::$app->request->get('id', 0);
  75 + if ($id) {
  76 + $status = Yii::$app->request->get('status', 0);
  77 + $model = Forbidden::findOne($id);
  78 + $model->status = $status;
  79 + if (!$model->save()) {
  80 + $ret = [
  81 + 'code' => 400,
  82 + 'msg' => '保存失败'
  83 + ];
  84 + } else {
  85 + $ret = [
  86 + 'code' => 200,
  87 + 'msg' => '保存成功'
  88 + ];
  89 + }
  90 + echo json_encode($ret);
  91 + exit;
  92 + }
  93 + }
  94 +
  95 +}
@@ -96,10 +96,11 @@ class LiveController extends BaseController @@ -96,10 +96,11 @@ class LiveController extends BaseController
96 $model->pic = $posts['pic']; 96 $model->pic = $posts['pic'];
97 $model->create_time = $model->update_time = time(); 97 $model->create_time = $model->update_time = time();
98 if($model->save()){ 98 if($model->save()){
99 - return $this->redirect(['master']); 99 + Yii::$app->session->setFlash('success', '保存成功。');
100 }else{ 100 }else{
101 - die("保存失败"); 101 + Yii::$app->session->setFlash('error', '保存失败。');
102 } 102 }
  103 + return $this->redirect(['master']);
103 } else { 104 } else {
104 return $this->render('add_master',['action'=>'/live/addmaster']); 105 return $this->render('add_master',['action'=>'/live/addmaster']);
105 } 106 }
@@ -118,10 +119,12 @@ class LiveController extends BaseController @@ -118,10 +119,12 @@ class LiveController extends BaseController
118 $model->meta = $posts['meta']; 119 $model->meta = $posts['meta'];
119 $model->pic = $posts['pic']; 120 $model->pic = $posts['pic'];
120 if($model->save()){ 121 if($model->save()){
121 - return $this->redirect(['editmaster','id' => $model->master_id]); 122 + //return $this->redirect(['editmaster','id' => $model->master_id]);
  123 + Yii::$app->session->setFlash('success', '编辑成功。');
122 }else{ 124 }else{
123 - die("保存失败"); 125 + Yii::$app->session->setFlash('error', '编辑失败。');
124 } 126 }
  127 + return $this->refresh();
125 }else{ 128 }else{
126 $row = Master::find()->where(['master_id'=>$id])->one(); 129 $row = Master::find()->where(['master_id'=>$id])->one();
127 return $this->render('add_master',[ 130 return $this->render('add_master',[
@@ -140,7 +143,7 @@ class LiveController extends BaseController @@ -140,7 +143,7 @@ class LiveController extends BaseController
140 if($model->delete()){ 143 if($model->delete()){
141 return $this->redirect(['master']); 144 return $this->redirect(['master']);
142 }else{ 145 }else{
143 - echo "删除失败";exit; 146 + Yii::$app->session->setFlash('error', '删除失败。');
144 } 147 }
145 } 148 }
146 } 149 }
  1 +<?php
  2 +
  3 +namespace app\models;
  4 +
  5 +use Yii;
  6 +
  7 +/**
  8 + * This is the model class for table "{{%forbidden}}".
  9 + *
  10 + * @property integer $id
  11 + * @property integer $room_id
  12 + * @property integer $uid
  13 + * @property string $name
  14 + * @property integer $status
  15 + */
  16 +class Forbidden extends \yii\db\ActiveRecord
  17 +{
  18 + /**
  19 + * @inheritdoc
  20 + */
  21 + public static function tableName()
  22 + {
  23 + return '{{%forbidden}}';
  24 + }
  25 +
  26 + /**
  27 + * @inheritdoc
  28 + */
  29 + public function rules()
  30 + {
  31 + return [
  32 + [['room_id', 'uid', 'status'], 'integer'],
  33 + [['name'], 'string', 'max' => 50],
  34 + ];
  35 + }
  36 +
  37 + /**
  38 + * @inheritdoc
  39 + */
  40 + public function attributeLabels()
  41 + {
  42 + return [
  43 + 'id' => 'ID',
  44 + 'room_id' => 'Room ID',
  45 + 'uid' => 'Uid',
  46 + 'name' => 'Name',
  47 + 'status' => 'Status',
  48 + ];
  49 + }
  50 +}
  1 +<div>
  2 + <p class="lead">
  3 + <span class="mr10">房间号:<?php echo $filter['room_id']?></span>
  4 + <span>直播状态:直播中</span>
  5 + </p>
  6 +</div>
  7 +<div class="col-md-6">
  8 + <div class="panel panel-default">
  9 + <div class="panel-body">
  10 + <form class="form-inline" method="post" action="/forbidden/create">
  11 + <div>
  12 + <div class="form-group">
  13 + <label class="control-label">用户UID:</label>
  14 + <input class="form-control" name="uid" value="">
  15 + </div>
  16 + <div class="form-group">
  17 + <label class="control-label">用户昵称:</label>
  18 + <input class="form-control" name="name" value="">
  19 + </div>
  20 + <input type="hidden" name="room_id" value="<?php echo $filter['room_id']?>">
  21 + <button type="submit" class="btn btn-primary mr5">+添加</button>
  22 + </div>
  23 + </form>
  24 + </div>
  25 + </div>
  26 + <div class="panel panel-default">
  27 + <div class="panel-body">
  28 + <form class="form-inline" method="get" >
  29 + <div>
  30 + <div class="form-group">
  31 + <label class="control-label">用户UID:</label>
  32 + <input class="form-control" name="uid" value="<?php echo $condition['uid']?>">
  33 + </div>
  34 + <div class="form-group">
  35 + <label class="control-label">用户昵称:</label>
  36 + <input class="form-control" name="name" value="<?php echo $condition['name']?>">
  37 + </div>
  38 + <input type="hidden" name="room_id" value="<?php echo $filter['room_id']?>">
  39 + <button type="submit" class="btn btn-primary mr5">查询</button>
  40 + </div>
  41 + </form>
  42 + </div>
  43 + </div>
  44 +<!-- <div class="form-group">-->
  45 +<!-- <label>-->
  46 +<!-- <a href="/live/addmaster" class="btn btn-primary">+ 添加主播</a>-->
  47 +<!-- </label>-->
  48 +<!-- </div>-->
  49 + <div class="row">
  50 + <div class="col-md-12">
  51 + <div class="table-responsive">
  52 + <table class="table table-dark mb30">
  53 + <thead>
  54 + <tr>
  55 + <th>用户UID</th>
  56 + <th>用户昵称</th>
  57 + <th>最后一次发言时间</th>
  58 + <th>操作</th>
  59 + </tr>
  60 + </thead>
  61 + <tbody>
  62 + <?php foreach($list as $v):?>
  63 + <tr>
  64 + <td><?php echo $v->uid;?></td>
  65 + <td><?php echo $v->name;?></td>
  66 + <td></td>
  67 + <td>
  68 + <?php if($v->status==Yii::$app->params['config']['is_forbid_stat']){?>
  69 + <a class="btn btn-info set-status" unqid="<?php echo $v->id;?>" stat="<?php echo (1-$v->status);?>">取消禁言</a>
  70 + <?php }else{?>
  71 + <a class="btn btn-danger set-status" unqid="<?php echo $v->id;?>" stat="<?php echo (1-$v->status);?>">禁言</a>
  72 + <?php }?>
  73 + </td>
  74 + </tr>
  75 + <?php endforeach;?>
  76 + </tbody>
  77 + </table>
  78 + </div>
  79 + <!-- table-responsive -->
  80 + </div>
  81 + <!-- col-md-12 -->
  82 + </div>
  83 +</div>
  84 +<div class="col-md-6">
  85 + <iframe width="100%">
  86 +
  87 +
  88 + </iframe>
  89 +</div>
  90 +<script type="text/javascript">
  91 + $(function(){
  92 + $('.set-status').click(function(){
  93 + var stat = $(this).attr('stat');
  94 + var obj = $(this);
  95 + $.get("/forbidden/set-status",{id:$(this).attr('unqid'),status:stat},function(data){
  96 +// alert(stat);return;
  97 + if(data.code==200){
  98 + if(stat==0){
  99 + obj.html('禁言');
  100 + obj.attr('stat',1).attr('class','btn btn-danger set-status');
  101 +// obj.className = "btn btn-info set-status";
  102 + }
  103 + if(stat==1){
  104 + obj.html('取消禁言');
  105 + obj.attr('stat',0).attr('class','btn btn-info set-status');
  106 + }
  107 + }else{
  108 + alert('设置失败');
  109 + }
  110 + },'json');
  111 + });
  112 + });
  113 +</script>
@@ -11,7 +11,11 @@ @@ -11,7 +11,11 @@
11 </form> 11 </form>
12 </div> 12 </div>
13 </div> 13 </div>
14 -<div><a class="btn btn-primary" href="/live/addmaster">添加主播</a></div> 14 +<div class="form-group">
  15 + <label>
  16 + <a href="/live/addmaster" class="btn btn-primary">+ 添加主播</a>
  17 + </label>
  18 +</div>
15 <div class="row"> 19 <div class="row">
16 <div class="col-md-12"> 20 <div class="col-md-12">
17 <div class="table-responsive"> 21 <div class="table-responsive">
@@ -72,7 +72,7 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title']; @@ -72,7 +72,7 @@ $this->title = $this->params['main_title'].'-'.$this->params['sub_title'];
72 72
73 <a href="/live/roomedit/<?=$row['id']?>" class="btn btn-default btn-white">编辑</a> 73 <a href="/live/roomedit/<?=$row['id']?>" class="btn btn-default btn-white">编辑</a>
74 74
75 - <a href="javascript:void(0)" class="btn btn-default btn-white">禁言</a> 75 + <a href="/forbidden/index?room_id=<?=$row['id']?>" class="btn btn-default btn-white">禁言</a>
76 76
77 <span type="status"> 77 <span type="status">
78 <button type="disable" onclick="javascript:void(0)" class="btn btn-danger btn-metro">删除</button> 78 <button type="disable" onclick="javascript:void(0)" class="btn btn-danger btn-metro">删除</button>
@@ -20,6 +20,9 @@ return [ @@ -20,6 +20,9 @@ return [
20 0=>'未开始', 20 0=>'未开始',
21 1=>'直播中', 21 1=>'直播中',
22 ], 22 ],
  23 + 'un_forbid_stat'=>0,
  24 + 'is_forbid_stat'=>1,
  25 +
23 ], 26 ],
24 27
25 ]; 28 ];