container = $container; $this->response = $response; $this->request = $request; $this->JWT = $JWT; } public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { $header = $request->getHeaders(); try { if($header && isset($header['token']) && $header['token'][0]!='null' && $header['token'][0]!='' && isset($header['token'][0])){ $ver = $this->JWT->getClaimsByToken($header['token'][0]); $tokenTime = $this->JWT->getTokenDynamicCacheTime($header['token'][0]); if($tokenTime==0){ $response = $this->response->withStatus(401) ->withHeader('Content-Type', 'application/json') ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([ 'code' => -1, 'data' => [], 'message' => 'token无效,请重新登录' ]))); return $response; } if($header['userurl'] && $header['userurl'][0]){ $origin = $header['userurl'][0]; $data = [ 'website_url'=>$origin ]; $result = $this->websiteServiceClient->getWebsiteId($data); if(!isset($result['data']['id']) || !$result['data']['id']){ $response = $this->response->withStatus(404) ->withHeader('Content-Type', 'application/json') ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([ 'code' => -1, 'data' => [], 'message' => '网站不存在...' ]))); return $response; } Context::set("SiteId",$result['data']['id']); } Context::set("UserId",$ver['uid']); Context::set("TypeId",$ver['type_id']); if ($ver) { return $handler->handle($request); } } $response = $this->response->withStatus(401) ->withHeader('Content-Type', 'application/json') ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([ 'code' => -1, 'data' => [], 'message' => 'token无效,请重新登录' ]))); return $response; } catch (\Exception $e) { $response = $this->response->withStatus(500) ->withHeader('Content-Type', 'application/json') ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([ 'code' => $e->getCode(), 'data' => [], 'message' => $e->getMessage() ]))); return $response; } } }