UserTokenMiddleware.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\middleware;
  12. use app\common\repositories\user\UserRepository;
  13. use app\Request;
  14. use crmeb\exceptions\AuthException;
  15. use crmeb\services\JwtTokenService;
  16. use Firebase\JWT\ExpiredException;
  17. use think\exception\ValidateException;
  18. use think\Response;
  19. use Throwable;
  20. class UserTokenMiddleware extends BaseMiddleware
  21. {
  22. /**
  23. * @param Request $request
  24. * @throws Throwable
  25. * @author xaboy
  26. * @day 2020-04-10
  27. */
  28. public function before(Request $request)
  29. {
  30. $force = $this->getArg(0, true);
  31. try {
  32. $token = trim($request->header('X-Token'));
  33. // $longitude = explode(',', $lonlat)[0] ?? 0;
  34. // $latitude = explode(',', $lonlat)[1] ?? 0;
  35. if (strpos($token, 'Bearer') === 0)
  36. $token = trim(substr($token, 6));
  37. if (!$token)
  38. throw new ValidateException('请登录');
  39. /**
  40. * @var UserRepository $repository
  41. */
  42. $repository = app()->make(UserRepository::class);
  43. $service = new JwtTokenService();
  44. try {
  45. $payload = $service->parseToken($token);
  46. } catch (ExpiredException $e) {
  47. $repository->checkToken($token);
  48. $payload = $service->decode($token);
  49. } catch (Throwable $e) {//Token 过期
  50. throw new AuthException('token 已过期');
  51. }
  52. if ('user' != $payload->jti[1])
  53. throw new AuthException('无效的 token');
  54. $user = $repository->get($payload->jti[0]);
  55. if (!$user)
  56. throw new AuthException('账号不存在');
  57. if (!$user['status'])
  58. throw new AuthException('账号已被禁用');
  59. } catch (Throwable $e) {
  60. if ($force)
  61. throw $e;
  62. $request->macro('isLogin', function () {
  63. return false;
  64. });
  65. $request->macros(['tokenInfo', 'uid', 'userInfo', 'token'], function () {
  66. throw new AuthException('请登录');
  67. });
  68. return;
  69. }
  70. $repository->updateToken($token);
  71. $request->macro('isLogin', function () {
  72. return true;
  73. });
  74. $request->macro('tokenInfo', function () use (&$payload) {
  75. return $payload;
  76. });
  77. $request->macro('token', function () use (&$token) {
  78. return $token;
  79. });
  80. $request->macro('uid', function () use (&$user) {
  81. return $user->uid;
  82. });
  83. $request->macro('userInfo', function () use (&$user) {
  84. return $user;
  85. });
  86. }
  87. public function after(Response $response)
  88. {
  89. // TODO: Implement after() method.
  90. }
  91. }