ApiSign.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Middleware;
  4. use App\Utils\AppResult;
  5. use App\Utils\Control\AuthUser;
  6. use App\Utils\LogUtil;
  7. use Hyperf\Coroutine\Coroutine;
  8. use Hyperf\HttpServer\Contract\RequestInterface;
  9. use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse;
  10. use Psr\Container\ContainerInterface;
  11. use Psr\Http\Message\ResponseInterface;
  12. use Psr\Http\Message\ServerRequestInterface;
  13. use Psr\Http\Server\MiddlewareInterface;
  14. use Psr\Http\Server\RequestHandlerInterface;
  15. class ApiSign implements MiddlewareInterface
  16. {
  17. /**
  18. * @var ContainerInterface
  19. */
  20. protected $container;
  21. /**
  22. * @var RequestInterface
  23. */
  24. protected $request;
  25. /**
  26. * @var HttpResponse
  27. */
  28. protected $response;
  29. public function __construct(ContainerInterface $container, HttpResponse $response, RequestInterface $request)
  30. {
  31. $this->container = $container;
  32. $this->response = $response;
  33. $this->request = $request;
  34. }
  35. public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
  36. {
  37. //验证登录
  38. if (!AuthUser::getInstance()->check()) {
  39. return $this->response401();
  40. }
  41. // 根据具体业务判断逻辑走向
  42. return $handler->handle($request);
  43. }
  44. /**
  45. * @param string $message
  46. * @param $result
  47. * @return ResponseInterface
  48. */
  49. private function response401(string $message = '未登录,令牌失效,请稍后重试!', $result = null): ResponseInterface
  50. {
  51. return AppResult::response401($message, $result);
  52. }
  53. }