PraiseListController.class.php
3.54 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
<?php
/**
* 【销售活动-后台】获取点赞列表
* User: WJY
* Date: 2017-11-02
*/
namespace Apicp\Controller\Activity;
use Common\Common\User;
use Common\Service\ActivityService;
use Common\Service\LikeService;
class PraiseListController extends \Apicp\Controller\AbstractController
{
// 类型为活动
const TYPE_ACTIVITY = 1;
/** @var LikeService */
protected $like_s;
/** @var User */
protected $user_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化活动信息表
$this->like_s = new LikeService();
// 实例化用户表
$this->user_s = new User();
return true;
}
public function Index_post()
{
$params = array(
'obj_id' => I('ac_id', 0, 'intval'),
'page' => I('page', ActivityService::DEFAULT_PAGE, 'intval'),
'limit' => I('limit', ActivityService::DEFAULT_LIMIT, 'intval'),
'type' => self::TYPE_ACTIVITY,
);
$list = $this->get_like_list($params);
$this->_result = $list;
return true;
}
/**
* 获取点赞列表
*
* @param array $params 查询参数
*
* @return array
*/
public function get_like_list($params = [])
{
// 每页条数
$limit = !isset($params['limit']) ? LikeService::DEFAULT_LIMIT : intval($params['limit']);
$page = !isset($params['page']) ? LikeService::DEFAULT_PAGE : intval($params['page']);
// 参数校验
$obj_id = intval($params['obj_id']);
$type = intval($params['type']);
if (!in_array($type, LikeService::$levels)) {
// 点赞类型不正确
E('_ERR_LIKE_TYPE');
}
if (!$obj_id) {
// 数据id不能为空
E('_EMPTY_DATA_ID');
}
list($start, $limit, $page) = page_limit($page, $limit);
// 分页参数
$page_option = [$start, $limit];
// 排序按照发布时间参数
$order_option = ['created' => 'DESC'];
// 组装搜索条件
$conds = ['obj_id' => $obj_id, 'type' => $type];
// 查询总条数
$total = $this->like_s->count_by_conds($conds);
$list = [];
$user_list = [];
if ($total > 0) {
// 返回参数
$fields = 'like_id,uid,created';
// 列表和总数
$list = $this->like_s->list_by_conds($conds, $page_option, $order_option, $fields);
$uids = array_column($list, 'uid');
// 查询用户集合
$users = $this->user_s->listByUid($uids);
$user_list = array_combine_by_key($users, 'memUid');
}
// 循环格式化列表数据
$res_list = [];
foreach ($list as $val) {
$value = [];
$user_info = $user_list[$val['uid']];
if (empty($user_info)) {
// 如果用户被删除
$user_info = $this->user_s->getByUid($val['uid']);
}
$value['like_id'] = $val['like_id'];
$value['uid'] = $val['uid'];
$value['username'] = strval($user_info['memUsername']);
$value['avatar'] = strval($user_info['memFace']);
$value['created'] = $val['created'];
$res_list[] = $value;
}
return [
'page' => $page,
'limit' => $limit,
'total' => intval($total),
'list' => $res_list,
];
}
}