LogController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. namespace Encore\Admin\Controllers;
  3. use Encore\Admin\Auth\Database\OperationLog;
  4. use Encore\Admin\Auth\Permission;
  5. use Encore\Admin\Grid;
  6. use Encore\Admin\Layout\Content;
  7. use Encore\Admin\Show;
  8. use Illuminate\Routing\Controller;
  9. class LogController extends Controller
  10. {
  11. /**
  12. * Index interface.
  13. *
  14. * @param Content $content
  15. *
  16. * @return Content
  17. */
  18. public function index(Content $content)
  19. {
  20. return $content
  21. ->header(trans('admin.operation_log'))
  22. ->description(trans('admin.list'))
  23. ->body($this->grid());
  24. }
  25. /**
  26. * @return Grid
  27. */
  28. protected function grid()
  29. {
  30. $grid = new Grid(new OperationLog());
  31. $grid->model()->orderBy('id', 'DESC');
  32. $grid->id('ID')->sortable();
  33. $grid->user()->name('管理员');
  34. // $grid->method()->display(function ($method) {
  35. // $color = array_get(OperationLog::$methodColors, $method, 'grey');
  36. //
  37. // return "<span class=\"badge bg-$color\">$method</span>";
  38. // });
  39. $grid->message("操作信息")->width(300);
  40. $grid->path("访问地址")->label('info');
  41. $grid->ip("访问IP")->label('primary');
  42. // $grid->input()->display(function ($input) {
  43. // $input = json_decode($input, true);
  44. // $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
  45. // if (empty($input)) {
  46. // return '<code>{}</code>';
  47. // }
  48. //
  49. // return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
  50. // });
  51. $grid->created_at(trans('admin.created_at'));
  52. $grid->actions(function (Grid\Displayers\Actions $actions) {
  53. $actions->disableView(false);
  54. if (Permission::isAdministrator()) {
  55. $actions->disableDelete(false);
  56. }
  57. });
  58. $grid->disableCreation();
  59. $grid->tools(function (Grid\Tools $tools) {
  60. $tools->batch(function (Grid\Tools\BatchActions $batch) {
  61. if (Permission::isAdministrator()) {
  62. $batch->disableDelete(false);
  63. }
  64. });
  65. });
  66. $grid->filter(function ($filter) {
  67. $userModel = config('admin.database.users_model');
  68. $filter->equal('user_id', '管理员')->select($userModel::all()->pluck('name', 'id'));
  69. $types=include app_path('Admin/logs/type.php');
  70. $filter->equal('route', '操作类型')->select($types);
  71. $filter->like('path', '访问路径');
  72. $filter->equal('IP');
  73. });
  74. return $grid;
  75. }
  76. /**
  77. * Show interface.
  78. *
  79. * @param mixed $id
  80. * @param Content $content
  81. *
  82. * @return Content
  83. */
  84. public function show($id, Content $content)
  85. {
  86. return $content
  87. ->header("日志")
  88. ->description('详情')
  89. ->body($this->detail($id));
  90. }
  91. /**
  92. * Make a show builder.
  93. *
  94. * @param mixed $id
  95. *
  96. * @return Show
  97. */
  98. protected function detail($id)
  99. {
  100. $show = new Show(OperationLog::findOrFail($id));
  101. $show->id('ID');
  102. $show->ip("IP")->label('primary');
  103. $show->user("管理员")->as(function ($user) {
  104. return $user->name;
  105. });
  106. $types=include app_path('Admin/logs/type.php');
  107. $show->route('操作类型')->unescape()->as(function ($route) use ($types) {
  108. if (isset($types[$route])) {
  109. return "<span class=\"badge bg-green\">$types[$route]</span>
  110. <span class=\"badge bg-green\">$route</span>";
  111. }
  112. return "<span class=\"badge bg-green\">$route</span>";
  113. });
  114. $show->message("操作信息");
  115. $show->path("访问路径")->label('info');
  116. $show->input('提交数据')->unescape()->as(function ($input) {
  117. $input = json_decode($input, true);
  118. $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
  119. if (empty($input)) {
  120. return '<code>{}</code>';
  121. }
  122. return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
  123. });
  124. $show->created_at('创建时间');
  125. return $show;
  126. }
  127. /**
  128. * @param mixed $id
  129. *
  130. * @return \Illuminate\Http\JsonResponse
  131. */
  132. public function destroy($id)
  133. {
  134. $ids = explode(',', $id);
  135. if (OperationLog::destroy(array_filter($ids))) {
  136. $data = [
  137. 'status' => true,
  138. 'message' => trans('admin.delete_succeeded'),
  139. ];
  140. } else {
  141. $data = [
  142. 'status' => false,
  143. 'message' => trans('admin.delete_failed'),
  144. ];
  145. }
  146. return response()->json($data);
  147. }
  148. }