feat: 新增 /拍一拍 功能 + 斜杠命令菜单
- 输入框输入 / 弹出命令菜单,当前支持 /拍一拍 - 选择对象后输入 /拍一拍 发送拍一拍通知 - 所有在线用户屏幕抖动 + 正常聊天样式显示消息 - 命令注册表可扩展,后续新增命令只需 push 到数组
This commit is contained in:
@@ -0,0 +1,68 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:用户"拍一拍"广播事件
|
||||
*
|
||||
* 用户输入 /拍一拍 用户名 后触发,通过 WebSocket 广播给房间内所有用户,
|
||||
* 前端显示 "XXX拍了拍XXX" 消息并触发屏幕抖动动画。
|
||||
*
|
||||
* @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 UserPat implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* 构造函数
|
||||
*
|
||||
* @param int $roomId 房间 ID
|
||||
* @param string $fromUser 拍人的用户
|
||||
* @param string $targetUser 被拍的用户
|
||||
* @param string $displayText 前端展示文本,如 "流星 拍了拍 张三"
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $roomId,
|
||||
public readonly string $fromUser,
|
||||
public readonly string $targetUser,
|
||||
public readonly string $displayText,
|
||||
public readonly ?string $fromUserHeadface = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 广播频道:向房间内所有在线用户推送
|
||||
*
|
||||
* @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 [
|
||||
'from_user' => $this->fromUser,
|
||||
'target_user' => $this->targetUser,
|
||||
'display_text' => $this->displayText,
|
||||
'from_user_headface' => $this->fromUserHeadface,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -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