2026-03-01 22:20:54 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:礼包红包发出事件(广播至房间所有用户)
|
|
|
|
|
|
*
|
|
|
|
|
|
* 触发时机:AdminCommandController::sendRedPacket() 成功后广播,
|
|
|
|
|
|
* 前端接收后显示红包卡片弹窗,并在聊天窗口追加系统公告。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
2026-03-12 15:26:54 +08:00
|
|
|
|
*
|
2026-03-01 22:20:54 +08:00
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
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 RedPacketSent implements ShouldBroadcastNow
|
|
|
|
|
|
{
|
|
|
|
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2026-03-12 15:26:54 +08:00
|
|
|
|
* @param int $roomId 房间 ID
|
|
|
|
|
|
* @param int $envelopeId 红包 ID
|
|
|
|
|
|
* @param string $senderUsername 发包人用户名
|
|
|
|
|
|
* @param int $totalAmount 总金额(金币)
|
|
|
|
|
|
* @param int $totalCount 总份数
|
|
|
|
|
|
* @param int $expireSeconds 过期秒数(用于前端倒计时)
|
2026-03-01 22:20:54 +08:00
|
|
|
|
*/
|
|
|
|
|
|
public function __construct(
|
2026-03-12 15:26:54 +08:00
|
|
|
|
public readonly int $roomId,
|
|
|
|
|
|
public readonly int $envelopeId,
|
2026-03-01 22:20:54 +08:00
|
|
|
|
public readonly string $senderUsername,
|
2026-03-12 15:26:54 +08:00
|
|
|
|
public readonly int $totalAmount,
|
|
|
|
|
|
public readonly int $totalCount,
|
|
|
|
|
|
public readonly int $expireSeconds,
|
2026-03-01 22:20:54 +08:00
|
|
|
|
public readonly string $type = 'gold',
|
|
|
|
|
|
) {}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 广播至房间 Presence 频道(所有在线用户均可收到)。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<int, \Illuminate\Broadcasting\Channel>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastOn(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [new PresenceChannel('room.'.$this->roomId)];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 广播数据:前端渲染红包弹窗所需字段。
|
|
|
|
|
|
*
|
|
|
|
|
|
* @return array<string, mixed>
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function broadcastWith(): array
|
|
|
|
|
|
{
|
|
|
|
|
|
return [
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'envelope_id' => $this->envelopeId,
|
2026-03-01 22:20:54 +08:00
|
|
|
|
'sender_username' => $this->senderUsername,
|
2026-03-12 15:26:54 +08:00
|
|
|
|
'total_amount' => $this->totalAmount,
|
|
|
|
|
|
'total_count' => $this->totalCount,
|
|
|
|
|
|
'expire_seconds' => $this->expireSeconds,
|
|
|
|
|
|
'type' => $this->type,
|
2026-03-01 22:20:54 +08:00
|
|
|
|
];
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** 自定义事件名称(前端监听时使用)。 */
|
|
|
|
|
|
public function broadcastAs(): string
|
|
|
|
|
|
{
|
|
|
|
|
|
return 'red-packet.sent';
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|