77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 文件功能:礼包红包发出事件(广播至房间所有用户)
|
||
*
|
||
* 触发时机:AdminCommandController::sendRedPacket() 成功后广播,
|
||
* 前端接收后显示红包卡片弹窗,并在聊天窗口追加系统公告。
|
||
*
|
||
* @author ChatRoom Laravel
|
||
*
|
||
* @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;
|
||
|
||
/**
|
||
* @param int $roomId 房间 ID
|
||
* @param int $envelopeId 红包 ID
|
||
* @param string $senderUsername 发包人用户名
|
||
* @param int $totalAmount 总金额(金币)
|
||
* @param int $totalCount 总份数
|
||
* @param int $expireSeconds 过期秒数(用于前端倒计时)
|
||
*/
|
||
public function __construct(
|
||
public readonly int $roomId,
|
||
public readonly int $envelopeId,
|
||
public readonly string $senderUsername,
|
||
public readonly int $totalAmount,
|
||
public readonly int $totalCount,
|
||
public readonly int $expireSeconds,
|
||
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 [
|
||
'envelope_id' => $this->envelopeId,
|
||
'sender_username' => $this->senderUsername,
|
||
'total_amount' => $this->totalAmount,
|
||
'total_count' => $this->totalCount,
|
||
'expire_seconds' => $this->expireSeconds,
|
||
'type' => $this->type,
|
||
];
|
||
}
|
||
|
||
/** 自定义事件名称(前端监听时使用)。 */
|
||
public function broadcastAs(): string
|
||
{
|
||
return 'red-packet.sent';
|
||
}
|
||
}
|