| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace App\Module\System\AdminControllers;
- use App\Module\System\Models\SystemLog;
- use Dcat\Admin\Grid;
- use Dcat\Admin\Show;
- use Spatie\RouteAttributes\Attributes\Resource;
- use UCore\DcatAdmin\AdminController;
- use UCore\DcatAdmin\FilterHelper;
- use UCore\DcatAdmin\GridHelper;
- /**
- * 系统日志管理控制器 - 只读
- */
- #[Resource('system-logs', names: 'dcat.admin.system-logs')]
- class SystemLogController extends AdminController
- {
- /**
- * 页面标题
- *
- * @var string
- */
- protected $title = '系统日志';
- /**
- * 列表页面
- *
- * @return Grid
- */
- protected function grid()
- {
- return Grid::make(new SystemLog(), function (Grid $grid) {
- $helper = new GridHelper($grid, $this);
- // 按ID倒序排列,显示最新的日志
- $grid->model()->orderByDesc('id');
- $helper->columnId();
- $grid->column('level1', '来源类型')->label('primary');
- $grid->column('message', '日志消息')->limit(80);
- $grid->column('data1', '数据')->display(function ($value) {
- if (empty($value)) {
- return '-';
- }
- // 尝试解析JSON数据
- $decoded = json_decode($value, true);
- if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
- return json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT);
- }
- return $value;
- })->limit(50);
- $helper->columnCreatedAt();
- // 禁用创建按钮
- $grid->disableCreateButton();
- // 禁用编辑和删除按钮
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableEdit();
- $actions->disableDelete();
- });
- // 筛选器
- $grid->filter(function (Grid\Filter $filter) {
- $helper = new FilterHelper($filter, $this);
- $helper->equalId();
- $filter->equal('level1', '来源类型');
- $filter->like('message', '日志消息');
- $helper->betweenDatetime('created_at', '创建时间');
- });
- });
- }
- /**
- * 详情页面
- *
- * @param mixed $id
- * @return Show
- */
- protected function detail($id)
- {
- return Show::make($id, new SystemLog(), function (Show $show) {
- $show->field('id', 'ID');
- $show->field('level1', '来源类型');
- $show->field('message', '日志消息');
- $show->field('data1', '数据')->as(function ($value) {
- if (empty($value)) {
- return '-';
- }
- // 尝试解析JSON数据并格式化显示
- $decoded = json_decode($value, true);
- if (json_last_error() === JSON_ERROR_NONE && is_array($decoded)) {
- return '<pre>' . json_encode($decoded, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT) . '</pre>';
- }
- return $value;
- });
- $show->field('created_at', '创建时间');
- });
- }
- }
|