点开工具、字典、anything


2019-11-07(操作记录log体系)

<h4>目的:</h4> <p>对后台重要接口操作日志记录,以便“对账”</p> <h4>设计:</h4> <p>admin_operation_control(接口对照表)记录需要记日志的接口,没新增一个需要记录的接口,都在这个表里新增一条</p> <p>admin_operation_logs(调用日志表)记录接口调用</p> <p>利用laravel中间件,每次请求查询对照表,如果需要记录,就记进记录表</p> <pre><code>CREATE TABLE `admin_operation_control` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `description` varchar(100) DEFAULT NULL COMMENT '方法描述', `control` varchar(50) NOT NULL COMMENT '控制器', `func` varchar(50) NOT NULL COMMENT '方法体', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='后台日志记录操作对照表';</code></pre> <pre><code>CREATE TABLE `shopping_admin_operation_logs` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `action_id` bigint(20) DEFAULT NULL COMMENT '方法id', `admin_uid` bigint(20) NOT NULL COMMENT '操作用户id', `control` varchar(100) DEFAULT NULL COMMENT '请求方法', `func` varchar(100) DEFAULT NULL COMMENT '请求路径', `params` longtext COMMENT '请求参数', `ex_params` text COMMENT '额外参数', `respone` longtext COMMENT '返回数据', `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; </code></pre> <pre><code>&lt;?php namespace App\Http\Middleware; use App\Models\Admin\OperationControl; use App\Models\Admin\OperationLogs; use App\Services\Admin\AdminAuthService; class ApiLog { public function handle($request, \Closure $next) { $action = $request-&gt;route()-&gt;getActionName(); $action = explode('@', substr($action, strrpos($action, "\\") + 1)); $control = $action[0]; $func = $action[1]; $logEnable = OperationControl::query()-&gt;where('control', $control)-&gt;where('func', $func)-&gt;first(); if ($logEnable) { $params = json_encode($request-&gt;all(), JSON_UNESCAPED_UNICODE); $exParams = json_encode($request-&gt;route()-&gt;parameters(), JSON_UNESCAPED_UNICODE); $adminUid = AdminAuthService::getUser()['user_id'] ?? 0; OperationLogs::create([ 'action_id' =&gt; $logEnable-&gt;id, 'admin_uid' =&gt; $adminUid, 'control' =&gt; $control, 'func' =&gt; $func, 'params' =&gt; $params, 'ex_params' =&gt; $exParams, ]); } return $next($request); } }</code></pre>

页面列表

ITEM_HTML