ComplaintMiddleware.php 3.9 KB

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