ListController.class.php
5.04 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
<?php
/**
* Created by PhpStorm.
* User: liyifei2012it
* Date: 17/4/11
* Time: 15:07
*/
namespace Apicp\Controller\News;
use Com\PackageValidate;
use Common\Common\Constant;
use Common\Service\ArticleService;
use Common\Service\ClassService;
class ListController extends \Apicp\Controller\AbstractController
{
/**
* List
* @desc 新闻列表
* @ahthor liyifei
* @param Int page:1 当前页(默认第一页)
* @param Int limit:20 当前页条数
* @param string start_time 更新开始时间(毫秒级时间戳)
* @param string end_time 更新结束时间(毫秒级时间戳)
* @param string title 标题关键词
* @param Int class_id 分类ID
* @param Int news_status 发布状态(1=草稿;2=已发布;3=预发布)
* @return array
* array(
* 'total' => 100, // 总条数
* 'page' => 1, // 当前页
* 'limit' => 20, // 当前页条数
* 'list' => array( // 列表数据
* 'article_id' => 1, // 新闻ID
* 'title' => '电商冲击,实体店靠什么赢', // 标题
* 'class_name' => '导购FM', // 栏目
* 'is_secret' => 1, // 是否保密(1=不保密,2=保密)
* 'read_total' => 1, // 已阅读人数
* 'allow_read_total' => 30, // 可阅读人数
* 'news_status' => 1, // 发布状态(1=草稿,2=已发布,3=预发布)
* 'convert_status' => 1, // 转码状态(1=转码中,2=转码成功,3=转码失败)
* 'like_total' => 12, // 点赞人数
* 'comment_total' => 34, // 评论人数
* 'send_time' => '1491897290000', // 最后更新时间(毫秒级时间戳)
* 'top_time' => '1491897290000', // 置顶时间(毫秒级时间戳,0为未置顶)
* 'qr_code' => 'awddwdad.awddadad?awdadwad=awdada', // 二维码链接
* ),
* )
*/
public function Index_post()
{
// 验证规则
$rules = [
'page' => 'integer',
'limit' => 'integer',
'start_time' => 'integer',
'end_time' => 'integer',
'title' => 'max:64',
'class_id' => 'integer',
'news_status' => '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 = [];
if (isset($postData['start_time'])) {
$conds['send_time > ?'] = $postData['start_time'];
}
if (isset($postData['end_time'])) {
$conds['send_time < ?'] = $postData['end_time'];
}
if (isset($postData['title'])) {
// 查询%化需要转义
$postData['title'] = str_replace("%", '\%', $postData['title']);
$conds['title like ?'] = '%' . $postData['title'] . '%';
}
if (isset($postData['class_id'])) {
$classServ = new ClassService();
$childList = $classServ->list_by_conds(['parent_id' => $postData['class_id']]);
$class_ids = array_column($childList, 'class_id');
$class_ids[] = $postData['class_id'];
$conds['class_id in (?)'] = $class_ids;
}
if (isset($postData['news_status'])) {
if ($postData['news_status'] == Constant::NEWS_STATUS_READY_SEND) {
$conds['news_status in (?)'] = [Constant::NEWS_STATUS_READY_SEND, Constant::NEWS_STATUS_TIMING_RELEASE];
} else {
$conds['news_status = ?'] = $postData['news_status'];
}
}
// 分页
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);
if ($list) {
// 可阅读人数
foreach ($list as $k => $v) {
$list[$k]['allow_read_total'] = $v['read_total'] + $v['unread_total'];
// 加入二维码链接
$list[$k]['qr_code'] = oaUrl('Frontend/Index/NewsQrcode/index', ['article_id' => $v['article_id']]);
}
}
// 数据总数
$total = $articleServ->count_by_conds($conds);
$this->_result = [
'page' => $postData['page'],
'limit' => $postData['limit'],
'total' => intval($total),
'list' => $list,
];
}
}