68 lines
2.1 KiB
PHP
68 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Admin;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\OperationLog;
|
|
use App\Support\IpAccess;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class OperationLogController extends Controller
|
|
{
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$data = $request->validate([
|
|
'page' => ['nullable', 'integer', 'min:1'],
|
|
'per_page' => ['nullable', 'integer', 'min:1', 'max:200'],
|
|
'q' => ['nullable', 'string', 'max:255'],
|
|
'method' => ['nullable', 'string', 'max:10'],
|
|
]);
|
|
|
|
$query = OperationLog::query()->orderByDesc('id');
|
|
$isAdmin = IpAccess::isAdmin($request->ip());
|
|
|
|
$search = trim((string) ($data['q'] ?? ''));
|
|
if ($search !== '') {
|
|
$query->where(function ($builder) use ($search, $isAdmin): void {
|
|
$builder->where('ip_address', 'like', "%{$search}%")
|
|
->orWhere('path', 'like', "%{$search}%")
|
|
->orWhere('route_name', 'like', "%{$search}%");
|
|
|
|
if ($isAdmin) {
|
|
$builder->orWhere('user_label', 'like', "%{$search}%");
|
|
}
|
|
});
|
|
}
|
|
|
|
$method = strtoupper((string) ($data['method'] ?? ''));
|
|
if ($method !== '') {
|
|
$query->where('method', $method);
|
|
}
|
|
|
|
$perPage = (int) ($data['per_page'] ?? 50);
|
|
$logs = $query->paginate($perPage);
|
|
$items = collect($logs->items())->map(function (OperationLog $log) use ($isAdmin): array {
|
|
$payload = $log->toArray();
|
|
if (!$isAdmin) {
|
|
$payload['user_label'] = null;
|
|
}
|
|
|
|
return $payload;
|
|
})->all();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'data' => [
|
|
'logs' => $items,
|
|
'pagination' => [
|
|
'current_page' => $logs->currentPage(),
|
|
'last_page' => $logs->lastPage(),
|
|
'per_page' => $logs->perPage(),
|
|
'total' => $logs->total(),
|
|
],
|
|
],
|
|
]);
|
|
}
|
|
}
|