123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- <?php
- declare (strict_types = 1);
- namespace app\middleware;
- use app\user\model\SystemLog;
- use think\facade\Cache;
- class HttpLog
- {
- private $logid;
- /**
- * 处理请求
- *
- * @param \think\Request $request
- * @param \Closure $next
- * @return Response
- */
- public function handle($request, \Closure $next)
- {
- if ($request->isOptions()) {
- error('OPTIONS请求');
- }
- $response = $next($request);
- $token=$request->param('token');
- $userinfo = Cache::get('user:info:'.$token);
- $data=[
- 'system' =>strtolower(ltrim($request->root(),"/")),
- 'controller' => strtolower($request->controller()),
- 'action' =>strtolower($request->action()),
- 'param' =>json_encode($request->param(),JSON_UNESCAPED_UNICODE),
- 'response' =>"[]",
- 'client_ip'=>$request->ip(),
- 'action_id' => $userinfo['id']??0,
- 'action_name' => $userinfo['nickname']??'system'
- ];
- $log=SystemLog::create($data);
- $this->logid=$log->id;
- return $response;
- }
- public function end(\think\Response $response)
- {
- SystemLog::where('id',$this->logid)->update(['response'=>json_encode($response->getData(),JSON_UNESCAPED_UNICODE)]);
- }
- }
|