container = $container; $this->response = $response; $this->request = $request; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $params = $this->request->all(); $appId = $params['app_id'] ?? 0; $requestLog = new RequestLog(); $requestLog->app_id = $appId; $requestLog->request_id = $this->generateOrderNo(); $requestLog->request_uri = $this->request->getRequestUri(); $requestLog->request_data = json_encode($params, JSON_UNESCAPED_UNICODE); $requestLog->request_time = date('Y-m-d H:i:s'); $requestLog->save(); $ctxResponse = Context::get(ResponseInterface::class); $ctxResponse = $ctxResponse->withHeader('X-Request-Id', $requestLog->request_id); Context::set(ResponseInterface::class, $ctxResponse); Context::set('requestLog', $requestLog); $response = $handler->handle($request); $requestLog->response_data = (string)$response->getBody(); $requestLog->response_time = date('Y-m-d H:i:s'); return $response; } private function generateOrderNo() { $now = time(); $key = RedisKey::getGenerateRequestIdKey($now); $incrId = Redis::incr($key); $incrId = '' . $incrId; Redis::expire($key, 5*60); $padLength = 8 - strlen($incrId); $incrId = str_pad($incrId, $padLength, '0', STR_PAD_LEFT); return date('YmdHis', $now) . $incrId; } }