SystemLogController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace App\Module\System\AdminControllers;
  3. use App\Module\System\Models\SystemLog;
  4. use Dcat\Admin\Grid;
  5. use Dcat\Admin\Show;
  6. use Spatie\RouteAttributes\Attributes\Resource;
  7. use UCore\DcatAdmin\AdminController;
  8. use UCore\DcatAdmin\FilterHelper;
  9. use UCore\DcatAdmin\GridHelper;
  10. /**
  11. * 系统日志管理控制器 - 只读
  12. */
  13. #[Resource('system-logs', names: 'dcat.admin.system-logs')]
  14. class SystemLogController extends AdminController
  15. {
  16. /**
  17. * 页面标题
  18. *
  19. * @var string
  20. */
  21. protected $title = '系统日志';
  22. /**
  23. * 列表页面
  24. *
  25. * @return Grid
  26. */
  27. protected function grid()
  28. {
  29. return Grid::make(new SystemLog(), function (Grid $grid) {
  30. $helper = new GridHelper($grid, $this);
  31. // 按ID倒序排列,显示最新的日志
  32. $grid->model()->orderByDesc('id');
  33. $helper->columnId();
  34. $grid->column('level1', '来源类型')->label('primary');
  35. $grid->column('message', '日志消息')->limit(80);
  36. $grid->column('data1', '数据')->display(function ($value) {
  37. if (empty($value)) {
  38. return '-';
  39. }
  40. // 尝试解析JSON数据
  41. $decoded = json_decode($value, true);
  42. if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
  43. return json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
  44. }
  45. return $value;
  46. })->limit(50);
  47. $helper->columnCreatedAt();
  48. // 禁用创建按钮
  49. $grid->disableCreateButton();
  50. // 禁用编辑和删除按钮
  51. $grid->actions(function (Grid\Displayers\Actions $actions) {
  52. $actions->disableEdit();
  53. $actions->disableDelete();
  54. });
  55. // 筛选器
  56. $grid->filter(function (Grid\Filter $filter) {
  57. $helper = new FilterHelper($filter, $this);
  58. $helper->equalId();
  59. $filter->equal('level1', '来源类型');
  60. $filter->like('message', '日志消息');
  61. $helper->betweenDatetime('created_at', '创建时间');
  62. });
  63. });
  64. }
  65. /**
  66. * 详情页面
  67. *
  68. * @param mixed $id
  69. * @return Show
  70. */
  71. protected function detail($id)
  72. {
  73. return Show::make($id, new SystemLog(), function (Show $show) {
  74. $show->field('id', 'ID');
  75. $show->field('level1', '来源类型');
  76. $show->field('message', '日志消息');
  77. $show->field('data1', '数据')->as(function ($value) {
  78. if (empty($value)) {
  79. return '-';
  80. }
  81. // 尝试解析JSON数据并格式化显示
  82. $decoded = json_decode($value, true);
  83. if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
  84. return '<pre>' . json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . '</pre>';
  85. }
  86. return $value;
  87. });
  88. $show->field('created_at', '创建时间');
  89. });
  90. }
  91. }