ListController.class.php
4 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/9/12
* Time: 15:39
*/
namespace Rpc\Controller\Train;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ArticleService;
use Common\Service\ClassService;
class ListController extends \Rpc\Controller\AbstractController
{
/**
* List
* @author tangxingguo
* @desc 获取课程列表接口
* @param Int page:1 页码
* @param Int limit:10 每页记录数
* @param Int class_id 分类ID
* @param String keyword 标题关键字
* @return array
array(
'page' => 1, // 页码
'limit' => 20, // 每页记录数
'total' => 200, // 记录总数
'list' => array(
array(
'app_data_id' => '8852ce5784c43e6dddd942ae12d78fad', // 数据ID
'title' => '冰糖雪梨', // 标题
'class_name' => '企业文化', // 分类名称
'author' => '张三', // 作者
'status' => '已发布', // 状态
),
),
)
*/
public function Index()
{
$postData = $this->_params;
// 验证规则
$rules = [
'page' => 'integer',
'limit' => 'integer',
'class_id' => 'integer',
'keyword' => 'max:64',
];
// 验证请求数据
$validate = new PackageValidate();
$validate->postData = $postData;
$validate->validateParams($rules);
// 分页
$postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$postData['limit'] = isset($postData['limit']) ? $postData['limit'] : 10;
list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
// 条件
$conds = [
'course_type' => Constant::COURSE_TYPE_TRAIN,
'article_status' => Constant::ARTICLE_STATUS_SEND,
];
if (strlen($postData['keyword']) > 0) {
$conds['article_title like ?'] = '%' . $postData['keyword'] . '%';
}
$classServ = new ClassService();
if ($postData['class_id'] != 0) {
// 无限级取出当前分类及所有已启用的子分类ID
$classIds = $classServ->getChildClassIds($postData['class_id']);
$conds['class_id'] = $classIds;
} else {
// 最新课程列表,排除被禁用的分类
$isOpenClassIds = $classServ->getOpenClassIds();
$conds['class_id'] = $isOpenClassIds;
}
// 排序条件
$order_option = ['update_time' => 'desc'];
// 课程列表
$list = [];
$articleServ = new ArticleService();
$articleList = $articleServ->list_by_conds($conds, [$start, $perpage], $order_option);
if (!empty($articleList)) {
// 分类数据
$classIds = array_column($articleList, 'class_id');
$classList = $classServ->list_by_conds(['class_id' => $classIds]);
if (!empty($classList)) {
$classList = array_combine_by_key($classList, 'class_id');
}
foreach ($articleList as $article) {
$list[] = [
'app_data_id' => $article['article_id'],
'title' => $article['article_title'],
'class_name' => isset($classList[$article['class_id']]) ? $classList[$article['class_id']]['class_name'] : '',
'author' => $article['ea_name'],
'status' => '已发布',
];
}
// 总数
$total = $articleServ->count_by_conds($conds);
}
return [
'page' => $postData['page'],
'limit' => $postData['limit'],
'total' => isset($total) ? $total : 0,
'list' => $list,
];
}
}