From 2386948fde27b09f77f28c077d415b666409c6c7 Mon Sep 17 00:00:00 2001 From: lkddi Date: Thu, 26 Feb 2026 21:49:37 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=EF=BC=9A=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E5=90=8D=E5=8D=95=E5=A4=B4=E5=83=8F=E5=85=A8=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E9=BB=98=E8=AE=A4=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - User::headface accessor 补充 setter,确保后台管理赋值时正确写入 usersf 字段 - changeAvatar() 修改头像后同步更新 Redis 在线用户列表 - ChatStateService 新增 getUserRooms() 方法,支持查找用户所在房间 --- app/Http/Controllers/ChatController.php | 16 ++++++++++++++ app/Models/User.php | 1 + app/Services/ChatStateService.php | 28 +++++++++++++++++++++++++ 3 files changed, 45 insertions(+) diff --git a/app/Http/Controllers/ChatController.php b/app/Http/Controllers/ChatController.php index 16c06a9..ff7597c 100644 --- a/app/Http/Controllers/ChatController.php +++ b/app/Http/Controllers/ChatController.php @@ -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' => '头像修改成功!', diff --git a/app/Models/User.php b/app/Models/User.php index 3a8871b..b56920b 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -86,6 +86,7 @@ class User extends Authenticatable { return Attribute::make( get: fn () => $this->usersf ?: '1.GIF', + set: fn (string $value) => ['usersf' => $value], ); } diff --git a/app/Services/ChatStateService.php b/app/Services/ChatStateService.php index 07bca52..d963bd2 100644 --- a/app/Services/ChatStateService.php +++ b/app/Services/ChatStateService.php @@ -42,6 +42,34 @@ class ChatStateService Redis::hdel($key, $username); } + /** + * 查找用户当前所在的所有房间 ID + * + * 扫描 Redis 中所有 room:*:users 的哈希表,检查用户是否在其中。 + * + * @param string $username 用户名 + * @return array 房间 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; + } + /** * 获取指定房间的所有在线用户列表。 *