修复:用户名单头像全显示默认的问题
- User::headface accessor 补充 setter,确保后台管理赋值时正确写入 usersf 字段 - changeAvatar() 修改头像后同步更新 Redis 在线用户列表 - ChatStateService 新增 getUserRooms() 方法,支持查找用户所在房间
This commit is contained in:
@@ -42,6 +42,34 @@ class ChatStateService
|
||||
Redis::hdel($key, $username);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查找用户当前所在的所有房间 ID
|
||||
*
|
||||
* 扫描 Redis 中所有 room:*:users 的哈希表,检查用户是否在其中。
|
||||
*
|
||||
* @param string $username 用户名
|
||||
* @return array<int> 房间 ID 数组
|
||||
*/
|
||||
public function getUserRooms(string $username): array
|
||||
{
|
||||
$rooms = [];
|
||||
$cursor = '0';
|
||||
do {
|
||||
[$cursor, $keys] = Redis::scan($cursor, ['match' => 'room:*:users', 'count' => 100]);
|
||||
foreach ($keys ?? [] as $key) {
|
||||
if (Redis::hexists($key, $username)) {
|
||||
// 从 key "room:123:users" 中提取房间 ID
|
||||
preg_match('/room:(\d+):users/', $key, $matches);
|
||||
if (isset($matches[1])) {
|
||||
$rooms[] = (int) $matches[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
} while ($cursor !== '0');
|
||||
|
||||
return $rooms;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定房间的所有在线用户列表。
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user