CommentListController.class.php
11.8 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
<?php
/**
* 【销售活动-后台】获取评论列表接口
*
* User: WJY
* Date: 2017-11-02
*/
namespace Apicp\Controller\Comment;
use Com\PackageValidate;
use Common\Common\Helper;
use Common\Common\User;
use Common\Service\ActivityService;
use Common\Service\CommentService;
use Common\Service\PacketrecordService;
use Common\Service\ReplyService;
class CommentListController extends \Apicp\Controller\AbstractController
{
// 回复默认页码,默认页大小
const REPLY_DEFAULT_PAGE = 1;
const REPLY_DEFAULT_LIMIT = 5;
/** @var ActivityService */
protected $activity_s;
/** @var PacketrecordService */
protected $packetrecord_s;
/** @var CommentService */
protected $comment_s;
/** @var ReplyService */
protected $reply_s;
/** @var User */
protected $user_s;
public function before_action($action = '')
{
if (!parent::before_action($action)) {
return false;
}
// 实例化活动信息表
$this->activity_s = new ActivityService();
// 实例化红包记录表
$this->packetrecord_s = new PacketrecordService();
// 实例化评论表
$this->comment_s = new CommentService();
// 实例化回复表
$this->reply_s = new ReplyService();
// 用户公共类
$this->user_s = new User();
return true;
}
public function Index_post()
{
// 验证规则
$rules = [
'ac_id' => 'require',
'check_status' => 'require|in:0,1,2',
];
$msg = [
'ac_id.require' => L('_EMPTY_ACTIVITY_ID'),
'check_status.require' => L('_ERR_COMMENT_STATUS_QUERY'),
'check_status.in' => L('_ERR_COMMENT_STATUS_QUERY'),
];
// 验证数据
$validate = new PackageValidate($rules, $msg, array_keys($rules));
$postData = $validate->postData;
$params = I('post.');
// 分页
$page = !empty($params['page']) ? intval($params['page']) : ActivityService::DEFAULT_PAGE;
$limit = !empty($params['limit']) ? intval($params['limit']) : ActivityService::DEFAULT_LIMIT;
list($start, $limit) = page_limit($page, $limit);
// 按照发布时间排序
$order_option = ['created' => 'DESC'];
// 活动获取
$activity_info = $this->activity_s->get($postData['ac_id']);
if (empty($activity_info)) {
E('_ERR_ACTIVITY_NOT_FOUND');
}
// tab头部的数目统计 待审核|已通过|已驳回
// 查询待审核
$check_conds = [
'ac_id' => $postData['ac_id'],
'check_status' => CommentService::CHECK_ING,
];
// 查询已通过
$pass_conds = [
'ac_id' => $postData['ac_id'],
'check_status' => CommentService::CHECK_OK,
];
// 查询已驳回
$fail_conds = [
'ac_id' => $postData['ac_id'],
'check_status' => CommentService::CHECK_NO,
];
// 查询当前状态数据
$comment_conds = [
'ac_id' => $postData['ac_id'],
'check_status' => $postData['check_status'],
];
// 如果查询人员
if (!empty($params['username'])) {
$uids = $this->activity_s->get_username_uid($params['username']);
// 组装用户查询条件
$check_conds['uid'] = $uids;
$pass_conds['uid'] = $uids;
$fail_conds['uid'] = $uids;
$comment_conds['uid'] = $uids;
}
$checking_total = $this->comment_s->count_by_conds($check_conds);
$pass_total = $this->comment_s->count_by_conds($pass_conds);
$fail_total = $this->comment_s->count_by_conds($fail_conds);
// 定义当前列表的总数
if (CommentService::CHECK_ING == $postData['check_status']) {
$total = $checking_total;
} elseif (CommentService::CHECK_OK == $postData['check_status']) {
$total = $pass_total;
} else {
$total = $fail_total;
}
$fields = 'comment_id,uid,created,check_time,content,check_uid,check_uname,check_phone,likes,replys,pic_ids,reason,ext_fields';
$comment_list = $this->comment_s->list_by_conds($comment_conds, [$start, $limit], $order_option, $fields);
// 获取评论列表的用户信息
$comment_uids = array_column($comment_list, 'uid');
$user_list = $this->user_s->listByUid($comment_uids);
// 开启红包 && 已通过 组装红包数据
if (ActivityService::RED_OPENED == $activity_info['is_red_open']
&& CommentService::CHECK_OK == $postData['check_status']) {
// 当前页评论ID
$comment_ids = array_column($comment_list, 'comment_id');
// 获取当前页的所有红包
$red_conds = [
'cid' => $comment_ids,
];
$red_list_all = $this->packetrecord_s->list_by_conds($red_conds);
$red_list_all = array_combine_by_key($red_list_all, 'rid');
}
// 查询回复列表
// check_avatar String 审核人头像 二期加上
foreach ($comment_list as $cmt_k => $cmt_v) {
// 存在子评论则查询获取
$reply_list = [];
if (!empty($cmt_v['replys'])) {
$reply_list = $this->get_reply_list($cmt_v['comment_id']);
}
// 如果本页评论存在红包
if (!empty($red_list_all)) {
$red_list = [];
$red_temp = array_column($red_list_all, 'cid', 'rid');
$rids = array_keys($red_temp, $cmt_v['comment_id']);
foreach ($rids as $red_v) {
$red_list[] = [
'red_id' => $red_list_all[$red_v]['rid'],
'red_type' => $red_list_all[$red_v]['p_type'],
'packet_status' => $red_list_all[$red_v]['packet_status'],
'red_money' => sprintf("%.2f", $red_list_all[$red_v]['rand_money'] / 100),
'send_uname' => $red_list_all[$red_v]['send_uname'],
'created' => $red_list_all[$red_v]['give_time']
];
}
}
// 处理已删除角色的信息
$user_info = $user_list[$cmt_v['uid']];
if (empty($user_info)) {
$user_info = $this->user_s->getByUid($cmt_v['uid']);
}
$comment_temp = [
'reply_list' => !empty($reply_list) ? $reply_list : [],
'name' => $user_info['memUsername'],
'avatar' => $user_info['memFace'],
'likes_num' => $cmt_v['likes'],
'content' => $this->comment_s->enter($cmt_v['content']),
'image_list' => $this->list_img_by_atids($cmt_v['pic_ids']),
'red_list' => !empty($red_list) ? $red_list : [],
'reply_total' => (int)$cmt_v['replys'],
'reply_page' => self::REPLY_DEFAULT_PAGE,
'reply_limit' => self::REPLY_DEFAULT_LIMIT
];
unset($comment_list[$cmt_k]['likes'], $comment_list[$cmt_k]['content']);
// 重新定义扩展字段的值
$comment_list[$cmt_k]['ext_fields'] = $this->get_ext_fields_list($comment_list[$cmt_k]['ext_fields']);
$comment_list[$cmt_k] = array_merge((array)$comment_list[$cmt_k], (array)$comment_temp);
}
$result = [
'page' => (int)$page,
'limit' => (int)$limit,
'total' => (int)$total,
'stop_pay_time' => $activity_info['stop_pay_time'],
'checking_total' => (int)$checking_total,
'pass_total' => (int)$pass_total,
'fail_total' => (int)$fail_total,
'is_red_open' => (int)$activity_info['is_red_open'],
'check_type' => (int)$activity_info['check_type'],
'list' => $comment_list
];
$this->_result = $result;
return true;
}
/**
* 获取自定义字段列表
* @param array $data 自定义字段数据
* @return array
*/
protected function get_ext_fields_list($data = [])
{
if (empty($data)) {
return [];
}
// 初始化列表
$list = [];
$data_list = unserialize($data);
// 遍历数据
foreach ($data_list as $key => $v) {
// 如果是文本类型或者时间类型
if (Helper::EXT_FIELD_TEXT_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_HI_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_YMD_TYPE == $v['type'] ||
Helper::EXT_FIELD_TIME_YMD_HI_TYPE == $v['type']
) {
$list[] = ['name' => $v['name'], 'value' => $v['text'], 'type' => intval($v['type'])];
}
// 数字类型
if (Helper::EXT_FIELD_NUMBER_TYPE == $v['type']) {
$list[] = ['name' => $v['name'], 'value' => $v['text'] . $v['unit'], 'type' => intval($v['type'])];
}
// 单选类型或者多选类型
if (Helper::EXT_FIELD_RADIO_TYPE == $v['type'] || Helper::EXT_FIELD_MULTI_SELECT_TYPE == $v['type']) {
// 默认选中项
$check = [];
foreach ($v['options'] as $item) {
// 如果被选中
if (Helper::EXT_FIELD_CHECK == $item['value']) {
$check[] = $item['key'];
}
}
$list[] = ['name' => $v['name'], 'value' => implode(';', $check), 'type' => intval($v['type'])];
}
}
return $list;
}
/**
* 根据评论ID获取回复列表
*
* @param $comment_id
*
* @return array|bool
*/
protected function get_reply_list($comment_id)
{
// 回复分页设置
list($start, $limit) = page_limit(self::REPLY_DEFAULT_PAGE, self::REPLY_DEFAULT_LIMIT);
$fields = 'comment_id,reply_id,created,content,likes,uid,pic_ids';
$reply_list = $this->reply_s->list_by_conds(['comment_id' => $comment_id], [$start, $limit],
['created' => 'DESC'], $fields);
// 获取回复列表的用户信息
$reply_uids = array_column($reply_list, 'uid');
$user_list = $this->user_s->listByUid($reply_uids);
foreach ($reply_list as $rep_k => $rep_v) {
// 处理已删除角色的信息
$user_info = $user_list[$rep_v['uid']];
if (empty($user_info)) {
$user_info = $this->user_s->getByUid($rep_v['uid']);
}
$reply_temp = [
'name' => $user_info['memUsername'],
'avatar' => $user_info['memFace'],
'time' => $rep_v['created'],
'content' => $this->reply_s->enter($rep_v['content']),
'likes_num' => $rep_v['likes'],
'image_list' => $this->list_img_by_atids($rep_v['pic_ids'])
];
unset(
$reply_list[$rep_k]['created'],
$reply_list[$rep_k]['likes'],
$reply_list[$rep_k]['content']
);
$reply_list[$rep_k] = array_merge((array)$reply_list[$rep_k], (array)$reply_temp);
}
return $reply_list;
}
/**
* 根据atid(逗号分隔)获取图片列表数组
*
* @param string $atids
*
* @return array
*/
protected function list_img_by_atids($atids = '')
{
$img_list = [];
if (!empty($atids)) {
$atid_arr = explode(',', $atids);
foreach ($atid_arr as $at_v) {
$img_list[] = [
'atId' => $at_v,
'imgUrl' => imgUrlReal($at_v)
];
}
}
return $img_list;
}
}