FooMiddleware.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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 FooMiddleware 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. Context::set("originUrl", $origin);
  66. var_dump("来源地址:", $origin);
  67. $data = [
  68. 'website_url' => $origin
  69. ];
  70. $result = $this->websiteServiceClient->getWebsiteId($data);
  71. var_dump($result, '获取网站id--------------------');
  72. if (!isset($result['data']['id']) || !$result['data']['id']) {
  73. return $this->response->json(
  74. [
  75. 'code' => -1,
  76. 'data' => [],
  77. 'message' => '网站不存在...',
  78. ]
  79. );
  80. }
  81. Context::set("SiteId", $result['data']['id']);
  82. // Context::set("websiteUrl", json_decode($result['data']['website_url'], true)[0] ?? "http://nwpre.bjzxtw.org.cn");
  83. if (in_array($ver['type_id'], [1, 2, 3, 4])) {
  84. $userInfo = $this->userController->getUserInfo($ver['uid']);
  85. if ($userInfo['code'] == 200 && isset($userInfo['data']) && !empty($userInfo['data']['sszq'])) {
  86. $sszq = $userInfo['data']['sszq'];
  87. //组id
  88. $authData = [
  89. 'id' => $sszq,
  90. 'SiteId' => $result['data']['id']
  91. ];
  92. // 调用 LoginController 中的 checkUserAuth 方法
  93. $resultAuth = $this->loginController->checkUserAuth($authData);
  94. if (!$resultAuth) {
  95. // 如果没有权限,返回错误响应
  96. return $this->response->json(
  97. [
  98. 'code' => -1,
  99. 'data' => [],
  100. 'message' => '没有权限登陆此网站',
  101. ]
  102. );
  103. }
  104. } else {
  105. return $this->response->json(
  106. [
  107. 'code' => -1,
  108. 'data' => [],
  109. 'message' => '用户没有群组',
  110. ]
  111. );
  112. }
  113. }
  114. }
  115. var_dump("中间件:", $ver);
  116. Context::set("UserId", $ver['uid']);
  117. Context::set("TypeId", $ver['type_id']);
  118. if ($ver) {
  119. return $handler->handle($request);
  120. }
  121. } else {
  122. return $this->response->json(
  123. [
  124. 'code' => -1,
  125. 'data' => [],
  126. 'message' => 'token无效,请重新登录',
  127. ]
  128. );
  129. }
  130. } catch (\Exception $e) {
  131. var_dump("错误消息1:", $e->getMessage(), $e->getCode());
  132. return $this->response->json(
  133. [
  134. 'code' => $e->getCode(),
  135. 'data' => [],
  136. 'message' => $e->getMessage(),
  137. ]
  138. );
  139. }
  140. return false;
  141. }
  142. }