diff --git a/app/Http/Controllers/FriendController.php b/app/Http/Controllers/FriendController.php index ca66e91..a8bf60b 100644 --- a/app/Http/Controllers/FriendController.php +++ b/app/Http/Controllers/FriendController.php @@ -191,42 +191,49 @@ class FriendController extends Controller $myAddedNames = $myRows->keys(); $addedMeNames = $addedMeRows->keys(); + // ── 查询全局在线用户(所有房间合并)── + $onlineUsernames = collect($this->chatState->getAllOnlineUsernames()); + // 我添加的好友详情 $friends = User::whereIn('username', $myAddedNames) ->get(['username', 'usersf', 'user_level', 'sex']) - ->map(function ($u) use ($myRows, $addedMeNames) { + ->map(function ($u) use ($myRows, $addedMeNames, $onlineUsernames) { $row = $myRows->get($u->username); return [ - 'username' => $u->username, - 'headface' => $u->headface, + 'username' => $u->username, + 'headface' => $u->headface, 'user_level' => $u->user_level, - 'sex' => $u->sex, - 'mutual' => $addedMeNames->contains($u->username), // 是否互相添加 - 'sub_time' => $row?->sub_time?->format('Y-m-d H:i') ?? '', + 'sex' => $u->sex, + 'mutual' => $addedMeNames->contains($u->username), // 是否互相添加 + 'sub_time' => $row?->sub_time?->format('Y-m-d H:i') ?? '', + 'is_online' => $onlineUsernames->contains($u->username), ]; }) + ->sortByDesc('is_online') // 在线好友排在前面 ->values(); // 对方加了我但我还未加的(pending) $pendingNames = $addedMeNames->diff($myAddedNames); $pending = User::whereIn('username', $pendingNames) ->get(['username', 'usersf', 'user_level', 'sex']) - ->map(function ($u) use ($addedMeRows) { + ->map(function ($u) use ($addedMeRows, $onlineUsernames) { $row = $addedMeRows->get($u->username); return [ - 'username' => $u->username, - 'headface' => $u->headface, + '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') ?? '', + 'sex' => $u->sex, + 'added_at' => $row?->sub_time?->format('Y-m-d H:i') ?? '', + 'is_online' => $onlineUsernames->contains($u->username), ]; }) + ->sortByDesc('is_online') ->values(); return response()->json([ - 'status' => 'success', + 'status' => 'success', 'friends' => $friends, 'pending' => $pending, ]); diff --git a/app/Services/ChatStateService.php b/app/Services/ChatStateService.php index 06c5af6..16f11b6 100644 --- a/app/Services/ChatStateService.php +++ b/app/Services/ChatStateService.php @@ -119,6 +119,28 @@ class ChatStateService return array_unique($roomIds); } + /** + * 获取全局所有在线用户名(跨所有房间,去重)。 + * + * 遍历所有活跃房间,合并用户名后去重, + * 用于好友面板在线状态标记。 + * + * @return array + */ + public function getAllOnlineUsernames(): array + { + $usernames = []; + foreach ($this->getAllActiveRoomIds() as $roomId) { + $key = "room:{$roomId}:users"; + $users = Redis::hkeys($key); // 只取 key(用户名),不取 value + foreach ($users as $username) { + $usernames[] = $username; + } + } + + return array_unique($usernames); + } + /** * 将一条新发言推入 Redis 列表,并限制最大保留数量,防止内存泄漏。 * diff --git a/resources/views/chat/partials/friend-panel.blade.php b/resources/views/chat/partials/friend-panel.blade.php index 1c59a8e..049d850 100644 --- a/resources/views/chat/partials/friend-panel.blade.php +++ b/resources/views/chat/partials/friend-panel.blade.php @@ -221,6 +221,28 @@ font-size: 12px; padding: 16px 0; } + + /* 在线状态小圆点 */ + .fp-status { + display: inline-flex; + align-items: center; + gap: 3px; + font-size: 10px; + font-weight: bold; + padding: 1px 6px; + border-radius: 10px; + flex-shrink: 0; + } + + .fp-status-online { + background: #dcfce7; + color: #15803d; + } + + .fp-status-offline { + background: #f1f5f9; + color: #94a3b8; + } {{-- ════ HTML ════ --}} @@ -400,6 +422,11 @@ name.className = 'fp-name'; name.textContent = f.username; + // 在线状态标记(映名后) + const status = document.createElement('span'); + status.className = 'fp-status ' + (f.is_online ? 'fp-status-online' : 'fp-status-offline'); + status.textContent = f.is_online ? '🟢 在线' : '⚫ 离线'; + const badge = document.createElement('span'); badge.className = 'fp-badge ' + (f.mutual ? 'fp-badge-mutual' : 'fp-badge-onesided'); badge.textContent = f.mutual ? '💚 互相好友' : '👤 单向关注'; @@ -413,7 +440,7 @@ btn.textContent = '删除'; btn.onclick = () => friendAction('remove', f.username, btn); - row.append(avatar, name, badge, date, btn); + row.append(avatar, name, status, badge, date, btn); return row; } @@ -436,6 +463,11 @@ name.className = 'fp-name'; name.textContent = p.username; + // 在线状态标记 + const status = document.createElement('span'); + status.className = 'fp-status ' + (p.is_online ? 'fp-status-online' : 'fp-status-offline'); + status.textContent = p.is_online ? '🟢 在线' : '⚫ 离线'; + const date = document.createElement('span'); date.className = 'fp-date'; date.textContent = p.added_at ? '他于 ' + p.added_at + ' 添加了我' : ''; @@ -445,7 +477,7 @@ btn.textContent = '➕ 回加'; btn.onclick = () => friendAction('add', p.username, btn); - row.append(avatar, name, date, btn); + row.append(avatar, name, status, date, btn); return row; }