LiveController.php
12 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
<?php
namespace backend\controllers;
use Yii;
use app\models\Master;
use app\models\Room;
use backend\components\Pagination;
use backend\widgets\Filter;
use backend\widgets\YHGImage\Common\Images as CommonImages;
use app\models\Video;
use yii\helpers\ArrayHelper;
/**
* Live controller
*/
class LiveController extends BaseController
{
/**
* 直播间
* @return type
*/
public function actionRoom()
{
$filter = Filter::self()->livingStatus()->keyword()->submit()->reset();
if ($condition = $filter->get_where(true)){
$model = \app\models\Room::condition($condition);
}else{
$model = \app\models\Room::find();
}
//var_dump($condition);
$count = clone $model;
$pagination = new Pagination(['totalCount' =>$count->count()]);
$list = $model
->with(['master','roomNums'])
->offset($pagination->offset)->limit($pagination->limit)
->orderBy(['create_time'=>SORT_DESC,'update_time'=>SORT_DESC])
->all();
//var_dump($list);
foreach ($list as $model){
$app = array_map(function($val){
return ArrayHelper::getValue(Yii::$app->params['config']['app'], $val);
},explode(',',$model->app));
$model->app = implode('<br />', array_filter($app));
}
return $this->render('room',[
'filter'=>$filter,
'pagination'=>$pagination,
'list'=>$list,
]);
}
/**
* 主播
* @return type
*/
public function actionMaster()
{
$filter = Filter::self()->masterName()->submit()->reset();
if ($condition = $filter->get_where(true)){
$model = Master::condition($condition);
}else{
$model = Master::find();
}
$count = clone $model;
$pagination = new Pagination([
'totalCount' => $count->count(),
]);
$masters = $model
->orderBy(['create_time'=>SORT_DESC,'update_time'=>SORT_DESC])
->offset($pagination->offset)->limit($pagination->limit)
->all();
return $this->render('master',[
'filter'=>$filter,
'pagination'=>$pagination,
'list'=>$masters,
]);
}
/**
* 视频
* @return type
*/
public function actionVideo()
{
$condition = Yii::$app->request->get();
$query = Video::condition($condition);
$pagination = new Pagination(['totalCount'=> $query->count()]);
$videoList = $query->offset($pagination->offset)
->limit($pagination->limit)->all();
$liveMasterIds = [];
$masterList = new \stdClass();
foreach($videoList as &$video) {
if(!empty($video->master_id)) {
$liveMasterIds[] = $video->master_id;
}
$app = array_map(function($val){
return ArrayHelper::getValue(Yii::$app->params['config']['app'], $val);
},explode(',',$video->app));
$video->app = implode('<br />', array_filter($app));
}
if(!empty($liveMasterIds)){
$masterList = Master::find()->where(['in', 'master_id', $liveMasterIds])->all();
}
return $this->render('video', ['videoList' => $videoList,'condition'=> $condition, 'masterList' => $masterList, 'pagination' => $pagination]);
}
/**
* 视频编辑
*
* @return
*/
public function actionVideoedit()
{
if($posts = Yii::$app->request->Post())
{
$model = Video::findOne($posts['id']);
if(!empty($model))
{
$model->pic = $posts['pic'];
$model->url = $posts['url'];
$model->title = $posts['title'];
if($model->save())
{
Yii::$app->session->setFlash('success', '编辑成功。');
}
}
else
{
Yii::$app->session->setFlash('error', '保存失败。');
}
return $this->refresh();
}
else
{
$id = Yii::$app->request->get('id', 0);
$video = Video::findOne(['id'=> $id]);
if(empty($video))
{
Yii::$app->session->setFlash('error', '找不到该视频');
return $this->redirect($this->_refer);
}
//图片上传组件
$uploader = \backend\widgets\UploadImage::self()->logo('pic', $video->pic);
return $this->render('videoedit', ['video' => $video, 'uploader' => $uploader]);
}
}
/**
* 视频状态设置
* @return
*/
public function actionVideosetstate()
{
if($posts = Yii::$app->request->post()) {
$status = Video::updateAll(['status' => intval($posts['status']),'update_time'=>time()], ['id' => $posts['videoId']]);
if($status)
{
$this->renderJson(200, '设置成功');
}
else
{
$this->renderJson(403,'设置失败');
}
}
else
{
$this->renderJson(403,'post方式');
}
}
/**
* 添加主播
* @return type
*/
public function actionAddmaster()
{
$model = new Master();
if ($posts = Yii::$app->request->post()) {
if(empty($posts['pic']) || empty($posts['name'])){
Yii::$app->session->setFlash('warning', '昵称或头像不能为空');
return $this->redirect('/live/addmaster');
}
$master = Master::findOne(['name'=>$posts['name']]);
if(!empty($master)){
Yii::$app->session->setFlash('warning', '主播名称已存在');
return $this->redirect('/live/addmaster');
}
$model->name = trim($posts['name']);
$model->meta = trim($posts['meta']);
$model->pic = $posts['pic'];
$model->create_time = $model->update_time = time();
if($model->save()){
Yii::$app->session->setFlash('success', '保存成功。');
}else{
Yii::$app->session->setFlash('error', '保存失败。');
}
return $this->redirect('/live/master');
}
//图片上传组件
$uploader = \backend\widgets\UploadImage::self()->logo('pic',$model->pic);
return $this->render('add_master',[
'row'=> $model,
'uploader'=>$uploader,
'action' => '/live/addmaster'
]);
}
/**
* 编辑主播
* @return type
*/
public function actionEditmaster()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = Master::findOne($id);
if (!$model){
Yii::$app->session->setFlash('error', '找不到该主播');
return $this->redirect($this->_refer);
}
if(Yii::$app->request->isPost){
$posts = Yii::$app->request->post();
$model->name = $posts['name'];
$model->meta = $posts['meta'];
$model->pic = $posts['pic'];
if($model->save()){
Yii::$app->session->setFlash('success', '编辑成功。');
}else{
Yii::$app->session->setFlash('error', '编辑失败。');
}
return $this->refresh();
}
//图片上传组件
$uploader = \backend\widgets\UploadImage::self()->logo('pic',$model->pic);
return $this->render('add_master',[
'row'=>$model,
'uploader'=>$uploader,
'action'=>'/live/editmaster/'.$id
]);
}
/**
* 删除主播
* @return type
*/
public function actionDelmaster(){
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = new Master;
if (!$model = $model->findOne($id)){
Yii::$app->session->setFlash('error', '找不到该主播');
return $this->redirect($this->_refer);
}
if($model->delete()){
Yii::$app->session->setFlash('success', '删除成功');
}else{
Yii::$app->session->setFlash('error', '删除失败。');
}
return $this->redirect($this->_refer);
}
/**
* 创建直播间
* @return type
*/
public function actionRoomcreate()
{
$model = new \app\models\Room;
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
if ($model->create($post)){
Yii::$app->session->setFlash('success', '创建成功。');
return $this->redirect('/live/room');
}else{
if (!empty($post['starting_time'])){
$post['starting_time'] = (int)strtotime($post['starting_time']);
}
Yii::$app->session->setFlash('error', '创建失败。');
$model->setAttributes($post);
}
}
//所有主播
$masters = \app\models\Master::getId2name();
$masters = [''=>'0 无主播']+$masters;
//图片上传组件
$picUploader = \backend\widgets\UploadImage::self()->single('pic',$model->pic);
$backgroundUploader = \backend\widgets\UploadImage::self()->single('background_pic',$model->background_pic);
return $this->render('roomedit',[
'model'=> $model,
'picUploader'=>$picUploader,
'backgroundUploader'=>$backgroundUploader,
'masters'=>$masters,
]);
}
/**
* 编辑直播间
* @return type
*/
public function actionRoomedit()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
$model = new \app\models\Room;
if (!$model = $model->findOne($id)){
Yii::$app->session->setFlash('error', '找不到该直播间');
return $this->redirect($this->_refer);
}
if (Yii::$app->getRequest()->isPost){
$post = Yii::$app->getRequest()->post();
if ($model->edit($post)){
Yii::$app->session->setFlash('success', '编辑成功。');
$model->refresh();
}else{
Yii::$app->session->setFlash('error', '编辑失败。');
}
}
$model->app = explode(',',$model->app);
//所有主播
$masters = \app\models\Master::getId2name();
$masters = [''=>'0 无主播']+$masters;
//图片上传组件
$picUploader = \backend\widgets\UploadImage::self()->single('pic',$model->pic);
$backgroundUploader = \backend\widgets\UploadImage::self()->single('background_pic',$model->background_pic);
return $this->render('roomedit',[
'model'=> $model,
'picUploader'=>$picUploader,
'backgroundUploader'=>$backgroundUploader,
'masters'=>$masters,
]);
}
/**
* 直播间操作
* @return type
*/
public function actionRoomoperate()
{
if (!$id = Yii::$app->getRequest()->getQueryParam('id')){
return $this->redirect($this->_refer);
}
if (!$type = Yii::$app->getRequest()->getQueryParam('type')){
return $this->redirect($this->_refer);
}
if (!$model = Room::findOne($id)){
Yii::$app->session->setFlash('error', '找不到该直播间');
return $this->redirect($this->_refer);
}
switch ($type){
case 'del'://删除直播间
$r = $model->del();
break;
case 'disable'://关闭直播间
$r = $model->status(0);
break;
case 'enable'://启用直播间
$r = $model->status(1);
break;
case 'start'://开始直播
$r = (new \common\models\Video)->setVideoStart($model->room_id);
default:
break;
}
if (!empty($r)){
Yii::$app->session->setFlash('success', '操作成功');
}else{
Yii::$app->session->setFlash('error', '操作失败');
}
return $this->redirect($this->_refer);
}
}