feat: 新增 /拍一拍 功能 + 斜杠命令菜单
- 输入框输入 / 弹出命令菜单,当前支持 /拍一拍 - 选择对象后输入 /拍一拍 发送拍一拍通知 - 所有在线用户屏幕抖动 + 正常聊天样式显示消息 - 命令注册表可扩展,后续新增命令只需 push 到数组
This commit is contained in:
@@ -1415,6 +1415,76 @@ class ChatController extends Controller
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 拍一拍:用户通过 /拍一拍 命令向所选对象发送拍一拍通知。
|
||||
*/
|
||||
public function pat(Request $request, int $id): JsonResponse
|
||||
{
|
||||
$user = Auth::user();
|
||||
|
||||
if ($response = $this->ensureUserCanActInRoom($id, $user, '请先进入当前房间后再使用拍一拍。')) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
// 0. 检查用户是否被禁言
|
||||
$muteKey = "mute:{$id}:{$user->username}";
|
||||
if (Redis::exists($muteKey)) {
|
||||
$ttl = Redis::ttl($muteKey);
|
||||
$minutes = ceil($ttl / 60);
|
||||
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "您正在禁言中,还需等待约 {$minutes} 分钟。",
|
||||
], 403);
|
||||
}
|
||||
|
||||
$targetUser = $request->input('target_user', '');
|
||||
if (empty($targetUser) || $targetUser === '大家') {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => '请选择一个聊天对象(不能为大家)进行拍一拍。',
|
||||
], 422);
|
||||
}
|
||||
|
||||
// 检查目标是否在线
|
||||
$isOnline = Redis::hexists("room:{$id}:users", $targetUser);
|
||||
if (! $isOnline) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => "【{$targetUser}】目前已离开聊天室或不在线。",
|
||||
], 200);
|
||||
}
|
||||
|
||||
// 不能拍自己
|
||||
if ($targetUser === $user->username) {
|
||||
return response()->json([
|
||||
'status' => 'error',
|
||||
'message' => '不能拍自己哦~',
|
||||
], 422);
|
||||
}
|
||||
|
||||
// 获取发送者头像
|
||||
$headface = $user->usersf ?: '1.gif';
|
||||
$headSrc = str_starts_with($headface, 'storage/') ? '/'.$headface : '/images/headface/'.$headface;
|
||||
|
||||
// 构造展示文本
|
||||
$displayText = "{$user->username} 拍了拍 {$targetUser}";
|
||||
|
||||
// 广播到房间
|
||||
broadcast(new \App\Events\UserPat(
|
||||
roomId: $id,
|
||||
fromUser: $user->username,
|
||||
targetUser: $targetUser,
|
||||
displayText: $displayText,
|
||||
fromUserHeadface: $headSrc,
|
||||
));
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => $displayText,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 校验目标用户是否仍在当前房间在线,避免跨房间赠送和消息注入。
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user