CourseSearchController.class.php
5.09 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
<?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 CourseSearchController extends \Apicp\Controller\AbstractController
{
/**
* CourseSearch
* @desc 用于首页 Banner 选择器的课程搜索接口
* @param string kw:false 待搜索的关键词,为空则返回全部
* @param integer categoryId:false 分类筛选
* @param integer limit:false:15 每页显示的数据条数
* @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 = [
'kw' => 'max:120',
'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']);
}
if (isset($postData['kw'])) {
$conds['article_title like ?'] = '%' . $postData['kw'] . '%';
}
// 分页
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'],
'attachId' => $v['cover_id'],
'url' => 'Course/Frontend/Index/Detail/Index?article_id=' . $v['article_id'] . '&data_id=' . $v['data_id'] . '&article_type=' . $v['article_type'],
'category' => isset($classList) ? $this->_getCategory($v['class_id'], $classList) : [],
];
}
}
// 数据总数
$total = $articleServ->count_by_conds($conds);
$this->_result = [
'limit' => $postData['limit'],
'page' => $postData['page'],
'total' => intval($total),
'pages' => ceil($total/$postData['limit']),
'categoryId' => isset($postData['categoryId']) ? $postData['categoryId'] : 0,
'list' => $result,
];
}
/**
* @desc 根据分类ID取出当前分类以及父级分类信息
* @author tangxingguo
* @param int $classId 分类ID
* @param array $classList 分类列表
* @return array
*/
private function _getCategory($classId, $classList)
{
$category = [];
while (isset($classList[$classId]) && $classId != 0) {
$category[] = [
'id' => rintval($classId),
'name' => $classList[$classId]['class_name'],
'upId' => rintval($classList[$classId]['parent_id']),
'url' => $classList[$classId]['parent_id'] == 0 ? frontUrl('/app/page/course/list/list', ['class_id' => $classId]) : '',
];
$classId = $classList[$classId]['parent_id'];
}
return $category;
}
}