MerchantTokenMiddleware.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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\merchant\MerchantAdminRepository;
  13. use app\common\repositories\system\merchant\MerchantRepository;
  14. use app\Request;
  15. use crmeb\exceptions\AuthException;
  16. use crmeb\services\JwtTokenService;
  17. use Firebase\JWT\ExpiredException;
  18. use think\exception\ValidateException;
  19. use think\Response;
  20. use Throwable;
  21. class MerchantTokenMiddleware 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 MerchantAdminRepository $repository
  41. */
  42. $repository = app()->make(MerchantAdminRepository::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 ('mer' != $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. /**
  60. * @var MerchantRepository $merchantRepository
  61. */
  62. $merchantRepository = app()->make(MerchantRepository::class);
  63. $merchant = $merchantRepository->get($admin->mer_id);
  64. if (!$merchant || !$merchant['status'])
  65. throw new AuthException('商户已被锁定');
  66. } catch (Throwable $e) {
  67. if ($force)
  68. throw $e;
  69. $request->macro('isLogin', function () {
  70. return false;
  71. });
  72. $request->macros(['tokenInfo', 'adminId', 'adminInfo', 'token', 'merchant', 'merchantId'], function () {
  73. throw new AuthException('请登录');
  74. });
  75. return;
  76. }
  77. $repository->updateToken($token);
  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->merchant_admin_id;
  89. });
  90. $request->macro('adminInfo', function () use (&$admin) {
  91. return $admin;
  92. });
  93. $request->macro('merchantId', function () use (&$admin) {
  94. return $admin->mer_id;
  95. });
  96. $request->macro('merchant', function () use (&$merchant) {
  97. return $merchant;
  98. });
  99. }
  100. public function after(Response $response)
  101. {
  102. // TODO: Implement after() method.
  103. }
  104. }