123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173 |
- <?php
- namespace Encore\Admin\Controllers;
- use Encore\Admin\Auth\Database\OperationLog;
- use Encore\Admin\Auth\Permission;
- use Encore\Admin\Grid;
- use Encore\Admin\Layout\Content;
- use Encore\Admin\Show;
- use Illuminate\Routing\Controller;
- class LogController extends Controller
- {
- /**
- * Index interface.
- *
- * @param Content $content
- *
- * @return Content
- */
- public function index(Content $content)
- {
- return $content
- ->header(trans('admin.operation_log'))
- ->description(trans('admin.list'))
- ->body($this->grid());
- }
- /**
- * @return Grid
- */
- protected function grid()
- {
- $grid = new Grid(new OperationLog());
- $grid->model()->orderBy('id', 'DESC');
- $grid->id('ID')->sortable();
- $grid->user()->name('管理员');
- // $grid->method()->display(function ($method) {
- // $color = array_get(OperationLog::$methodColors, $method, 'grey');
- //
- // return "<span class=\"badge bg-$color\">$method</span>";
- // });
- $grid->message("操作信息")->width(300);
- $grid->path("访问地址")->label('info');
- $grid->ip("访问IP")->label('primary');
- // $grid->input()->display(function ($input) {
- // $input = json_decode($input, true);
- // $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
- // if (empty($input)) {
- // return '<code>{}</code>';
- // }
- //
- // return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
- // });
- $grid->created_at(trans('admin.created_at'));
- $grid->actions(function (Grid\Displayers\Actions $actions) {
- $actions->disableView(false);
- if (Permission::isAdministrator()) {
- $actions->disableDelete(false);
- }
- });
- $grid->disableCreation();
- $grid->tools(function (Grid\Tools $tools) {
- $tools->batch(function (Grid\Tools\BatchActions $batch) {
- if (Permission::isAdministrator()) {
- $batch->disableDelete(false);
- }
- });
- });
- $grid->filter(function ($filter) {
- $userModel = config('admin.database.users_model');
- $filter->equal('user_id', '管理员')->select($userModel::all()->pluck('name', 'id'));
- $types=include app_path('Admin/logs/type.php');
- $filter->equal('route', '操作类型')->select($types);
- $filter->like('path', '访问路径');
- $filter->equal('IP');
- });
- return $grid;
- }
- /**
- * Show interface.
- *
- * @param mixed $id
- * @param Content $content
- *
- * @return Content
- */
- public function show($id, Content $content)
- {
- return $content
- ->header("日志")
- ->description('详情')
- ->body($this->detail($id));
- }
- /**
- * Make a show builder.
- *
- * @param mixed $id
- *
- * @return Show
- */
- protected function detail($id)
- {
- $show = new Show(OperationLog::findOrFail($id));
- $show->id('ID');
- $show->ip("IP")->label('primary');
- $show->user("管理员")->as(function ($user) {
- return $user->name;
- });
- $types=include app_path('Admin/logs/type.php');
- $show->route('操作类型')->unescape()->as(function ($route) use ($types) {
- if (isset($types[$route])) {
- return "<span class=\"badge bg-green\">$types[$route]</span>
- <span class=\"badge bg-green\">$route</span>";
- }
- return "<span class=\"badge bg-green\">$route</span>";
- });
- $show->message("操作信息");
- $show->path("访问路径")->label('info');
- $show->input('提交数据')->unescape()->as(function ($input) {
- $input = json_decode($input, true);
- $input = array_except($input, ['_pjax', '_token', '_method', '_previous_']);
- if (empty($input)) {
- return '<code>{}</code>';
- }
- return '<pre>'.json_encode($input, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE).'</pre>';
- });
- $show->created_at('创建时间');
- return $show;
- }
- /**
- * @param mixed $id
- *
- * @return \Illuminate\Http\JsonResponse
- */
- public function destroy($id)
- {
- $ids = explode(',', $id);
- if (OperationLog::destroy(array_filter($ids))) {
- $data = [
- 'status' => true,
- 'message' => trans('admin.delete_succeeded'),
- ];
- } else {
- $data = [
- 'status' => false,
- 'message' => trans('admin.delete_failed'),
- ];
- }
- return response()->json($data);
- }
- }
|