ListController.class.php
5.85 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
<?php
/**
* Created by PhpStorm.
* User: tangxingguo
* Date: 2017/4/11
* Time: 10:40
*/
namespace Api\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ArticleService;
use Common\Service\ClassService;
use Common\Service\ReadService;
use Common\Service\RightService;
class ListController extends \Api\Controller\AbstractController
{
/**
* List
* @author tangxingguo
* @desc 新闻列表
* @param int page:false:1 页码
* @param int limit:false:20 每页记录数
* @param int class_id 新闻分类ID
* @return array 新闻列表
* array(
* 'page' => 1, // 页码
* 'limit' => 20, // 每页记录数
* 'total' => 200, // 记录总数
* 'class_id' => 21, // 新闻分类ID
* 'list' => array(
* array(
* 'article_id' => 12, // 新闻ID
* 'title' => '冰糖雪梨', // 新闻标题
* 'cover_id' => 'b3ddbc502e307665f346cbd6e52cc10d', // 封面ID
* 'cover_url' => 'http://qy.vchangyi.org', // 封面图片URL
* 'top_time' => 0, // 置顶时间(0为未置顶,毫秒级时间戳)
* 'send_time' => 1491880545000, // 发送、编辑时间(毫秒级时间戳)
* 'is_read' => 1, // 是否已读(1=否,2=是)
* ),
* ),
* );
*/
public function Index_post()
{
$user = $this->_login->user;
// 验证规则
$rules = [
'class_id' => 'integer',
'limit' => 'integer',
'page' => 'integer',
];
// 验证数据
$validate = new PackageValidate($rules, [], ['keyword', 'class_id', 'limit', 'page']);
$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;
// 全公司对应的新闻ID
$rightConds = ['obj_type' => Constant::RIGHT_TYPE_ALL];
$rightServ = new RightService();
$rightInfo = $rightServ->list_by_conds($rightConds);
$articleIds = array_column($rightInfo, 'article_id');
// 人员、部门、标签、职位、角色对应的新闻ID
$rightData = $rightServ->getUserRight($user);
$right = $rightData[Constant::RIGHT_TYPE_USER];
if (isset($rightData[Constant::RIGHT_TYPE_DEPARTMENT])) {
$right = array_merge($right, $rightData[Constant::RIGHT_TYPE_DEPARTMENT]);
}
if (isset($rightData[Constant::RIGHT_TYPE_TAG])) {
$right = array_merge($right, $rightData[Constant::RIGHT_TYPE_TAG]);
}
if (isset($rightData[Constant::RIGHT_TYPE_JOB])) {
$right = array_merge($right, $rightData[Constant::RIGHT_TYPE_JOB]);
}
if (isset($rightData[Constant::RIGHT_TYPE_ROLE])) {
$right = array_merge($right, $rightData[Constant::RIGHT_TYPE_ROLE]);
}
$rightConds = ['obj_id in (?)' => array_values($right)];
$rightInfo = $rightServ->list_by_conds($rightConds);
// 合并、去重新闻ID
$articleIds = array_merge($articleIds, array_column($rightInfo, 'article_id'));
$articleIds = array_values(array_unique($articleIds));
// 条件
$conds = ['news_status' => Constant::NEWS_STATUS_SEND];
if ($articleIds) {
$conds['article_id in (?)'] = $articleIds;
}
$classServ = new ClassService();
if (isset($postData['class_id']) && $postData['class_id'] > 0) {
$childList = $classServ->list_by_conds([
'parent_id' => $postData['class_id'],
'is_open' => Constant::CLASS_IS_OPEN_TRUE
]);
// 分类内有子分类
$class_ids = array_column($childList, 'class_id');
$class_ids[] = $postData['class_id'];
$conds['class_id in (?)'] = $class_ids;
} else {
$childList = $classServ->list_by_conds(['is_open' => Constant::CLASS_IS_OPEN_TRUE]);
if ($childList) {
$class_ids = array_column($childList, 'class_id');
$conds['class_id in (?)'] = $class_ids;
}
}
// 分页
list($start, $perpage) = page_limit($postData['page'], $postData['limit']);
// 排序
$order_option = ['top_time' => 'desc', 'send_time' => 'desc'];
// 总数量
if ($postData['keyword']) {
$conds['title like ?'] = '%' . trim($postData['keyword']) . '%';
}
$articleServ = new ArticleService();
$total = $articleServ->count_by_conds($conds);
// 列表
$list = [];
$readServ = new ReadService();
$newsList = $articleServ->list_by_conds($conds, [$start, $perpage], $order_option);
if ($newsList) {
// 取是否已读
$article_ids = array_column($newsList, 'article_id');
$readList = $readServ->list_by_conds(['article_id in (?)' => $article_ids, 'uid' => $user['memUid']]);
if ($readList) {
$readList = array_combine_by_key($readList, 'article_id');
}
foreach ($newsList as $k => $v) {
$newsList[$k]['is_read'] = isset($readList[$v['article_id']]) ? Constant::READ_STATUS_IS_YES : Constant::READ_STATUS_IS_NO;
}
$list = $newsList;
}
$result = [
'total' => intval($total),
'list' => $list,
];
$this->_result = array_merge($postData, $result);
}
}