feat: menric
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\SystemLog;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SystemLogController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
return view('admin.system.logs', [
|
||||
'actions' => SystemLog::actionLabels(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function data(Request $request)
|
||||
{
|
||||
$q = SystemLog::query();
|
||||
|
||||
$username = trim((string) $request->query('username', ''));
|
||||
$action = trim((string) $request->query('action', ''));
|
||||
$keyword = trim((string) $request->query('keyword', ''));
|
||||
if ($username !== '') {
|
||||
$q->where('actor_username', 'like', '%'.$username.'%');
|
||||
}
|
||||
if ($action !== '' && isset(SystemLog::actionLabels()[$action])) {
|
||||
$q->where('action', $action);
|
||||
}
|
||||
if ($keyword !== '') {
|
||||
$q->where('content', 'like', '%'.$keyword.'%');
|
||||
}
|
||||
|
||||
$sortable = ['id', 'created_at'];
|
||||
$field = (string) $request->query('field', 'id');
|
||||
$order = strtolower((string) $request->query('order', 'desc')) === 'asc' ? 'asc' : 'desc';
|
||||
if (! in_array($field, $sortable, true)) {
|
||||
$field = 'id';
|
||||
}
|
||||
$q->orderBy($field, $order);
|
||||
|
||||
$limit = max(1, min(100, (int) $request->query('limit', 20)));
|
||||
$page = max(1, (int) $request->query('page', 1));
|
||||
$paginator = $q->paginate($limit, ['*'], 'page', $page);
|
||||
|
||||
$data = collect($paginator->items())->map(function (SystemLog $row) {
|
||||
return [
|
||||
'id' => $row->id,
|
||||
'actor_role' => $row->actorRoleLabel(),
|
||||
'actor_username' => $row->actor_username ?: '—',
|
||||
'content' => $row->content ?: '',
|
||||
'ip' => $row->ip ?: '',
|
||||
'created_at' => optional($row->created_at)->format('Y-m-d H:i:s'),
|
||||
];
|
||||
})->values();
|
||||
|
||||
return response()->json([
|
||||
'code' => 0,
|
||||
'msg' => '',
|
||||
'count' => $paginator->total(),
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user