From bef797abd5348e9594d813292cfaced964d3525e Mon Sep 17 00:00:00 2001 From: lkddi Date: Wed, 22 Apr 2026 10:10:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=A8=E7=BA=BF=E5=90=8D?= =?UTF-8?q?=E5=8D=95=E8=81=8C=E5=8A=A1=E5=9B=BE=E6=A0=87=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routes/channels.php | 7 +++++++ tests/Feature/ChatControllerTest.php | 25 +++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/routes/channels.php b/routes/channels.php index 5d811ee..23fc669 100644 --- a/routes/channels.php +++ b/routes/channels.php @@ -2,6 +2,7 @@ use Illuminate\Support\Facades\Broadcast; +// 用户私有频道:仅允许用户本人订阅自己的通知频道。 Broadcast::channel('App.Models.User.{id}', function ($user, $id) { return (int) $user->id === (int) $id; }); @@ -19,6 +20,9 @@ Broadcast::channel('room.{roomId}', function ($user, $roomId) { } $superLevel = (int) \App\Models\Sysparam::getValue('superlevel', '100'); + // 预加载当前在职职务,供右侧在线名单直接显示职务图标与名称。 + $activePosition = $user->activePosition()->with('position.department')->first(); + $position = $activePosition?->position; return [ 'id' => $user->id, @@ -30,6 +34,9 @@ Broadcast::channel('room.{roomId}', function ($user, $roomId) { 'vip_name' => $user->vipName(), 'vip_color' => $user->isVip() ? ($user->vipLevel?->color ?? '') : '', 'is_admin' => $user->user_level >= $superLevel, + 'position_icon' => $position?->icon ?? '', + 'position_name' => $position?->name ?? '', + 'department_name' => $position?->department?->name ?? '', ]; }); diff --git a/tests/Feature/ChatControllerTest.php b/tests/Feature/ChatControllerTest.php index 13c707c..2258073 100644 --- a/tests/Feature/ChatControllerTest.php +++ b/tests/Feature/ChatControllerTest.php @@ -124,6 +124,31 @@ class ChatControllerTest extends TestCase $this->assertFalse($channelCallback($user, (string) $room->id)); } + /** + * 测试聊天室 Presence 频道会返回当前在职职务图标与名称。 + */ + public function test_room_presence_channel_returns_active_position_payload(): void + { + $room = Room::create([ + 'room_name' => 'posbadge', + 'door_open' => true, + ]); + $user = $this->createUserWithPositionPermissions([]); + $position = $user->activePosition()->with('position.department')->first()?->position; + $channelCallback = Broadcast::driver()->getChannels()->get('room.{roomId}'); + + $this->assertIsCallable($channelCallback); + + $this->actingAs($user)->get(route('chat.room', $room->id)); + + $authorizedPayload = $channelCallback($user, (string) $room->id); + + $this->assertIsArray($authorizedPayload); + $this->assertSame('🛡️', $authorizedPayload['position_icon'] ?? null); + $this->assertSame($position?->name, $authorizedPayload['position_name'] ?? null); + $this->assertSame($position?->department?->name, $authorizedPayload['department_name'] ?? null); + } + /** * 测试主干默认聊天室页面不会渲染虚拟形象挂载点和配置。 */