NewsSearchController.class.php
4.88 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/11
* Time: 18:36
*/
namespace Apicp\Controller\Operate;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ArticleService;
use Common\Service\ClassService;
class NewsSearchController extends \Apicp\Controller\AbstractController
{
/**
* Banner 搜索新闻接口
* @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;
// 组合条件
$conds = ['news_status' => Constant::NEWS_STATUS_SEND];
if (isset($postData['categoryId'])) {
$conds['class_id'] = $postData['categoryId'];
}
if (isset($postData['kw'])) {
$conds['title like ?'] = '%' . $postData['kw'] . '%';
}
// 分页
list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
// 排序
$order_option = ['top_time' => 'desc', 'send_time' => 'desc'];
// 列表
$articleServ = new ArticleService();
$list = $articleServ->list_by_conds($conds, [$start, $perpage], $order_option);
$resList = [];
if (!empty($list)) {
// 取分类
$classServ = new ClassService();
$classList = $classServ->list_all();
if (!empty($classList)) {
$classList = array_combine_by_key($classList, 'class_id');
}
// 格式化数据
foreach ($list as $v) {
$resList[] = [
'id' => $v['article_id'],
'subject' => $v['title'],
'time' => $v['send_time'],
'categoryName' => $v['class_name'],
'categoryId' => $v['class_id'],
'author' => $v['author'],
'attachId' => $v['cover_id'],
'url' => 'News/Frontend/Index/Detail/Index?article_id=' . $v['article_id'],
'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' => $resList,
];
}
/**
* @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' => $classId,
'name' => $classList[$classId]['class_name'],
'upId' => $classList[$classId]['parent_id'],
'url' => $classList[$classId]['parent_id'] == 0 ? frontUrl('/app/page/news/list/list', ['class_id' => $classId]) : '',
];
$classId = $classList[$classId]['parent_id'];
}
return $category;
}
}