FooMiddleware.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Middleware\Auth;
  4. use Hyperf\Di\Annotation\Inject;
  5. use Hyperf\HttpServer\Contract\RequestInterface;
  6. use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
  7. use Psr\Container\ContainerInterface;
  8. use Psr\Http\Message\ResponseInterface;
  9. use Psr\Http\Message\ServerRequestInterface;
  10. use Psr\Http\Server\MiddlewareInterface;
  11. use Psr\Http\Server\RequestHandlerInterface;
  12. use Hyperf\Context\Context;
  13. use App\JsonRpc\WebsiteServiceInterface;
  14. use Phper666\JWTAuth\JWT;
  15. class FooMiddleware implements MiddlewareInterface
  16. {
  17. protected ContainerInterface $container;
  18. protected RequestInterface $request;
  19. protected HttpResponse $response;
  20. protected JWT $JWT;
  21. /**
  22. * @var WebsiteServiceInterface
  23. */
  24. #[Inject]
  25. private $websiteServiceClient;
  26. public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request,Jwt $JWT)
  27. {
  28. $this->container = $container;
  29. $this->response = $response;
  30. $this->request = $request;
  31. $this->JWT = $JWT;
  32. }
  33. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  34. {
  35. $header = $request->getHeaders();
  36. try {
  37. if($header && isset($header['token']) && $header['token'][0]!='null' && $header['token'][0]!='' && isset($header['token'][0])){
  38. $ver = $this->JWT->getClaimsByToken($header['token'][0]);
  39. $tokenTime = $this->JWT->getTokenDynamicCacheTime($header['token'][0]);
  40. if($tokenTime==0){
  41. $response = $this->response->withStatus(401)
  42. ->withHeader('Content-Type', 'application/json')
  43. ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
  44. 'code' => -1,
  45. 'data' => [],
  46. 'message' => 'token无效,请重新登录'
  47. ])));
  48. return $response;
  49. }
  50. if($header['userurl'] && $header['userurl'][0]){
  51. $origin = $header['userurl'][0];
  52. $data = [
  53. 'website_url'=>$origin
  54. ];
  55. $result = $this->websiteServiceClient->getWebsiteId($data);
  56. if(!isset($result['data']['id']) || !$result['data']['id']){
  57. $response = $this->response->withStatus(404)
  58. ->withHeader('Content-Type', 'application/json')
  59. ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
  60. 'code' => -1,
  61. 'data' => [],
  62. 'message' => '网站不存在...'
  63. ])));
  64. return $response;
  65. }
  66. Context::set("SiteId",$result['data']['id']);
  67. }
  68. Context::set("UserId",$ver['uid']);
  69. Context::set("TypeId",$ver['type_id']);
  70. if ($ver) {
  71. return $handler->handle($request);
  72. }
  73. }
  74. $response = $this->response->withStatus(401)
  75. ->withHeader('Content-Type', 'application/json')
  76. ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
  77. 'code' => -1,
  78. 'data' => [],
  79. 'message' => 'token无效,请重新登录'
  80. ])));
  81. return $response;
  82. } catch (\Exception $e) {
  83. $response = $this->response->withStatus(500)
  84. ->withHeader('Content-Type', 'application/json')
  85. ->withBody(new \Hyperf\HttpMessage\Stream\SwooleStream(json_encode([
  86. 'code' => $e->getCode(),
  87. 'data' => [],
  88. 'message' => $e->getMessage()
  89. ])));
  90. return $response;
  91. }
  92. }
  93. }