2026-02-27 14:14:35 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:聊天室全屏特效广播事件
|
|
|
|
|
|
*
|
2026-02-27 16:19:21 +08:00
|
|
|
|
* 管理员或用户购买单次卡后触发,通过 WebSocket 广播给房间内用户播放 Canvas 动画。
|
|
|
|
|
|
* 支持指定接收者(target_username 为 null 则全员播放)。
|
2026-02-27 14:14:35 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
2026-02-27 16:19:21 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @version 2.0.0
|
2026-02-27 14:14:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Events;
|
|
|
|
|
|
|
|
|
|
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|
|
|
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
|
|
|
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|
|
|
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|
|
|
|
|
use Illuminate\Queue\SerializesModels;
|
|
|
|
|
|
|
|
|
|
|
|
class EffectBroadcast implements ShouldBroadcastNow
|
|
|
|
|
|
{
|
|
|
|
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 支持的特效类型列表(用于校验)
|
|
|
|
|
|
*/
|
2026-02-27 16:19:21 +08:00
|
|
|
|
public const TYPES = ['fireworks', 'rain', 'lightning', 'snow'];
|
2026-02-27 14:14:35 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 构造函数
|
|
|
|
|
|
*
|
2026-02-27 16:19:21 +08:00
|
|
|
|
* @param int $roomId 房间 ID
|
|
|
|
|
|
* @param string $type 特效类型:fireworks / rain / lightning / snow
|
|
|
|
|
|
* @param string $operator 触发特效的用户名(购买者)
|
|
|
|
|
|
* @param string|null $targetUsername 接收者用户名(null = 全员)
|
|
|
|
|
|
* @param string|null $giftMessage 附带赠言
|
2026-02-27 14:14:35 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function __construct(
|
|
|
|
|
|
public readonly int $roomId,
|
|
|
|
|
|
public readonly string $type,
|
|
|
|
|
|
public readonly string $operator,
|
2026-02-27 16:19:21 +08:00
|
|
|
|
public readonly ?string $targetUsername = null,
|
|
|
|
|
|
public readonly ?string $giftMessage = null,
|
2026-02-27 14:14:35 +08:00
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 广播频道:向房间内所有在线用户推送
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<int, \Illuminate\Broadcasting\Channel>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastOn(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
2026-02-27 16:19:21 +08:00
|
|
|
|
new PresenceChannel('room.'.$this->roomId),
|
2026-02-27 14:14:35 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-02-27 16:19:21 +08:00
|
|
|
|
* 广播数据:特效类型、操作者、目标用户、赠言
|
2026-02-27 14:14:35 +08:00
|
|
|
|
*
|
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastWith(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
2026-02-27 16:19:21 +08:00
|
|
|
|
'type' => $this->type,
|
2026-02-27 14:14:35 +08:00
|
|
|
|
'operator' => $this->operator,
|
2026-02-27 16:19:21 +08:00
|
|
|
|
'target_username' => $this->targetUsername, // null = 全员
|
|
|
|
|
|
'gift_message' => $this->giftMessage,
|
2026-02-27 14:14:35 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|