75 lines
2.1 KiB
PHP
75 lines
2.1 KiB
PHP
|
|
<?php
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 文件功能:开发日志发布广播事件
|
|||
|
|
* 当管理员发布新的开发日志并勾选"通知大厅"时触发
|
|||
|
|
* 广播至 Room ID=1(星光大厅)的 presence 频道
|
|||
|
|
* 前端监听此事件并在聊天消息区显示系统通知(含可点击的查看详情链接)
|
|||
|
|
*
|
|||
|
|
* @author ChatRoom Laravel
|
|||
|
|
*
|
|||
|
|
* @version 1.0.0
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
namespace App\Events;
|
|||
|
|
|
|||
|
|
use App\Models\DevChangelog;
|
|||
|
|
use Illuminate\Broadcasting\InteractsWithSockets;
|
|||
|
|
use Illuminate\Broadcasting\PresenceChannel;
|
|||
|
|
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
|||
|
|
use Illuminate\Foundation\Events\Dispatchable;
|
|||
|
|
use Illuminate\Queue\SerializesModels;
|
|||
|
|
|
|||
|
|
class ChangelogPublished implements ShouldBroadcastNow
|
|||
|
|
{
|
|||
|
|
use Dispatchable, InteractsWithSockets, SerializesModels;
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 构造函数:传入触发通知的日志对象
|
|||
|
|
*
|
|||
|
|
* @param DevChangelog $changelog 刚发布的开发日志
|
|||
|
|
*/
|
|||
|
|
public function __construct(
|
|||
|
|
public readonly DevChangelog $changelog,
|
|||
|
|
) {}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播频道:仅向 Room 1(星光大厅)的 presence 频道广播
|
|||
|
|
* 复用现有聊天室频道机制,无需额外配置
|
|||
|
|
*
|
|||
|
|
* @return array<int, \Illuminate\Broadcasting\Channel>
|
|||
|
|
*/
|
|||
|
|
public function broadcastOn(): array
|
|||
|
|
{
|
|||
|
|
return [
|
|||
|
|
// 固定广播至 Room ID = 1 的大厅频道
|
|||
|
|
new PresenceChannel('room.1'),
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播事件名称(前端 .listen('ChangelogPublished', ...) 监听此名称)
|
|||
|
|
*/
|
|||
|
|
public function broadcastAs(): string
|
|||
|
|
{
|
|||
|
|
return 'ChangelogPublished';
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 广播携带的数据(前端可直接访问)
|
|||
|
|
*
|
|||
|
|
* @return array<string, mixed>
|
|||
|
|
*/
|
|||
|
|
public function broadcastWith(): array
|
|||
|
|
{
|
|||
|
|
return [
|
|||
|
|
'version' => $this->changelog->version,
|
|||
|
|
'title' => $this->changelog->title,
|
|||
|
|
'type' => $this->changelog->type,
|
|||
|
|
'type_label' => $this->changelog->type_label,
|
|||
|
|
// 前端点击后跳转的目标 URL,自动锚定至对应版本
|
|||
|
|
'url' => url('/changelog').'#v'.$this->changelog->version,
|
|||
|
|
];
|
|||
|
|
}
|
|||
|
|
}
|