AdminTokenMiddleware.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\system\admin\AdminRepository;
  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\facade\Route;
  19. use think\Response;
  20. use Throwable;
  21. class AdminTokenMiddleware extends BaseMiddleware
  22. {
  23. /**
  24. * @param Request $request
  25. * @throws Throwable
  26. * @author xaboy
  27. * @day 2020-04-10
  28. */
  29. public function before(Request $request)
  30. {
  31. $force = $this->getArg(0, true);
  32. try {
  33. $token = trim($request->header('X-Token'));
  34. if(!$token) $token = trim($request->param('token',''));
  35. if (strpos($token, 'Bearer') === 0)
  36. $token = trim(substr($token, 6));
  37. if (!$token)
  38. throw new ValidateException('请登录');
  39. /**
  40. * @var AdminRepository $repository
  41. */
  42. $repository = app()->make(AdminRepository::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 ('admin' != $payload->jti[1])
  53. throw new AuthException('无效的 token');
  54. $admin = $repository->get($payload->jti[0]);
  55. if (!$admin)
  56. throw new AuthException('账号不存在');
  57. if (!$admin['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', 'adminId', 'adminInfo', '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('adminId', function () use (&$admin) {
  81. return $admin->admin_id;
  82. });
  83. $request->macro('adminInfo', function () use (&$admin) {
  84. return $admin;
  85. });
  86. }
  87. public function after(Response $response)
  88. {
  89. // TODO: Implement after() method.
  90. }
  91. }