user(); $userModel = getAuthUserModel(); if (! $user instanceof $userModel) { return; } [$module, $controller, $action] = CatchAdmin::parseFromRouteAction(); $requestStartAt = app(Kernel::class)->requestStartedAt()->getPreciseTimestamp(3); $params = $request->all(); // 如果参数过长则不记录 if (!empty($params)) { if (strlen(\json_encode($params, JSON_UNESCAPED_UNICODE)) > 5000) { $params = []; } } $timeTaken = intval(microtime(true) * 1000 - $requestStartAt); $this->storeBy([ 'module' => $module, 'action' => $controller . '@' . $action, 'creator_id' => $user->id, 'http_method' => $request->method(), 'http_code' => $response->getStatusCode(), 'start_at' => intval($requestStartAt/1000), 'time_taken' => $timeTaken, 'ip' => $request->ip(), 'params' => \json_encode($params, JSON_UNESCAPED_UNICODE), 'created_at' => time() ]); } /** * * @return Attribute */ protected function timeTaken(): Attribute { return Attribute::make( get: fn ($value) => $value > 1000 ? intval($value/1000) . 's' : $value . 'ms', ); } }