ListController.class.php
4.95 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<?php
/**
* 【手机端】08-评价列表
* Created by PhpStorm.
* @author:wanghuan
* @date:2017-08-31
*/
namespace Api\Controller\Comment;
use Common\Common\User;
use Common\Service\CommentService;
use Common\Service\LikeService;
class ListController extends \Api\Controller\AbstractController
{
/** @var CommentService */
protected $comment_s;
/** @var LikeService */
protected $like_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化评论service
$this->comment_s = new CommentService();
// 实例化点赞service
$this->like_s = new LikeService();
return true;
}
/**
* 评价列表
*/
public function Index_post()
{
// 接收post参数
$params = I('post.');
// 培训id
$ed_id = I('post.ed_id', 0, 'intval');
// 培训id为空
if (!$ed_id) {
E('_EMPTY_ED_ID');
}
// 培训详情
$education = $this->get_education($ed_id);
// 判断分页参数是否为空,为空赋默认值
$page = !empty($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : self::DEFAULT_LIMIT;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 评价查询条件
$cond = [
'ed_id' => $ed_id,
'c_content != ?' => ''
];
// 判断是否只能看到本人的评论
if ($education['ed_comment_show_self']) {
$cond['c_uid'] = $this->uid;
}
// 获取记录总数
$total = $this->comment_s->count_by_conds($cond);
$list = [];
// 记录总数不为空,查询列表数据
if ($total) {
// 分页参数
$page_option = [$start, $limit];
// 排序:点赞量倒序、上传时间倒序
$order_option = [
'c_likes' => 'DESC',
'c_id' => 'DESC'
];
// 获取列表数据
$list = $this->comment_s->list_by_conds($cond, $page_option, $order_option);
}
// 评价列表不为空
if (!empty($list)) {
// 数据格式化
$list = $this->format_data($list);
}
// 返回结果
$this->_result = [
'total' => intval($total),
'page' => intval($page),
'limit' => intval($limit),
'list' => $list
];
}
/**
* 格式化评价列表数据
*
* @param array $comments 待格式化数据
*
* @return array 格式化后数据
*/
protected function format_data($comments)
{
// 获取关注企业员工列表
$user_list = User::instance()->listAll(['memSubscribeStatus' => 1]);
$user_list = array_combine_by_key($user_list, 'memUid');
// 点赞查询条件
$cond = [
'like_type' => LikeService::LIKE_TYPE_COMMENT,
'like_uid' => $this->uid,
'ed_id' => $comments[0]['ed_id']
];
// 当前登录用户点赞的数据
$likes = $this->like_s->list_by_conds($cond);
$likes = array_combine_by_key($likes, 'like_obj_id');
$result = [];
foreach ($comments as $key => $val) {
// 用户信息
$user = $user_list[$val['c_uid']];
// 用户头像
$avatar = $this->format_avatar($user['memFace']);
// 用户名
$username = $user['memUsername'];
// 匿名
if (CommentService::ANONYMOUS == $val['c_is_anonymous']) {
// 自己发的评价
if ($this->uid == $val['c_uid']) {
// 名称显示格式为:用户名(匿名)
$username = $username . '(匿名)';
} else { // 不是自己发的评价
// 前端判断为空显示缺省头像
$avatar = '';
// 名称显示为:匿名
$username = '匿名';
}
}
// 点赞详情
$like = $likes[$val['c_id']];
// 默认未点赞
$is_like = LikeService::USER_UNLIKE;
// 点赞存在
if (!empty($like)) {
// 已点赞
$is_like = LikeService::USER_LIKE;
}
$result[$key] = [
'c_id' => intval($val['c_id']),
'c_uid' => $val['c_uid'],
'avatar' => $avatar,
'username' => $username,
'c_content' => $this->comment_s->Enter($val['c_content']),
'created' => $val['created'],
'c_likes' => intval($val['c_likes']),
'is_like' => $is_like
];
}
return $result;
}
}