2026-03-03 15:07:36 +08:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 文件功能:运维工具控制器
|
|
|
|
|
|
* 提供缓存清理、路由清理、视图清理、房间在线名单清理等一键运维操作
|
|
|
|
|
|
* 仅 id=1 超管可访问
|
|
|
|
|
|
*
|
|
|
|
|
|
* @author ChatRoom Laravel
|
|
|
|
|
|
*
|
|
|
|
|
|
* @version 1.0.0
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
|
|
|
|
|
|
|
|
use App\Http\Controllers\Controller;
|
|
|
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
|
|
use Illuminate\Support\Facades\Artisan;
|
|
|
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
|
|
use Illuminate\Support\Facades\Redis;
|
|
|
|
|
|
use Illuminate\View\View;
|
|
|
|
|
|
|
|
|
|
|
|
class OpsController extends Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 运维工具主页
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function index(): View
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Auth::id() !== 1) {
|
|
|
|
|
|
abort(403, '无权限操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return view('admin.ops.index');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清理应用缓存(config:clear + cache:clear)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function clearCache(): RedirectResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Auth::id() !== 1) {
|
|
|
|
|
|
abort(403, '无权限操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Artisan::call('config:clear');
|
|
|
|
|
|
Artisan::call('cache:clear');
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.ops.index')
|
|
|
|
|
|
->with('ops_success', '✅ 应用缓存已清除(config:clear + cache:clear)');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清理路由缓存(route:clear)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function clearRoutes(): RedirectResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Auth::id() !== 1) {
|
|
|
|
|
|
abort(403, '无权限操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Artisan::call('route:clear');
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.ops.index')
|
|
|
|
|
|
->with('ops_success', '✅ 路由缓存已清除(route:clear)');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清理视图缓存(view:clear)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function clearViews(): RedirectResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Auth::id() !== 1) {
|
|
|
|
|
|
abort(403, '无权限操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Artisan::call('view:clear');
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.ops.index')
|
|
|
|
|
|
->with('ops_success', '✅ 视图缓存已清除(view:clear)');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 清理所有房间 Redis 在线名单(清除幽灵在线脏数据)
|
|
|
|
|
|
*/
|
|
|
|
|
|
public function clearRoomOnline(): RedirectResponse
|
|
|
|
|
|
{
|
|
|
|
|
|
if (Auth::id() !== 1) {
|
|
|
|
|
|
abort(403, '无权限操作');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-03-12 15:26:54 +08:00
|
|
|
|
$prefix = config('database.redis.options.prefix', '');
|
|
|
|
|
|
$cursor = '0';
|
2026-03-03 15:07:36 +08:00
|
|
|
|
$cleaned = 0;
|
|
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
|
[$cursor, $keys] = Redis::scan($cursor, ['match' => $prefix.'room:*:users', 'count' => 100]);
|
|
|
|
|
|
foreach ($keys ?? [] as $fullKey) {
|
|
|
|
|
|
// 去掉前缀,还原为 Laravel Facade 使用的短 Key
|
|
|
|
|
|
$shortKey = $prefix ? substr($fullKey, strlen($prefix)) : $fullKey;
|
|
|
|
|
|
Redis::del($shortKey);
|
|
|
|
|
|
$cleaned++;
|
|
|
|
|
|
}
|
|
|
|
|
|
} while ($cursor !== '0');
|
|
|
|
|
|
|
|
|
|
|
|
return redirect()->route('admin.ops.index')
|
|
|
|
|
|
->with('ops_success', "✅ 已清理 {$cleaned} 个房间的在线名单(幽灵在线已清除)");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|