Files
chatroom/app/Events/FriendRemoved.php
lkddi 700ab9def4 feat: 好友系统全实现
后端:
- FriendController:add/remove/status/index 四个接口
- FriendAdded / FriendRemoved 广播事件(私有频道)
- channels.php 注册 user.{username} 私有频道鉴权
- routes/web.php 注册好友路由
- ChatController::init() 修复 DutyLog 在 return 后执行的 bug
- ChatController::notifyFriendsOnline() 上线时悄悄话通知好友

前端:
- user-actions:写私信 → 加好友/删好友按钮(动态状态)
- toggleFriend() 方法 + fetchUser 后加载好友状态
- scripts:监听私有频道 FriendAdded/FriendRemoved
- showFriendToast() 右下角浮窗通知(5秒自动消失)
- global-dialog 加 fdSlideIn 动画
2026-03-01 00:48:51 +08:00

60 lines
1.5 KiB
PHP

<?php
/**
* 文件功能:好友删除广播事件
*
* 当用户 A 删除用户 B 为好友时,向 B 的私有频道广播此事件,
* B 的客户端收到后展示弹窗通知。
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class FriendRemoved implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
/**
* 构造好友删除事件。
*
* @param string $fromUsername 发起删除的用户名
* @param string $toUsername 被删除的用户名(接收通知方)
*/
public function __construct(
public readonly string $fromUsername,
public readonly string $toUsername,
) {}
/**
* 广播到被删除用户的私有频道,仅本人可见。
*/
public function broadcastOn(): Channel
{
return new PrivateChannel('user.'.$this->toUsername);
}
/**
* 广播负载:包含发起人信息,供前端弹窗使用。
*
* @return array<string, string>
*/
public function broadcastWith(): array
{
return [
'from_username' => $this->fromUsername,
'to_username' => $this->toUsername,
'type' => 'friend_removed',
];
}
}