优化:好友通知弹窗根据互相状态显示不同内容

FriendAdded 事件:
- 新增 hasAddedBack 字段(B 是否已回加 A)
- Toast:已互相好友 → '你们现在互为好友 🎉'
- Toast:未回加 → '但你还没有添加对方为好友' + [ 回加] 一键操作按钮

FriendRemoved 事件:
- 新增 hadAddedBack 字段(之前是否互相好友)
- Toast:之前互相好友 → 提示 + [🗑️ 同步移除] 一键操作按钮
- Toast:单向好友 → 简单通知,无操作按钮

Toast 改进:
- 右上角 × 关闭按钮
- 快捷操作按钮支持 fetch 直接请求
- 完成后显示结果并自动关闭,延时改为 8 秒
This commit is contained in:
2026-03-01 00:54:10 +08:00
parent 700ab9def4
commit 3c2038e8fe
4 changed files with 136 additions and 31 deletions
+9 -4
View File
@@ -5,6 +5,8 @@
*
* 当用户 A 添加用户 B 为好友时,向 B 的私有频道广播此事件,
* B 的客户端收到后展示弹窗通知。
* 携带 has_added_back 字段:若 B 已将 A 加为好友则为 true(双向好友),
* 否则为 false,前端提示 B 可以点击回加。
*
* @author ChatRoom Laravel
*
@@ -27,12 +29,14 @@ class FriendAdded implements ShouldBroadcast
/**
* 构造好友添加事件。
*
* @param string $fromUsername 发起添加的用户名
* @param string $toUsername 被添加的用户名(接收通知方)
* @param string $fromUsername 发起添加的用户名A
* @param string $toUsername 被添加的用户名(B接收通知方)
* @param bool $hasAddedBack B 是否已将 A 加为好友(互相添加=true
*/
public function __construct(
public readonly string $fromUsername,
public readonly string $toUsername,
public readonly bool $hasAddedBack = false,
) {}
/**
@@ -44,9 +48,9 @@ class FriendAdded implements ShouldBroadcast
}
/**
* 广播负载:包含发起人信息,供前端弹窗使用。
* 广播负载:包含发起人信息和互相好友状态,供前端弹窗使用。
*
* @return array<string, string>
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
@@ -54,6 +58,7 @@ class FriendAdded implements ShouldBroadcast
'from_username' => $this->fromUsername,
'to_username' => $this->toUsername,
'type' => 'friend_added',
'has_added_back' => $this->hasAddedBack,
];
}
}
+9 -4
View File
@@ -5,6 +5,8 @@
*
* 当用户 A 删除用户 B 为好友时,向 B 的私有频道广播此事件,
* B 的客户端收到后展示弹窗通知。
* 携带 hadAddedBack 字段:若 B 之前也把 A 加为好友(互相好友)则为 true
* 前端可提示 B "是否同步移除对方"
*
* @author ChatRoom Laravel
*
@@ -27,12 +29,14 @@ class FriendRemoved implements ShouldBroadcast
/**
* 构造好友删除事件。
*
* @param string $fromUsername 发起删除的用户名
* @param string $toUsername 被删除的用户名(接收通知方)
* @param string $fromUsername 发起删除的用户名A
* @param string $toUsername 被删除的用户名(B接收通知方)
* @param bool $hadAddedBack B 之前是否也将 A 加为好友(互相好友=true
*/
public function __construct(
public readonly string $fromUsername,
public readonly string $toUsername,
public readonly bool $hadAddedBack = false,
) {}
/**
@@ -44,9 +48,9 @@ class FriendRemoved implements ShouldBroadcast
}
/**
* 广播负载:包含发起人信息,供前端弹窗使用。
* 广播负载:包含发起人信息和之前互相好友状态,供前端弹窗使用。
*
* @return array<string, string>
* @return array<string, mixed>
*/
public function broadcastWith(): array
{
@@ -54,6 +58,7 @@ class FriendRemoved implements ShouldBroadcast
'from_username' => $this->fromUsername,
'to_username' => $this->toUsername,
'type' => 'friend_removed',
'had_added_back' => $this->hadAddedBack,
];
}
}
+14 -4
View File
@@ -106,8 +106,13 @@ class FriendController extends Controller
'sub_time' => now(),
]);
// 广播给对方(仅对方可见
broadcast(new FriendAdded($me->username, $username));
// 检查 B 是否已将 A 加为好友(互相好友判断
$hasAddedBack = FriendRequest::where('who', $username)
->where('towho', $me->username)
->exists();
// 广播给对方(仅对方可见),携带是否已回加的状态
broadcast(new FriendAdded($me->username, $username, $hasAddedBack));
// 若对方在线,推送聊天区悄悄话
$this->notifyOnlineUser($username, $me->username, 'added', $request->input('room_id'));
@@ -140,8 +145,13 @@ class FriendController extends Controller
return response()->json(['status' => 'error', 'message' => '好友关系不存在'], 404);
}
// 广播给对方
broadcast(new FriendRemoved($me->username, $username));
// 检查 B 之前是否也将 A 加为好友(删除前的互相状态)
$hadAddedBack = FriendRequest::where('who', $username)
->where('towho', $me->username)
->exists();
// 广播给对方,携带之前的互相好友状态
broadcast(new FriendRemoved($me->username, $username, $hadAddedBack));
// 若对方在线,推送聊天区悄悄话
$this->notifyOnlineUser($username, $me->username, 'removed', $request->input('room_id'));