feat: add my community comments
This commit is contained in:
@@ -307,6 +307,81 @@ class CommunityController extends BaseApiController
|
||||
]);
|
||||
}
|
||||
|
||||
// 我的评论列表
|
||||
public function myCommentLists()
|
||||
{
|
||||
$page = max($this->request->get('page_no/d', 1), 1);
|
||||
$size = min(max($this->request->get('page_size/d', 20), 1), 50);
|
||||
|
||||
$query = CommunityComment::where('user_id', $this->userId)->where('status', 1);
|
||||
$total = (clone $query)->count();
|
||||
$list = $query->field('id,post_id,parent_id,reply_user_id,content,create_time')
|
||||
->order('create_time desc')
|
||||
->page($page, $size)
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
$postIds = array_values(array_unique(array_filter(array_column($list, 'post_id'))));
|
||||
$postMap = [];
|
||||
if (!empty($postIds)) {
|
||||
$posts = CommunityPost::whereIn('id', $postIds)
|
||||
->field('id,status')
|
||||
->select()
|
||||
->toArray();
|
||||
foreach ($posts as $post) {
|
||||
$postMap[$post['id']] = $post;
|
||||
}
|
||||
}
|
||||
|
||||
$parentIds = array_values(array_unique(array_filter(array_column($list, 'parent_id'))));
|
||||
$parentMap = [];
|
||||
if (!empty($parentIds)) {
|
||||
$parents = CommunityComment::whereIn('id', $parentIds)
|
||||
->where('status', 1)
|
||||
->field('id,user_id,content')
|
||||
->select()
|
||||
->toArray();
|
||||
foreach ($parents as $parent) {
|
||||
$parentMap[$parent['id']] = $parent;
|
||||
}
|
||||
}
|
||||
|
||||
$replyUserIds = array_values(array_unique(array_filter(array_merge(
|
||||
array_column($list, 'reply_user_id'),
|
||||
array_column($parentMap, 'user_id')
|
||||
))));
|
||||
$userMap = [];
|
||||
if (!empty($replyUserIds)) {
|
||||
$users = User::whereIn('id', $replyUserIds)->field('id,nickname')->select()->toArray();
|
||||
foreach ($users as $user) {
|
||||
$userMap[$user['id']] = $user;
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($list as &$item) {
|
||||
$post = $postMap[$item['post_id']] ?? null;
|
||||
$item['post'] = $post && (int) $post['status'] === 1 ? [
|
||||
'id' => $post['id'],
|
||||
] : null;
|
||||
|
||||
$parent = $parentMap[$item['parent_id']] ?? null;
|
||||
$replyUserId = $item['reply_user_id'] ?: ($parent['user_id'] ?? 0);
|
||||
$item['reply_to'] = $parent ? [
|
||||
'id' => $parent['id'],
|
||||
'content' => $parent['content'],
|
||||
'user' => $userMap[$replyUserId] ?? null,
|
||||
] : null;
|
||||
}
|
||||
unset($item);
|
||||
|
||||
return $this->data([
|
||||
'lists' => $list,
|
||||
'count' => $total,
|
||||
'page_no' => $page,
|
||||
'page_size' => $size,
|
||||
]);
|
||||
}
|
||||
|
||||
// 发评论
|
||||
public function addComment()
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user