CourseListController.class.php
3.84 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/11
* Time: 16:31
*/
namespace Apicp\Controller\Operate;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ArticleService;
use Common\Service\ClassService;
class CourseListController extends \Apicp\Controller\AbstractController
{
/**
* Banner 课程列表接口
* @desc 用于首页 Banner 课程列表的接口
* @param integer categoryId:false 要列表的分类 Id,为空则请求全部
* @param integer limit:false:20 每页显示的数据条数
* @param integer page:false:1 当前请求的页码
* @return array
* array(
* 'limit' => 20, // 每页显示的数据条数
* 'page' => 1, // 当前请求的页码
* 'total' => 20, // 数据总数
* 'pages' => 1, // 页码总数
* 'categoryId' => 1, // 当前请求的分类 ID
* 'list' => array(
* 'id' => '课程 ID', // 课程ID
* 'subject' => '课程标题', // 课程标题
* 'time' => '发表时间', // 发表时间
* 'categoryName' => '所属分类名称', // 分类名称
* 'categoryId' => '所属分类 ID', // 分类ID
* 'author' => '作者名称', // 作者名称
* )
* )
*
*/
public function Index_post()
{
// 验证规则
$rules = [
'categoryId' => 'integer',
'limit' => 'integer',
'page' => 'integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], array_keys($rules));
$postData = $validate->postData;
// 默认值
$postData['page'] = isset($postData['page']) ? $postData['page'] : Constant::PAGING_DEFAULT_PAGE;
$postData['limit'] = isset($postData['limit']) ? $postData['limit'] : Constant::PAGING_DEFAULT_LIMIT;
// 组合条件
$classServ = new ClassService();
$conds = ['course_type' => Constant::COURSE_TYPE_NORMAL];
if (isset($postData['categoryId'])) {
// 取子类的课程ID
$conds['class_id'] = $classServ->getChildClassIds($postData['categoryId']);
}
// 分页
list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
// 排序
$order_option = ['top_time' => 'DESC', 'update_time' => 'DESC'];
// 列表
$articleServ = new ArticleService();
$list = $articleServ->list_by_conds($conds, [$start, $perpage], $order_option);
$result = [];
if (!empty($list)) {
// 取所有分类,组合分类名称字段
$classList = $classServ->list_all();
if (!empty($classList)) {
$classList = array_combine_by_key($classList, 'class_id');
}
foreach ($list as $v) {
$result[] = [
'id' => rintval($v['article_id']),
'subject' => $v['article_title'],
'time' => rintval($v['update_time']),
'categoryName' => isset($classList[$v['class_id']]) ? $classList[$v['class_id']]['class_name'] : '',
'categoryId' => rintval($v['class_id']),
'author' => $v['ea_name']
];
}
}
// 数据总数
$total = $articleServ->count_by_conds($conds);
$this->_result = [
'page' => rintval($postData['page']),
'limit' => rintval($postData['limit']),
'total' => rintval($total),
'pages' => ceil($total/$postData['limit']),
'categoryId' => isset($postData['categoryId']) ? rintval($postData['categoryId']) : 0,
'list' => $result,
];
}
}