LogController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Module\OpenAPI\AdminControllers;
  3. use App\Module\OpenAPI\Models\OpenApiLog;
  4. use App\Module\OpenAPI\Repositorys\OpenApiLogRepository;
  5. use Dcat\Admin\Grid;
  6. use Dcat\Admin\Show;
  7. use UCore\DcatAdmin\AdminController;
  8. use Spatie\RouteAttributes\Attributes\Resource;
  9. /**
  10. * OpenAPI调用日志管理控制器
  11. *
  12. * @AdminController(
  13. * title="API调用日志",
  14. * permission="openapi.logs"
  15. * )
  16. */
  17. #[Resource('openapi-logs')]
  18. class LogController extends AdminController
  19. {
  20. /**
  21. * 获取数据仓库
  22. *
  23. * @return string
  24. */
  25. protected function repository(): string
  26. {
  27. return OpenApiLogRepository::class;
  28. }
  29. /**
  30. * 配置数据表格
  31. *
  32. * @return Grid
  33. */
  34. protected function grid(): Grid
  35. {
  36. return Grid::make(new OpenApiLog(), function (Grid $grid) {
  37. // 基础配置
  38. $grid->model()->orderBy('created_at', 'desc');
  39. $grid->disableCreateButton();
  40. $grid->disableEditButton();
  41. $grid->disableDeleteButton();
  42. $grid->disableBatchDelete();
  43. // 字段配置
  44. $grid->column('id', 'ID')->sortable();
  45. $grid->column('app_id', '应用ID');
  46. $grid->column('method', '请求方法')->label([
  47. 'GET' => 'primary',
  48. 'POST' => 'success',
  49. 'PUT' => 'warning',
  50. 'DELETE' => 'danger',
  51. 'PATCH' => 'info',
  52. ]);
  53. $grid->column('uri', '请求路径')->limit(50);
  54. $grid->column('response_status', '状态码')->display(function ($status) {
  55. $color = 'secondary';
  56. if ($status >= 200 && $status < 300) {
  57. $color = 'success';
  58. } elseif ($status >= 400 && $status < 500) {
  59. $color = 'warning';
  60. } elseif ($status >= 500) {
  61. $color = 'danger';
  62. }
  63. return "<span class='label label-{$color}'>{$status}</span>";
  64. });
  65. $grid->column('response_time', '响应时间')->display(function ($time) {
  66. if ($time < 1000) {
  67. return $time . 'ms';
  68. }
  69. return round($time / 1000, 2) . 's';
  70. })->sortable();
  71. $grid->column('ip_address', 'IP地址');
  72. $grid->column('created_at', '请求时间')->sortable();
  73. // 筛选器
  74. $grid->filter(function (Grid\Filter $filter) {
  75. $filter->equal('app_id', '应用ID');
  76. $filter->equal('method', '请求方法')->select([
  77. 'GET' => 'GET',
  78. 'POST' => 'POST',
  79. 'PUT' => 'PUT',
  80. 'DELETE' => 'DELETE',
  81. 'PATCH' => 'PATCH',
  82. ]);
  83. $filter->between('response_status', '状态码')->integer();
  84. $filter->like('uri', '请求路径');
  85. $filter->like('ip_address', 'IP地址');
  86. $filter->between('response_time', '响应时间(ms)')->integer();
  87. $filter->between('created_at', '请求时间')->datetime();
  88. });
  89. // 导出功能
  90. $grid->export()->rows(function (array $rows) {
  91. foreach ($rows as &$row) {
  92. $row['response_time'] = $row['response_time'] < 1000
  93. ? $row['response_time'] . 'ms'
  94. : round($row['response_time'] / 1000, 2) . 's';
  95. }
  96. return $rows;
  97. });
  98. });
  99. }
  100. /**
  101. * 配置详情页面
  102. *
  103. * @return Show
  104. */
  105. protected function detail(): Show
  106. {
  107. return Show::make(OpenApiLog::class, function (Show $show) {
  108. // 基础信息
  109. $show->field('id', 'ID');
  110. $show->field('request_id', '请求ID');
  111. $show->field('app_id', '应用ID');
  112. $show->field('method', '请求方法');
  113. $show->field('uri', '请求路径');
  114. $show->field('ip_address', 'IP地址');
  115. $show->field('user_agent', 'User Agent');
  116. // 请求信息
  117. $show->divider('请求信息');
  118. $show->field('headers', '请求头')->json();
  119. $show->field('query_params', '查询参数')->json();
  120. $show->field('body', '请求体')->code();
  121. // 响应信息
  122. $show->divider('响应信息');
  123. $show->field('response_status', '状态码');
  124. $show->field('response_headers', '响应头')->json();
  125. $show->field('response_body', '响应体')->code();
  126. $show->field('response_time', '响应时间')->as(function ($time) {
  127. return $time < 1000 ? $time . 'ms' : round($time / 1000, 2) . 's';
  128. });
  129. // 错误信息
  130. $show->field('error_message', '错误信息');
  131. // 时间信息
  132. $show->divider('时间信息');
  133. $show->field('created_at', '请求时间');
  134. // 工具栏配置
  135. });
  136. }
  137. }