功能:好友面板昵称后显示在线离线状态

后端:
- ChatStateService 新增 getAllOnlineUsernames(),跨房间聚合在线用户名
- FriendController::index() 为每位好友/待回加用户附加 is_online 字段
- 在线好友自动排在列表前面

前端:
- 昵称后显示 🟢 在线 /  离线 徽标
- .fp-status-online 绿底绿字,.fp-status-offline 灰底灰字
This commit is contained in:
2026-03-03 17:42:47 +08:00
parent 36fbc9982c
commit 40fcce2db3
3 changed files with 75 additions and 14 deletions
+19 -12
View File
@@ -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,
]);
+22
View File
@@ -119,6 +119,28 @@ class ChatStateService
return array_unique($roomIds);
}
/**
* 获取全局所有在线用户名(跨所有房间,去重)。
*
* 遍历所有活跃房间,合并用户名后去重,
* 用于好友面板在线状态标记。
*
* @return array<string>
*/
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 列表,并限制最大保留数量,防止内存泄漏。
*
@@ -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;
}
</style>
{{-- ════ 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;
}