AdminTokenMiddleware.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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 Throwable;
  13. use app\Request;
  14. use think\Response;
  15. use Firebase\JWT\ExpiredException;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\services\JwtTokenService;
  18. use think\exception\ValidateException;
  19. use app\common\repositories\system\admin\AdminRepository;
  20. use app\common\repositories\system\merchant\MerchantRepository;
  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. $regionMerId = [];
  72. if ($admin->region_ids) {
  73. $regionMerId = app()->make(MerchantRepository::class)
  74. ->getSearch([])
  75. ->whereIn('region_id', $admin->region_ids)
  76. ->column('mer_id');
  77. }
  78. $request->macro('isLogin', function () {
  79. return true;
  80. });
  81. $request->macro('tokenInfo', function () use (&$payload) {
  82. return $payload;
  83. });
  84. $request->macro('token', function () use (&$token) {
  85. return $token;
  86. });
  87. $request->macro('adminId', function () use (&$admin) {
  88. return $admin->admin_id;
  89. });
  90. $request->macro('adminInfo', function () use (&$admin) {
  91. return $admin;
  92. });
  93. $request->macro('userType', function () {
  94. return 2;
  95. });
  96. $request->macro('regionIds', function () use($admin){
  97. return is_array($admin->region_ids) ? $admin->region_ids : [];
  98. });
  99. $request->macro('regionAuthority', function () use($admin, $regionMerId){
  100. if ($admin->region_ids) {
  101. return empty($regionMerId) ? [0] : $regionMerId;
  102. }
  103. return [];
  104. });
  105. }
  106. public function after(Response $response)
  107. {
  108. // TODO: Implement after() method.
  109. }
  110. }