2026-02-26 12:02:00 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Support\Facades\Broadcast;
|
|
|
|
|
|
|
2026-04-22 10:10:40 +08:00
|
|
|
|
// 用户私有频道:仅允许用户本人订阅自己的通知频道。
|
2026-02-26 12:02:00 +08:00
|
|
|
|
Broadcast::channel('App.Models.User.{id}', function ($user, $id) {
|
|
|
|
|
|
return (int) $user->id === (int) $id;
|
|
|
|
|
|
});
|
2026-02-26 13:35:38 +08:00
|
|
|
|
|
|
|
|
|
|
// 聊天室房间 Presence Channel 鉴权与成员信息抓取
|
|
|
|
|
|
Broadcast::channel('room.{roomId}', function ($user, $roomId) {
|
2026-04-19 14:42:52 +08:00
|
|
|
|
$room = \App\Models\Room::find($roomId);
|
|
|
|
|
|
if (! $room || ! $room->canUserEnter($user)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
$chatState = app(\App\Services\ChatStateService::class);
|
|
|
|
|
|
if (! $chatState->isUserInRoom((int) $roomId, $user->username)) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2026-02-26 13:35:38 +08:00
|
|
|
|
|
2026-02-26 22:03:03 +08:00
|
|
|
|
$superLevel = (int) \App\Models\Sysparam::getValue('superlevel', '100');
|
2026-04-22 10:10:40 +08:00
|
|
|
|
// 预加载当前在职职务,供右侧在线名单直接显示职务图标与名称。
|
|
|
|
|
|
$activePosition = $user->activePosition()->with('position.department')->first();
|
|
|
|
|
|
$position = $activePosition?->position;
|
2026-02-26 22:03:03 +08:00
|
|
|
|
|
2026-02-26 13:35:38 +08:00
|
|
|
|
return [
|
|
|
|
|
|
'id' => $user->id,
|
|
|
|
|
|
'username' => $user->username,
|
|
|
|
|
|
'user_level' => $user->user_level,
|
|
|
|
|
|
'sex' => $user->sex,
|
2026-02-26 23:27:35 +08:00
|
|
|
|
'headface' => $user->headface, // 通过 accessor 读取 usersf,默认 1.gif
|
2026-02-26 22:03:03 +08:00
|
|
|
|
'vip_icon' => $user->vipIcon(),
|
|
|
|
|
|
'vip_name' => $user->vipName(),
|
|
|
|
|
|
'vip_color' => $user->isVip() ? ($user->vipLevel?->color ?? '') : '',
|
|
|
|
|
|
'is_admin' => $user->user_level >= $superLevel,
|
2026-04-22 10:10:40 +08:00
|
|
|
|
'position_icon' => $position?->icon ?? '',
|
|
|
|
|
|
'position_name' => $position?->name ?? '',
|
|
|
|
|
|
'department_name' => $position?->department?->name ?? '',
|
2026-02-26 13:35:38 +08:00
|
|
|
|
];
|
|
|
|
|
|
});
|
2026-03-01 00:48:51 +08:00
|
|
|
|
|
2026-03-01 01:41:04 +08:00
|
|
|
|
// 用户私有频道鉴权(好友通知:FriendAdded / FriendRemoved / BannerNotification)
|
|
|
|
|
|
// 使用数字 ID 命名频道,避免中文用户名导致 Pusher 频道名验证失败。
|
2026-03-01 01:54:19 +08:00
|
|
|
|
Broadcast::channel('user.{id}', function ($user, $id) {
|
|
|
|
|
|
return (int) $user->id === (int) $id;
|
2026-03-01 00:48:51 +08:00
|
|
|
|
});
|
2026-03-12 08:35:21 +08:00
|
|
|
|
|
|
|
|
|
|
// 五子棋对局私有频道(仅对局双方可订阅,用于实时同步落子)
|
|
|
|
|
|
Broadcast::channel('gomoku.{gameId}', function ($user, $gameId) {
|
|
|
|
|
|
$game = \App\Models\GomokuGame::find($gameId);
|
|
|
|
|
|
if (! $game) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return $game->belongsToUser($user->id);
|
|
|
|
|
|
});
|