128 lines
4.5 KiB
PHP
128 lines
4.5 KiB
PHP
<?php
|
||
|
||
use App\Http\Controllers\Admin\AuthController;
|
||
use Illuminate\Support\Facades\Route;
|
||
use Illuminate\Support\Facades\Auth;
|
||
use App\Exceptions\Handler;
|
||
|
||
/*
|
||
|--------------------------------------------------------------------------
|
||
| 后台管理 API 路由
|
||
|--------------------------------------------------------------------------
|
||
|
|
||
| 这里定义后台管理相关的API路由,使用Token认证方式
|
||
| 前缀: /admin
|
||
|
|
||
*/
|
||
|
||
// 后台认证相关路由(无需认证)
|
||
Route::prefix('auth')->group(function () {
|
||
// 后台登录 (返回Token)
|
||
Route::post('/login', [AuthController::class, 'login'])->name('admin.login');
|
||
});
|
||
|
||
// 需要Token认证的路由
|
||
Route::middleware('admin.auth')->group(function () {
|
||
|
||
// 认证相关
|
||
Route::prefix('auth')->group(function () {
|
||
// 登出当前设备
|
||
Route::post('/logout', [AuthController::class, 'logout'])->name('admin.logout');
|
||
|
||
// 登出所有设备
|
||
Route::post('/logout-all', [AuthController::class, 'logoutAll'])->name('admin.logout.all');
|
||
|
||
// 获取当前用户信息
|
||
Route::get('/me', [AuthController::class, 'me'])->name('admin.me');
|
||
|
||
// 刷新Token
|
||
Route::post('/refresh', [AuthController::class, 'refresh'])->name('admin.refresh');
|
||
|
||
// 获取设备列表
|
||
Route::get('/devices', [AuthController::class, 'devices'])->name('admin.devices');
|
||
|
||
// 删除指定设备
|
||
Route::delete('/devices', [AuthController::class, 'deleteDevice'])->name('admin.devices.delete');
|
||
|
||
// 缓存管理
|
||
Route::delete('/cache', [AuthController::class, 'clearAuthCache'])->name('admin.cache.clear');
|
||
Route::get('/cache/stats', [AuthController::class, 'getCacheStats'])->name('admin.cache.stats');
|
||
|
||
// 缓存测试(仅开发环境)
|
||
if (config('app.debug')) {
|
||
Route::get('/cache/test', function () {
|
||
$tokenAuthService = app(\App\Services\Auth\TokenAuthService::class);
|
||
|
||
$stats = $tokenAuthService->getCacheStats();
|
||
|
||
return response()->json([
|
||
'success' => true,
|
||
'message' => '缓存测试结果',
|
||
'data' => $stats,
|
||
'timestamp' => now()->toDateTimeString()
|
||
]);
|
||
})->name('admin.cache.test');
|
||
}
|
||
});
|
||
|
||
// 仪表盘数据
|
||
Route::get('/dashboard', function () {
|
||
$user = Auth::user();
|
||
return response()->json([
|
||
'success' => true,
|
||
'data' => [
|
||
'message' => '欢迎来到后台管理系统',
|
||
'user' => [
|
||
'id' => $user->id,
|
||
'username' => $user->username,
|
||
'nickname' => $user->nickname,
|
||
],
|
||
'stats' => [
|
||
'total_users' => \App\Models\User::count(),
|
||
'online_users' => 1,
|
||
'system_info' => [
|
||
'php_version' => PHP_VERSION,
|
||
'laravel_version' => app()->version(),
|
||
]
|
||
]
|
||
],
|
||
'code' => 200,
|
||
'message' => 'success'
|
||
]);
|
||
})->name('admin.dashboard');
|
||
|
||
// 异常处理测试路由(仅开发环境)
|
||
if (config('app.debug')) {
|
||
Route::prefix('test')->group(function () {
|
||
// 测试参数验证异常
|
||
Route::post('/validation', function () {
|
||
request()->validate([
|
||
'required_field' => 'required|string',
|
||
'email_field' => 'required|email',
|
||
]);
|
||
return response()->json(['message' => '验证通过']);
|
||
})->name('admin.test.validation');
|
||
|
||
// 测试业务异常
|
||
Route::get('/business-exception', function () {
|
||
Handler::throw('测试数据不存在', 404);
|
||
})->name('admin.test.business');
|
||
|
||
// 测试系统异常
|
||
Route::get('/system-exception', function () {
|
||
throw new \Exception('测试系统异常');
|
||
})->name('admin.test.system');
|
||
|
||
// 测试参数错误异常
|
||
Route::get('/param-error', function () {
|
||
Handler::error('测试参数错误');
|
||
})->name('admin.test.param');
|
||
|
||
// 测试操作失败异常
|
||
Route::get('/fail', function () {
|
||
Handler::fail('测试操作失败');
|
||
})->name('admin.test.fail');
|
||
});
|
||
}
|
||
});
|