ReplyService.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* CommentService.class.php
* 评论回复信息表
* @author: daijun
* @copyright: vchangyi.com
*/
namespace Common\Service;
use Common\Common\AttachOperation;
use Common\Common\User;
use Common\Model\ActivityModel;
use Common\Model\CommentModel;
use Common\Model\LikeModel;
use Common\Model\ReplyModel;
class ReplyService extends AbstractService
{
/**
* @var LikeModel 点赞model
*/
protected $_d_like;
/**
* @var CommentModel 分享model
*/
protected $_d_comment;
/**
* @var ActivityModel 活动model
*/
protected $_d_activity;
// 构造方法
public function __construct()
{
$this->_d = new ReplyModel();
$this->_d_like = new LikeModel();
$this->_d_comment = new CommentModel();
$this->_d_activity = new ActivityModel();
parent::__construct();
}
/**
* 前端删除我的回复
*
* @param int $reply_id 回复id
* @param string $uid 前端登录用户ID
*
* @return bool
*/
public function del_reply($reply_id = 0, $uid = '')
{
// 查询回复数据
$data = $this->_d->get($reply_id);
if (empty($data)) {
// 如果数据不存在
E('_ERR_DATA_NOT_EXIST');
}
if ($uid != $data['uid']) {
// 如果不是自己发布的,则不能删除
E('_ERR_COMMENT_FORYOU');
}
try {
// 开始事务
$this->start_trans();
// 删除回复
$this->_d->delete($reply_id);
// 删除回复点赞
$this->_d_like->delete_by_conds(['obj_id' => $reply_id, 'type' => self::LIKE_REPLY_TYPE]);
// 此处给该回帖的回复数减1
$this->_d_comment->update($data['comment_id'], ['replys = replys-?' => 1]);
// 提交事务
$this->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->rollback();
E('_ERR_DEL_FAIL');
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
E('_ERR_DEL_FAIL');
}
return true;
}
/**
* 后端删除我的回复【管理端】
*
* @param int $reply_id 回复id
*
* @return bool
*/
public function del_reply_pc($reply_id = 0)
{
// 查询评论数据
$data = $this->_d->get($reply_id);
if (empty($data)) {
// 如果数据不存在
E('_ERR_DATA_NOT_EXIST');
}
try {
// 开始事务
$this->start_trans();
// 删除评论
$this->_d->delete($reply_id);
// 删除评论点赞
$this->_d_like->delete_by_conds(['obj_id' => $reply_id, 'type' => self::LIKE_REPLY_TYPE]);
// 此处给该评论的回复数减1
$this->_d_comment->update($data['comment_id'], ['replys = replys-?' => 1]);
// 提交事务
$this->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
// 事务回滚
$this->_set_error($e->getMessage(), $e->getCode());
$this->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_set_error($e->getMessage(), $e->getCode());
// 事务回滚
$this->rollback();
return false;
}
return true;
}
/**
* 发布回复【手机端】
*
* @param array $data 请求参数
* @param array $comment 分享数据
* @param array|string $user 登录用户的信息
*
* @return mixed
*/
public function publish_reply($data = [], $comment = [], $user = [])
{
$comment_id = intval($data['comment_id']);
$piclist = $data['images'];
$content = trim($data['content']);
$images = array_column($piclist, 'atId');
if (!\Com\Validator::is_len_in_range($content, 2, 140, 'utf-8')) {
//判断回复内容长度
E('_ERR_REPLY_LENGTH');
}
$pic_total = count($images);
if ($pic_total > self::IMAGES_MAX_COUNT) {
// 上传图片数量是否大于上传图片个数最大值
E('_ERR_AC_IMAGE_LENGTH');
}
// 初始化
$reply_id = 0;
try {
// 开始事务
$this->_d->start_trans();
// 组装插入数据
$insert_data = [
'ac_id' => $comment['ac_id'],
'comment_id' => $comment_id,
'content' => $content,
'pic_ids' => empty($images) ? '' : implode(',', $images),
'uid' => $user['memUid']
];
// 插入回复信息
$reply_id = $this->_d->insert($insert_data);
// 如果插入成功
if ($reply_id) {
// 更新分享回复数
$this->_d_comment->update($comment_id, ['replys=replys+(?)' => 1]);
}
//提交修改
$this->_d->commit();
} catch (\Think\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
return false;
} catch (\Exception $e) {
\Think\Log::record($e);
$this->_d->rollback();
return false;
}
// 发表回复成功且发帖人和回复人不是同一个人则发送消息提醒
if (!empty($reply_id) && $user['memUid'] != $comment['uid']) {
// 推消息给发帖人【评论消息】
$activity = $this->_d_activity->get($comment['ac_id']);
$params = [
'subject' => $activity['subject'], // 活动标题
'username' => $user['memUsername'], // 回复人
'content' => $content, // 回复内容
'uids' => $comment['uid'], // 接收消息人员集合
'comment_id' => $comment_id,
'ac_id' => $comment['ac_id']
];
$this->send_msg($params, self::MSG_COMMENT_REPLY);
}
// 附件操作
if (!empty($images)) {
$attach_serv = new AttachOperation();
$attach_serv->insert_attach(
APP_DIR,
'reply',
$reply_id,
$images
);
}
return $reply_id;
}
/**
* 格式化回复列表【手机端】
*
* @param array $reply_list 回复列表
* @param string $uid 当前用户uid
*
* @return array
*/
public function format_reply_list($reply_list = [], $uid = '')
{
// 获取用户ID集合
$uids = array_column($reply_list, 'uid');
// 初始化
$user = User::instance();
// 查询所有用户包含已删除用户信息
$user_list = $user->listUsersAll(['memUids' => $uids, 'memAll' => 1]);
// 获取回复ID集合
$replys_ids = array_column($reply_list, 'reply_id');
// 查询该用户点赞这些回复的记录
$like_list = $this->_d_like->list_by_conds(
[
'type' => self::LIKE_REPLY_TYPE,
'uid' => $uid,
'obj_id' => $replys_ids
]
);
$like_ids = array_column($like_list, 'obj_id');
foreach ($reply_list as &$v) {
// 转换数据返回格式
$v['reply_id'] = intval($v['reply_id']);
$v['likes'] = intval($v['likes']);
$v['content'] = $this->enter($v['content']);
// 组装用户数据
$user_info = $user_list[$v['uid']];
$v['uid'] = strval($v['uid']);
$v['username'] = strval($user_info['memUsername']);
$v['avatar'] = $user->avatar($v['uid'], $user_info);
$v['created'] = $this->get_time($v['created']);
// 组装是否可删除标识
if ($v['uid'] == $uid) {
// 可以删除
$v['is_del'] = self::IS_DEL_OK;
} else {
// 不可删除
$v['is_del'] = self::IS_DEL_NO;
}
// 组装是否已点赞标识
if (in_array($v['reply_id'], $like_ids)) {
// 已点赞
$v['is_like'] = self::IS_LIKE_OK;
} else {
// 未点赞
$v['is_like'] = self::IS_LIKE_NO;
}
// 获取回复图片信息
$images = [];
if (!empty($v['pic_ids'])) {
// 格式化图片
$images = $this->get_image_list($v['pic_ids']);
}
$v['images'] = $images;
}
unset($v);
return $reply_list;
}
/**
* 获取回复列表【管理端】
*
* @param array $params 请求参数
*
* @return array
*/
public function get_list_admin($params = [])
{
if (empty($params['comment_id'])) {
// 分享id不能为空
E('_EMPTY_COMMENT_ID');
}
// 默认值
$page = isset($params['page']) ? intval($params['page']) : self::DEFAULT_PAGE;
$limit = isset($params['limit']) ? intval($params['limit']) : 5;
// 分页
list($start, $limit) = page_limit($page, $limit);
// 排序:发布时间倒序
$order_option = ['created' => 'DESC'];
// 分页
$page_option = [$start, $limit];
// 查询条件
$conds = ['comment_id' => $params['comment_id']];
$total = $this->_d->count_by_conds($conds);
$res_list = [];
if ($total) {
// 查询字段
$fields = 'reply_id,comment_id,uid,content,pic_ids,likes,created';
// 获取列表
$list = $this->_d->list_by_conds($conds, $page_option, $order_option, $fields);
// 格式化列表数据
$res_list = $this->format_reply_list_admin($list);
}
return [
'total' => intval($total),
'page' => intval($page),
'limit' => intval($limit),
'list' => $res_list
];
}
/**
* 格式化回复列表数据【后台】
*
* @param array $list 列表数据
*
* @return array
*/
private function format_reply_list_admin($list = [])
{
// 取出回复的人员uid集合
$uids = array_unique(array_column($list, 'uid'));
$user = User::instance();
$user_list = $user->listByUid($uids);
$res_list = [];
foreach ($list as $v) {
$user_info = $user_list[$v['uid']];
if (empty($user_info)) {
// 获取删除的用户信息
$user_info = $user->getByUid($v['uid']);
}
// 获取回复图片信息
$image_list = [];
if (!empty($v['pic_ids'])) {
$img_arr = explode(',', $v['pic_ids']);
foreach ($img_arr as $img) {
$image_list[] = [
'atId' => $img,
'imgUrl' => imgUrlReal($img),
];
}
}
$res_list[] = [
'comment_id' => intval($v['comment_id']),
'reply_id' => intval($v['reply_id']),
'name' => $user_info['memUsername'],
'avatar' => $user_info['memFace'],
'time' => $v['created'],
'content' => $this->enter($v['content']),
'likes_num' => $v['likes'],
'image_list' => $image_list
];
}
return $res_list;
}
}