Room.php
12.8 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
<?php
namespace app\models;
use Yii;
use common\lib\QcloudApi\Client as QcloudApiClient;
use common\models\RoomNums;
use common\models\Share;
use common\config\Params;
/**
* This is the model class for table "{{%room}}".
*
* @property string $id
* @property string $room_id
* @property string $title
* @property string $master_id
* @property string $pic
* @property integer $starting_time
* @property string $starting_pic
* @property string $secret
* @property string $best
* @property string $vest
* @property integer $status
* @property integer $create_time
* @property integer $update_time
*/
class Room extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%room}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['room_id', 'master_id', 'starting_time', 'best', 'vest', 'is_addup', 'living', 'status', 'unstart_sort', 'living_sort', 'create_time', 'update_time'], 'integer'],
[['title', 'watermark'], 'string', 'max' => 100],
[['app', 'secret'], 'string', 'max' => 50],
[['pic', 'background_pic'], 'string', 'max' => 255],
[['room_id'], 'unique'],
[['secret'], 'unique'],
[['pic', 'background_pic'], 'required'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'room_id' => '房间ID',
'title' => '直播标题',
'watermark' => '水印文案',
'master_id' => '主播ID',
'app' => '业务线',
'secret' => '房间密钥',
'pic' => '头图',
'background_pic' => '背景图',
'starting_time' => '预告开始时间',
'best' => '是否精选',
'vest' => '马甲状态',
'living' => '直播状态',
'status' => '房间状态',
'unstart_sort' => '未开始排序',
'living_sort' => '直播中排序',
'create_time' => '创建时间',
'update_time' => '更新时间',
];
}
public function behaviors() {
return [
[
'class' => \yii\behaviors\TimestampBehavior::className(),
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
]
];
}
/**
* 关联表-主播
* @return type
*/
public function getMaster()
{
return $this->hasOne(Master::className(), ['master_id'=>'master_id']);
}
/**
* 关联表-腾讯云直播频道
* @return type
*/
public function getRoomQchannel()
{
return $this->hasOne(RoomQchannel::className(), ['room_id'=>'room_id']);
}
/**
* 关联表-正在录制的视频
* @return type
*/
public function getVideoRecording()
{
return $this->hasOne(Video::className(), ['room_id'=>'room_id'])->where(['and',['>','live_start_time',0],['live_end_time'=>0]])->orderBy(['create_time'=>SORT_DESC]);
}
/**
* 关联表-计数
* @return type
*/
public function getRoomNums()
{
return $this->hasOne(RoomNums::className(), ['room_id'=>'room_id']);
}
/**
* 生成房间ID
*/
public function bornRoomId()
{
$max_id = (int)$this->find()->max('room_id');
return $max_id ? $max_id+1 : 1001;
}
/**
* 注入查询条件
* @param array $condition
*/
public static function condition($condition){
$model = parent::find();
foreach ($condition as $field=>$value){
if ($field == 'keyword'){
if (!empty($value)){
$model->alias('r')->joinWith('master m')->andWhere(['or',['room_id'=>$value],['r.master_id'=>$value],['like','title',$value],['like','m.name',$value]]);
}
}else{
$model->andWhere([$field=>$value]);
}
}
return $model;
}
/**
* 创建直播间
* @param array $data 直播间信息
*/
public function create(array $data)
{
$transaction = $this->getDb()->beginTransaction();
try{
if (empty($data['app'])){
throw new \Exception('未选择业务线');
}
if (!empty($data['starting_time'])){
$data['starting_time'] = (int)strtotime($data['starting_time']);
if ($data['starting_time'] <= time()){
throw new \Exception('预告开始时间不能早于当前时间');
}
}
$data['room_id'] = $this->bornRoomId();
sort($data['app']);
$data['app'] = implode(',', $data['app']);
if ($data['best'] > 0){
$count = $this->find()->where(['!=','best','0'])->count();
if ($count >= 2){
throw new \Exception('精选房间上限是2个');
}
}
$this->setAttributes($data);
if (!$this->save()){
throw new \Exception(current($this->getFirstErrors()));
}
$title = $this->title;
$ret = QcloudApiClient::self()->CreateLVBChannel($title);
if (!$ret){
throw new \Exception(QcloudApiClient::self()->error());
}
$RoomQchannel = new RoomQchannel;
$RoomQchannel->setAttributes([
'room_id'=>$this->room_id,
'channel_id'=>$ret['channel_id'],
'upstream_address'=>$ret['channelInfo']['upstream_address'],
'downstream_address'=>json_encode($ret['channelInfo']['downstream_address'],JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE)
]);
if (!$RoomQchannel->save()){
throw new \Exception(current($RoomQchannel->getFirstErrors()));
}
//插入分享内容
$RoomShare = new Share;
$RoomShare->setAttributes([
'obj_id'=>$this->room_id,
'type'=>Params::LIVE_SHARE_TYPE,
'pic'=>$data['share_pic'],
'title'=>$data['share_title'],
'content'=>$data['share_content'],
]);
if (!$RoomShare->save()){
throw new \Exception(current($RoomShare->getFirstErrors()));
}
$transaction->commit();
return $this->room_id;
}catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('warning', $e->getMessage());
return false;
}
}
/**
* 编辑直播间
* @param array $data 直播间信息
*/
public function edit(array $data)
{
$transaction = $this->getDb()->beginTransaction();
try{
if (empty($data['app'])){
throw new \Exception('未选择业务线');
}
if (!empty($data['starting_time'])){
$data['starting_time'] = (int)strtotime($data['starting_time']);
if ($data['starting_time'] <= time()){
throw new \Exception('预告开始时间不能早于当前时间');
}
}
@sort($data['app']);
$data['app'] = implode(',', $data['app']);
if ($data['best'] > 0){
$count = $this->find()->where(['and',['!=','best','0'],['!=','id',$this->id]])->count();
if ($count >= 2){
throw new \Exception('精选房间上限是2个');
}
}
if (isset($data['is_addup'])){
$data['is_addup'] = Yii::$app->params['config']['is_addup'];
}else{
$data['is_addup'] = 0;
}
$this->setAttributes($data);
if (!$this->save()){
throw new \Exception(current($this->getFirstErrors()));
}
//编辑分享内容
if (!$RoomShare = Share::findOne(['type' => Params::LIVE_SHARE_TYPE, 'obj_id' => $this->room_id])) {
//编辑以前的房间时,没有相应的分享记录,那么这时候要重新创建一个对象来做插入
$RoomShare = new Share();
}
$RoomShare->setAttributes([
'obj_id' => $this->room_id,
'type' => Params::LIVE_SHARE_TYPE,
'pic' => $data['share_pic'],
'title' => $data['share_title'],
'content' => $data['share_content'],
]);
if (!$RoomShare->save()) {
throw new \Exception(current($RoomShare->getFirstErrors()));
}
$transaction->commit();
return true;
}catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('warning', $e->getMessage());
return false;
}
}
/**
* 删除直播间
* @param type $id
*/
public function del()
{
$transaction = $this->getDb()->beginTransaction();
try{
$model = $this;
if (!$model->delete()){
throw new \Exception(current($model->getFirstErrors()));
}
if ($model->roomQchannel){
if (!$model->roomQchannel->delete()){
throw new \Exception(current($model->roomQchannel->getFirstErrors()));
}
if (!QcloudApiClient::self()->DeleteLVBChannel([$model->roomQchannel->channel_id])){
throw new \Exception(QcloudApiClient::self()->error());
}
}
//删除分享
$delShare = (new Share())->delLivingShare($model->room_id);
if (!$delShare) {
throw new \Exception("删除分享失败");
}
$transaction->commit();
return true;
}catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('warning', $e->getMessage());
return false;
}
}
/**
* 修改频道状态
* @param type $id
*/
public function status($status)
{
$transaction = $this->getDb()->beginTransaction();
try{
$model = $this;
$model->status = $status;
if ($status == 1){//启用时,直播状态设为未开始
$model->living = 0;
}elseif ($status == 0){//禁用时,直播状态设为已结束
//fixbug 只有正在直播中的房间才会把视频片段设置观看人数 add by yaoxiaofeng|2016.9.29
if ($model->living == 1 && $model->videoRecording){
//停止正在录制的视频
$roomNums = \common\models\RoomNums::getRoomNums($model->room_id);
$model->videoRecording->audience_num = $roomNums['audience_nums'];
$model->videoRecording->like_num = $roomNums['like_nums'];
$model->videoRecording->live_end_time = time();
$model->videoRecording->save();
}
$model->living = 2;
}
if (!$model->save(false)){
throw new \Exception(current($model->getFirstErrors()));
}
if ($model->roomQchannel){
if ($status == 1){//启用
$ret = QcloudApiClient::self()->StartLVBChannel([$model->roomQchannel->channel_id]);
}elseif ($status == 0){//禁用
/*if ($model->videoRecording){
//停止正在录制的视频
if ($model->videoRecording->task_id){
if (QcloudApiClient::self()->StopRecord($model->roomQchannel->channel_id,$model->videoRecording->task_id)){
$model->videoRecording->live_end_time = time();
$model->videoRecording->save();
}
}
}*/
$ret = QcloudApiClient::self()->StopLVBChannel([$model->roomQchannel->channel_id]);
}
if (!$ret){
throw new \Exception(QcloudApiClient::self()->error());
}
}
$transaction->commit();
return true;
}catch (\Exception $e) {
$transaction->rollBack();
Yii::$app->session->setFlash('warning', $e->getMessage());
return false;
}
}
}