修复:用户名单头像全显示默认的问题
- User::headface accessor 补充 setter,确保后台管理赋值时正确写入 usersf 字段 - changeAvatar() 修改头像后同步更新 Redis 在线用户列表 - ChatStateService 新增 getUserRooms() 方法,支持查找用户所在房间
This commit is contained in:
@@ -331,6 +331,22 @@ class ChatController extends Controller
|
||||
$user->usersf = $headface;
|
||||
$user->save();
|
||||
|
||||
// 将新头像同步到 Redis 在线用户列表中(所有房间)
|
||||
// 通过更新 Redis 的用户信息,使得其他用户和自己刷新后都能看到新头像
|
||||
$superLevel = (int) Sysparam::getValue('superlevel', '100');
|
||||
$rooms = $this->chatState->getUserRooms($user->username);
|
||||
foreach ($rooms as $roomId) {
|
||||
$this->chatState->userJoin((int) $roomId, $user->username, [
|
||||
'level' => $user->user_level,
|
||||
'sex' => $user->sex,
|
||||
'headface' => $headface,
|
||||
'vip_icon' => $user->vipIcon(),
|
||||
'vip_name' => $user->vipName(),
|
||||
'vip_color' => $user->isVip() ? ($user->vipLevel?->color ?? '') : '',
|
||||
'is_admin' => $user->user_level >= $superLevel,
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => '头像修改成功!',
|
||||
|
||||
@@ -86,6 +86,7 @@ class User extends Authenticatable
|
||||
{
|
||||
return Attribute::make(
|
||||
get: fn () => $this->usersf ?: '1.GIF',
|
||||
set: fn (string $value) => ['usersf' => $value],
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -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