Files
chatroom/app/Events/RedPacketSent.php
lkddi 6fa42b90d5 功能:站长礼包系统(金币/经验双类型)+ 后台用户编辑权限收紧(仅 id=1 超管)
新增功能:
- 礼包系统:superlevel 站长可发 888 数量 10 份礼包,支持金币/经验双类型
- 发包前三按钮选择(金币礼包 / 经验礼包 / 取消),使用 chatBanner 弹窗
- 聊天室系统公告含「立即抢包」按钮,金币红色/经验紫色配色区分
- WebSocket 实时推送红包弹窗卡片至所有在线用户
- Redis LPOP 原子分发 + 数据库 unique 约束防重领,并发安全
- 弹窗打开自动拉取服务端最新状态(剩余数量/已领/过期实时刷新)
- 新增 GET /red-packet/{id}/status 状态查询接口
- 新增 CurrencySource::RED_PACKET_RECV / RED_PACKET_RECV_EXP 枚举
安全加固:
- 后台用户编辑/强杀按钮仅 id=1 超管可见(前端隐藏 + 后端 403 双重拦截)
2026-03-01 22:20:54 +08:00

76 lines
2.3 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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';
}
}