LogController.php 14.1 KB
<?php

namespace App\Admin\Controllers\Auth;

use App\Models\Area\Area;
use App\Admin\Rewrite\Auth\Database\OperationLog;
use Encore\Admin\Controllers\LogController as BaseController;
use Encore\Admin\Grid;
use Illuminate\Support\Str;

class LogController extends BaseController
{
    /**
     * {@inheritdoc}
     */
    protected function title()
    {
        return trans('admin.operation_log');
    }

    /**
     * @return Grid
     */
    protected function grid()
    {
        $menuModel = config('admin.database.menu_model');
        $menu = $menuModel::get()->toArray();
        $operations = [];
        foreach ($menu as $key => $value) {
            if ($value['uri']) {
                $operation['url'] = 'admin'.$value['uri'];
                if ($value['uri'] == '/') {
                    $operation['url'] = 'admin';
                }
            } else {
                $operation['url'] = '';
            }

            $operation['title'] = $value['title'];
            $operations[] = $operation;
        }
        // 增加其他的设置

        $grid = new Grid(new OperationLog());
        $ignore_path = ['admin/_handle_renderable_', 'admin/user/card','admin/user/password','admin/user/card-reader'];
        $grid->model()->whereNotIn('path', $ignore_path)
            ->where('path', 'NOT LIKE', '%select-options%')
//            ->where('path', 'NOT LIKE' ,'%handle-page%')
            ->where('path', 'NOT LIKE', '%_handle_renderable_%')
            ->where('path', 'NOT LIKE', '%_handle_action_%')
            ->orderBy('id', 'DESC');
        $grid->column('id', 'ID')->sortable();
        $grid->column('user.name', '访问用户');
        $grid->path('访问地址')->label('info');
        $grid->method_zh('请求方法')->label('primary');
        $grid->ip('ip地址')->label('primary');
        $grid->show('操作描述')->display(function () use ($operations) {
            $path    =  $this->path;
            $method  =  $this->method;
            $input = json_decode($this->input, true);
            $operate = '';
            if (Str::contains($path, 'area/village/select-options')) {
                return '查询 '.Area::VILLAGE_ZH.' options 数据';
            } elseif (Str::contains($path, 'area/group/select-options')) {
                return '查询 '.Area::GROUP_ZH.' options 数据';
            } elseif (Str::contains($path, 'area/building/select-options')) {
                return '查询 '.Area::BUILDING_ZH.' options 数据';
            } elseif (Str::contains($path, 'area/room/select-options')) {
                return '查询 '.Area::ROOM_ZH.' options 数据';
            } elseif (Str::contains($path, 'admin/map-data')) {
                return '查询 地图 数据';
            } elseif (Str::contains($path, 'auth/menu')) {
                return '进入 菜单 列表';
            } elseif (Str::contains($path, 'map/room')) {
                return '地图 查询 房间';
            } elseif (Str::contains($path, 'map/data-page')) {
                return '地图 地图 数据';
            } elseif (Str::contains($path, 'admin/report-locks')) {
                return '进入 智能门锁报告 列表';
            } elseif (Str::contains($path, 'admin/user/password')) {
                return '打开 用户密码 界面';
            } elseif (Str::contains($path, 'admin/user/password')) {
                return '打开 用户密码 界面';
            }

//            if ($path == 'admin/area/room/select-options') {
//                dump($path);
//            }
            foreach ($operations as $key => $value) {
                if ($value['url']) {
                    if (Str::startsWith($path, $value['url'])) {
                       //
                        if ($path == $value['url']) {
                            if ($method == 'GET') {
                                $operate = '进入'.$value['title'] .'列表';
                            } elseif ($method == 'POST') {
                                $operate = '新增'.$value['title'];
                            }
                        } else {
                            // 定义操作
                            $behind = 'ID为<span style="font-weight:bold;font-size:18px;padding:0 3px;">
                                      {id}</span>的<span style="font-weight:bold; padding:0 3px;">'.
                                      $value['title'].
                                      '</span>';
                            $action = '';
                            $id     = 0;
                            if (Str::endsWith($path, 'edit')) {
                                $operate = '进入'.$value['title'] .'编辑页面';
                            } elseif (Str::endsWith($path, 'create')) {
                                $operate = '进入'.$value['title'] .'新增页面';
                            } elseif (Str::endsWith($path, 'chunk-file-upload')) {
                                 return '上传大视频';
                            } else {
                                if (strstr($path, 'audit/pass')) {
                                    if ($method == 'PUT') {
                                        $temp = explode('/', $path);
                                        $id =$temp[count($temp)-1];
                                        // $operate = '审核不通过ID为<span class="label label-info">'.$id.'</span>的'.$value['title'];
                                        $action     ='<span style="color: green">审核通过</span>';
                                    }
                                } elseif (strstr($path, 'audit/refusal')) {
                                    if ($method == 'PUT') {
                                        $temp = explode('/', $path);
                                        $id =$temp[count($temp)-1];
                                        // $operate = '审核不通过ID为<span class="label label-info">'.$id.'</span>的'.$value['title'];
                                        $action     ='<span style="color:red">审核不通过</span>';
                                    }
                                } elseif (strstr($path, 'offline')) {
                                    if ($method == 'PUT') {
                                        $temp = explode('/', $path);
                                        $id =$temp[count($temp)-1];
                                        //
                                        $action     ='<span style="color:#f39c12">下架</span>';
                                    }
                                } elseif (strstr($path, 'online')) {
                                    if ($method == 'PUT') {
                                        $temp = explode('/', $path);
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span style="color:#398439">上架</span>';
                                    }
                                } elseif (strstr($path, 'cancelTopping')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/cancelTopping', '', $path));
                                        $id     =$temp[count($temp)-1];
                                        $action     ='<span style="color:#f39c12">取消置顶</span>';
                                    }
                                } elseif (strstr($path, 'setTopping')) {
                                    if ($method == 'PUT') {
                                         $temp   = explode('/', str_replace('/setTopping', '', $path));
                                        $id =$temp[count($temp)-1];

                                        $action     ='<span style="color:#398439">置顶</span>';
                                    }
                                } elseif (strstr($path, 'enable')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/enable', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span style="color:#398439">启用</span>';
                                    }
                                } elseif (strstr($path, 'disable')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/disable', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span style="color:#f39c12">禁用</span>';
                                    }
                                } elseif (strstr($path, 'restore')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/restore', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span class="text text-success">恢复</span>';
                                    }
                                } elseif (strstr($path, 'drill')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/drill', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span class="text text-warning">演习</span>';
                                    }
                                } elseif (strstr($path, 'notify')) {
                                    if ($method == 'POST') {
                                        $temp   = explode('/', str_replace('/notify', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span class="text text-success">通知</span>';
                                    }
                                } elseif (strstr($path, 'update')) {
                                    if ($method == 'PUT') {
                                        $temp   = explode('/', str_replace('/update', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span style="color:#398439">更新</span>';
                                    }
                                } elseif (strstr($path, 'info')) {
                                    if ($method == 'GET') {
                                        $temp   = explode('/', str_replace('/info', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span style="color:#398439">查看</span>';
                                    }
                                } else {
                                    $id   = str_replace($value['url'] .'/', '', $path);

//                                    if ($path == 'admin/warnings/16/users') {
//                                        dump($path);dump($value['url']);
//                                        dump($value['title']);
//
//                                    }
                                    if ($method == 'GET') {
                                        $id   = str_replace('/edit', '', $id);
                                        $action     ='编辑';
                                        if ($path =='admin/auth/login') {
                                            return '<span style="color: green">登录</span>';
                                        } elseif ($path =='admin/auth/logout') {
                                            return '<span style="color:#999">退出登录</span>';
                                        } elseif ($path =='admin/statistics/users') {
                                            return '查看数据面板会员统计图';
                                        } elseif ($path =='admin/statistics/orders') {
                                            return '查看数据面板订单统计图';
                                        } elseif ($path =='admin/statistics/sales') {
                                            return '查看数据面板销售额统计图';
                                        } elseif (strstr($path, 'quotations')) {
                                            return '查看方案的报价单';
                                        }
                                    } elseif ($method == 'DELETE') {
                                        $temp   = explode('/', str_replace('/notify', '', $path));
                                        $id =$temp[count($temp)-1];
                                        $action     ='<span class="text text-danger">删除</span>';
                                    } elseif ($method == 'PUT') {
                                        $action     ='<span style="color:#398439">更新</span>';
                                    }
                                }
                                $behind    = str_replace('{id}', $id, $behind);
                                $operate = '<span style="font-weight:bold;padding:0 3px;">'.$action .'</span>'. $behind;
                            }
                        }
                    }
                }
            }
            return $operate;
        });

        $grid->column('created_at', trans('admin.created_at'));

        $grid->actions(function (Grid\Displayers\Actions $actions) {
            $actions->disableEdit();
            $actions->disableView();
        });

        $grid->disableCreateButton();
        $grid->disableExport();
        $grid->disableRowSelector();

        $grid->filter(function (Grid\Filter $filter) {
//            $userModel = config('admin.database.users_model');

            $filter->between('created_at', '操作时间')->date();

//            $filter->equal('user_id', 'User')->select($userModel::all()->pluck('name', 'id'));
            $filter->equal('method')->select(OperationLog::$methodShows)->placeholder('请求方法');
//            $filter->like('path');
//            $filter->equal('ip');
            //去掉默认的搜索
            $filter->disableIdFilter();
        });

        return $grid;
    }
}