新增聊天室刷新同步与全员刷新功能
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:聊天室浏览器刷新请求广播事件
|
||||
*
|
||||
* 仅供站长触发“刷新全员”命令时使用,
|
||||
* 向当前房间所有在线用户广播前端刷新指令。
|
||||
*
|
||||
* @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 BrowserRefreshRequested implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* 构造函数:记录房间与操作者信息。
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $roomId,
|
||||
public readonly string $operator,
|
||||
public readonly string $reason = '',
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 广播频道:当前聊天室 PresenceChannel。
|
||||
*
|
||||
* @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 [
|
||||
'operator' => $this->operator,
|
||||
'reason' => $this->reason,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* 文件功能:用户定向页面刷新广播事件
|
||||
*
|
||||
* 在任命或撤销职务成功后,向目标用户私有频道推送刷新指令,
|
||||
* 确保对方页面上的权限按钮与职务状态及时同步。
|
||||
*
|
||||
* @author ChatRoom Laravel
|
||||
*
|
||||
* @version 1.0.0
|
||||
*/
|
||||
|
||||
namespace App\Events;
|
||||
|
||||
use Illuminate\Broadcasting\InteractsWithSockets;
|
||||
use Illuminate\Broadcasting\PrivateChannel;
|
||||
use Illuminate\Contracts\Broadcasting\ShouldBroadcastNow;
|
||||
use Illuminate\Foundation\Events\Dispatchable;
|
||||
use Illuminate\Queue\SerializesModels;
|
||||
|
||||
/**
|
||||
* 类功能:向指定用户广播页面刷新请求。
|
||||
*/
|
||||
class UserBrowserRefreshRequested implements ShouldBroadcastNow
|
||||
{
|
||||
use Dispatchable, InteractsWithSockets, SerializesModels;
|
||||
|
||||
/**
|
||||
* 构造函数:记录目标用户与刷新说明。
|
||||
*/
|
||||
public function __construct(
|
||||
public readonly int $targetUserId,
|
||||
public readonly string $operator,
|
||||
public readonly string $reason = '',
|
||||
) {}
|
||||
|
||||
/**
|
||||
* 广播频道:目标用户私有频道。
|
||||
*/
|
||||
public function broadcastOn(): PrivateChannel
|
||||
{
|
||||
return new PrivateChannel('user.'.$this->targetUserId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 广播数据:供前端展示提示并执行刷新。
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function broadcastWith(): array
|
||||
{
|
||||
return [
|
||||
'operator' => $this->operator,
|
||||
'reason' => $this->reason,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -16,6 +16,7 @@
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Enums\CurrencySource;
|
||||
use App\Events\BrowserRefreshRequested;
|
||||
use App\Events\EffectBroadcast;
|
||||
use App\Events\MessageSent;
|
||||
use App\Jobs\SaveMessageJob;
|
||||
@@ -449,6 +450,40 @@ class AdminCommandController extends Controller
|
||||
return response()->json(['status' => 'success', 'message' => '已执行全员清屏']);
|
||||
}
|
||||
|
||||
/**
|
||||
* 站长触发当前房间全员刷新页面。
|
||||
*
|
||||
* 仅允许 id=1 的站长使用,向当前聊天室在线用户广播刷新事件,
|
||||
* 适用于功能更新后强制让前端重新拉取最新页面状态。
|
||||
*/
|
||||
public function refreshAll(Request $request): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'room_id' => 'required|integer',
|
||||
'reason' => 'nullable|string|max:100',
|
||||
]);
|
||||
|
||||
$admin = Auth::user();
|
||||
if ((int) $admin->id !== 1) {
|
||||
return response()->json(['status' => 'error', 'message' => '仅站长可执行全员刷新'], 403);
|
||||
}
|
||||
|
||||
$roomId = (int) $request->input('room_id');
|
||||
$reason = trim((string) $request->input('reason', ''));
|
||||
|
||||
// 立即广播页面刷新指令,确保在线用户尽快拿到最新前端状态。
|
||||
broadcast(new BrowserRefreshRequested(
|
||||
roomId: $roomId,
|
||||
operator: $admin->username,
|
||||
reason: $reason,
|
||||
));
|
||||
|
||||
return response()->json([
|
||||
'status' => 'success',
|
||||
'message' => '已通知当前房间所有在线用户刷新页面',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 管理员触发全屏特效。
|
||||
*
|
||||
|
||||
@@ -14,6 +14,7 @@ namespace App\Http\Controllers;
|
||||
|
||||
use App\Events\AppointmentAnnounced;
|
||||
use App\Events\MessageSent;
|
||||
use App\Events\UserBrowserRefreshRequested;
|
||||
use App\Jobs\SaveMessageJob;
|
||||
use App\Models\Position;
|
||||
use App\Models\User;
|
||||
@@ -106,6 +107,13 @@ class ChatAppointmentController extends Controller
|
||||
icon: '✨',
|
||||
);
|
||||
}
|
||||
|
||||
// 任命成功后,通知目标用户刷新页面,及时同步输入框上方的管理按钮与权限状态。
|
||||
broadcast(new UserBrowserRefreshRequested(
|
||||
targetUserId: (int) $target->id,
|
||||
operator: $operator->username,
|
||||
reason: '你的职务已发生变更,页面权限正在同步更新。',
|
||||
));
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
@@ -161,6 +169,13 @@ class ChatAppointmentController extends Controller
|
||||
icon: '📋',
|
||||
);
|
||||
}
|
||||
|
||||
// 撤职成功后,同步通知目标用户刷新页面,移除已失效的管理入口和权限按钮。
|
||||
broadcast(new UserBrowserRefreshRequested(
|
||||
targetUserId: (int) $target->id,
|
||||
operator: $operator->username,
|
||||
reason: '你的职务已被撤销,页面权限正在同步更新。',
|
||||
));
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
|
||||
Reference in New Issue
Block a user