model()->orderBy('created_at', 'desc'); $grid->disableCreateButton(); $grid->disableEditButton(); $grid->disableDeleteButton(); $grid->disableBatchDelete(); // 字段配置 $grid->column('id', 'ID')->sortable(); $grid->column('app_id', '应用ID'); $grid->column('method', '请求方法')->label([ 'GET' => 'primary', 'POST' => 'success', 'PUT' => 'warning', 'DELETE' => 'danger', 'PATCH' => 'info', ]); $grid->column('uri', '请求路径')->limit(50); $grid->column('response_status', '状态码')->display(function ($status) { $color = 'secondary'; if ($status >= 200 && $status < 300) { $color = 'success'; } elseif ($status >= 400 && $status < 500) { $color = 'warning'; } elseif ($status >= 500) { $color = 'danger'; } return "{$status}"; }); $grid->column('response_time', '响应时间')->display(function ($time) { if ($time < 1000) { return $time . 'ms'; } return round($time / 1000, 2) . 's'; })->sortable(); $grid->column('ip_address', 'IP地址'); $grid->column('created_at', '请求时间')->sortable(); // 筛选器 $grid->filter(function (Grid\Filter $filter) { $filter->equal('app_id', '应用ID'); $filter->equal('method', '请求方法')->select([ 'GET' => 'GET', 'POST' => 'POST', 'PUT' => 'PUT', 'DELETE' => 'DELETE', 'PATCH' => 'PATCH', ]); $filter->between('response_status', '状态码')->integer(); $filter->like('uri', '请求路径'); $filter->like('ip_address', 'IP地址'); $filter->between('response_time', '响应时间(ms)')->integer(); $filter->between('created_at', '请求时间')->datetime(); }); // 导出功能 $grid->export()->rows(function (array $rows) { foreach ($rows as &$row) { $row['response_time'] = $row['response_time'] < 1000 ? $row['response_time'] . 'ms' : round($row['response_time'] / 1000, 2) . 's'; } return $rows; }); }); } /** * 配置详情页面 * * @return Show */ protected function detail(): Show { return Show::make(OpenApiLog::class, function (Show $show) { // 基础信息 $show->field('id', 'ID'); $show->field('request_id', '请求ID'); $show->field('app_id', '应用ID'); $show->field('method', '请求方法'); $show->field('uri', '请求路径'); $show->field('ip_address', 'IP地址'); $show->field('user_agent', 'User Agent'); // 请求信息 $show->divider('请求信息'); $show->field('headers', '请求头')->json(); $show->field('query_params', '查询参数')->json(); $show->field('body', '请求体')->code(); // 响应信息 $show->divider('响应信息'); $show->field('response_status', '状态码'); $show->field('response_headers', '响应头')->json(); $show->field('response_body', '响应体')->code(); $show->field('response_time', '响应时间')->as(function ($time) { return $time < 1000 ? $time . 'ms' : round($time / 1000, 2) . 's'; }); // 错误信息 $show->field('error_message', '错误信息'); // 时间信息 $show->divider('时间信息'); $show->field('created_at', '请求时间'); // 工具栏配置 }); } }