| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- <?php
- namespace App\Module\OpenAPI\AdminControllers;
- use App\Module\OpenAPI\Models\OpenApiLog;
- use App\Module\OpenAPI\Repositorys\OpenApiLogRepository;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use UCore\DcatAdmin\AdminController;
- use Spatie\RouteAttributes\Attributes\Resource;
- /**
- * OpenAPI调用日志管理控制器
- *
- * @AdminController(
- * title="API调用日志",
- * permission="openapi.logs"
- * )
- */
- #[Resource('openapi-logs')]
- class LogController extends AdminController
- {
- /**
- * 获取数据仓库
- *
- * @return string
- */
- protected function repository(): string
- {
- return OpenApiLogRepository::class;
- }
- /**
- * 配置数据表格
- *
- * @return Grid
- */
- protected function grid(): Grid
- {
- return Grid::make(new OpenApiLog(), function (Grid $grid) {
- // 基础配置
- $grid->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 "<span class='label label-{$color}'>{$status}</span>";
- });
-
- $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', '请求时间');
- // 工具栏配置
- });
- }
- }
|