ListController.class.php
6.81 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
<?php
/**
* 直播活动列表接口
* Created by PhpStorm.
* User: yingcai
* Date: 2018/1/9
* Time: 下午3:16
*/
namespace Apicp\Controller\Room;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Common\Order;
use Common\Service\MainService;
use Common\Service\StudioFileService;
use Common\Service\StudioPlaybackService;
class ListController extends AbstractController
{
/**
* List
* @author houyingcai
* @desc 直播活动列表接口
* @param Int page:false:1 页码
* @param Int limit:false:20 每页记录数
* @param String name:false 名称关键词
* @param Int start_time_begin:false 起始开始时间
* @param Int start_time_end:false 终止开始时间
* @param Int live_status:false 活动状态
* @return array|bool 直播列表
array(
'page' => 1, // 页码
'limit' => 5, // 每页记录数
'total' => 20, // 列表记录总数
'list' => array(
array(
'lm_id' => 1, // 直播ID
'name' => '产品培训讲解', // 直播名称
'pic' => 'b3ddbc502e307665f346cbd6e52cc10d', // 封面ID
'pic_url' => 'http://qy.vchangyi.org', // 封面图片URL
'live_status' => 1, // 直播状态(1=草稿;2=未开始;3=进行中;4=已结束)
'watched_total' => 150, // 已观看直播人数
'range_total' => 200, // 可观看直播总人数
'updated' => 1515407868910, // 最后更新时间
'start_time' => 1515207667910, // 开始时间
'video_url' => 'http://qy.vchangyi.org', // 视频回放地址
'has_playback' => true, // 是否有回放 true 有 false 无
),
),
)
*/
public function Index_post()
{
// 验证规则
$rules = [
'page' => 'integer',
'limit' => 'integer',
'name' => 'max:30',
'start_time_begin' => 'integer',
'start_time_end' => 'integer',
'live_status' => 'integer|in:1,2,3,4',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 分页默认值
$page = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$limit = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
list($start, $limit) = page_limit($page, $limit);
// 组合搜索条件
$conds = $this->assemble_where($postData);
// 获取记录总数
$mainService = new MainService();
$total = $mainService->count_by_conds($conds);
$list = [];
// 记录总数不为空:有数据
if ($total) {
// 分页参数
$pageOption = [$start, $limit];
// 排序
$orderOption = [
'updated'=> 'DESC',
'created' => 'DESC',
];
// 查询字段
$fields = 'lm_id,name,pic,live_status,start_time,watched_total,range_total,updated,created';
// 获取列表数据
$list = $mainService->list_by_conds($conds, $pageOption, $orderOption, $fields);
// 获取对应的回放地址
$studioPlaybackServ = new StudioPlaybackService();
$playBackList = $studioPlaybackServ->list_by_conds(['lm_id' => array_column($list, 'lm_id')]);
$lmIdWithPlayBack = [];
foreach ($playBackList as $item) {
// 根据 lm_id 归总 并把序号作为 key 后面再拿来排序用
$lmIdWithPlayBack[$item['lm_id']][$item['number']] = $item['sd'];
}
// 格式化列表数据
foreach ($list as &$v) {
// 封面图URL
$v['pic_url'] = $mainService->formatCover($v['pic']);
// 转换状态
$v['live_status'] = $mainService->live_status($v['live_status'], $v['start_time']);
// 最后更新时间
$v['updated'] = $v['updated'] ? $v['updated'] : $v['created'];
// 视频回放地址
ksort($lmIdWithPlayBack[$v['lm_id']]);
$v['video_url'] = !empty($lmIdWithPlayBack[$v['lm_id']]) ?
array_values($lmIdWithPlayBack[$v['lm_id']]) : [];
// 是否有视频回放
$v['has_playback'] = !empty($lmIdWithPlayBack);
}
}
// 判断是否还有一条直播活动
$hasAnotherLive = $mainService->get_by_conds(['live_status' => Constant::LIVE_STATUS_PUBLISHED]);
// 返回结果
$this->_result = [
'page' => intval($page),
'limit' => intval($limit),
'total' => intval($total),
'list' => $list,
'can_create' => empty($hasAnotherLive)
];
return true;
}
/**
* @desc 组合搜索条件
* @param array $postData 搜索条件
* @return array
*/
private function assemble_where($postData)
{
$conds = [];
if (isset($postData['name'])) {
$postData['name'] = str_replace("%", '\%', $postData['name']);
$conds['name like ?'] = '%' . $postData['name'] . '%';
}
if (isset($postData['start_time_begin']) && isset($postData['start_time_end'])) {
$conds['start_time >= ?'] = $postData['start_time_begin'];
$conds['start_time <= ?'] = $postData['start_time_end'];
}
// 直播状态不为空
if (isset($postData['live_status'])) {
switch ($postData['live_status']) {
// 草稿
case Constant::LIVE_WEB_STATUS_DRAFT:
$conds['live_status'] = Constant::LIVE_STATUS_DEAFT;
break;
// 未开始
case Constant::LIVE_WEB_STATUS_NOT_START:
$conds['start_time > ?'] = MILLI_TIME;
$conds['live_status'] = Constant::LIVE_STATUS_PUBLISHED;
break;
// 进行中
case Constant::LIVE_WEB_STATUS_ING:
$conds['live_status'] = Constant::LIVE_STATUS_PUBLISHED;
$conds['start_time < ?'] = MILLI_TIME;
break;
// 已结束
case Constant::LIVE_WEB_STATUS_END:
$conds['live_status'] = Constant::LIVE_STATUS_OVER;
break;
default:
}
}
return $conds;
}
}