From 4ced484419df01f79c1dac29ac325ae1005f0777 Mon Sep 17 00:00:00 2001 From: lkddi Date: Sun, 1 Mar 2026 13:38:30 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=9A=E5=A5=BD=E5=8F=8B?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E9=9D=A2=E6=9D=BF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 后端(FriendController::index): - 返回 sub_time 添加时间 - 新增 pending 列表(对方加了我但我未回加) 包含用户信息 + added_at(对方添加我的时间) 前端(toolbar.blade.php): - 工具栏顶部加「好友」按钮(openFriendPanel) - 好友弹窗面板(#friend-panel): ① 搜索栏:输入用户名 Enter/按钮添加好友 ② 「我关注的好友」列表:头像/用户名/互相徽章/ 添加时间/删除按钮 ③ 「对方已加我,待我回加」列表:头像/用户名/ 对方添加时间/➕回加按钮 ④ 面板顶部提示区(成功/失败消息) - 所有添加/删除调用与双击用户卡片完全相同的接口 (/friend/{username}/add、/friend/{username}/remove) --- app/Http/Controllers/FriendController.php | 57 ++- .../views/chat/partials/toolbar.blade.php | 443 ++++++++++++++++++ 2 files changed, 490 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/FriendController.php b/app/Http/Controllers/FriendController.php index 74dbd6a..ca66e91 100644 --- a/app/Http/Controllers/FriendController.php +++ b/app/Http/Controllers/FriendController.php @@ -168,31 +168,68 @@ class FriendController extends Controller } /** - * 获取当前用户的好友列表(我添加的 + 对方也添加我的 = 双向好友标记)。 + * 获取当前用户的完整好友数据,供好友面板使用。 + * + * 返回两个列表: + * - friends:我已添加的好友(含互相状态、添加时间) + * - pending:对方已加我但我还未加对方的(含对方添加我的时间) */ public function index(): JsonResponse { $me = Auth::user(); - // 我添加的所有人 - $myAdded = FriendRequest::where('who', $me->username)->pluck('towho'); + // ── 我添加的好友及添加时间 ── + $myRows = FriendRequest::where('who', $me->username) + ->get(['towho', 'sub_time']) + ->keyBy('towho'); - // 也把我加了的 - $addedMe = FriendRequest::where('towho', $me->username)->pluck('who'); + // ── 把我加了的人(用于互相判断 + pending 列表)── + $addedMeRows = FriendRequest::where('towho', $me->username) + ->get(['who', 'sub_time']) + ->keyBy('who'); - $friends = User::whereIn('username', $myAdded) + $myAddedNames = $myRows->keys(); + $addedMeNames = $addedMeRows->keys(); + + // 我添加的好友详情 + $friends = User::whereIn('username', $myAddedNames) ->get(['username', 'usersf', 'user_level', 'sex']) - ->map(function ($u) use ($addedMe) { + ->map(function ($u) use ($myRows, $addedMeNames) { + $row = $myRows->get($u->username); + return [ 'username' => $u->username, 'headface' => $u->headface, 'user_level' => $u->user_level, 'sex' => $u->sex, - 'mutual' => $addedMe->contains($u->username), // 是否互相添加 + 'mutual' => $addedMeNames->contains($u->username), // 是否互相添加 + 'sub_time' => $row?->sub_time?->format('Y-m-d H:i') ?? '', ]; - }); + }) + ->values(); - return response()->json(['status' => 'success', 'friends' => $friends]); + // 对方加了我但我还未加的(pending) + $pendingNames = $addedMeNames->diff($myAddedNames); + $pending = User::whereIn('username', $pendingNames) + ->get(['username', 'usersf', 'user_level', 'sex']) + ->map(function ($u) use ($addedMeRows) { + $row = $addedMeRows->get($u->username); + + return [ + 'username' => $u->username, + 'headface' => $u->headface, + 'user_level' => $u->user_level, + 'sex' => $u->sex, + 'added_at' => $row?->sub_time?->format('Y-m-d H:i') ?? '', + ]; + }) + ->values(); + + return response()->json([ + 'status' => 'success', + 'friends' => $friends, + 'pending' => $pending, + ]); } /** diff --git a/resources/views/chat/partials/toolbar.blade.php b/resources/views/chat/partials/toolbar.blade.php index 830ffbe..18c71a4 100644 --- a/resources/views/chat/partials/toolbar.blade.php +++ b/resources/views/chat/partials/toolbar.blade.php @@ -15,6 +15,7 @@ {{-- ═══════════ 竖向工具条按钮 ═══════════ --}}
+
好友
商店
存点
娱乐
@@ -165,6 +166,448 @@
+{{-- ═══════════ 工具条相关 JS 函数 ═══════════ --}} + + {{-- ═══════════ 工具条相关 JS 函数 ═══════════ --}}