ServiceTokenMiddleware.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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\model\store\service\StoreService;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  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 ServiceTokenMiddleware 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. $repository = app()->make(StoreServiceRepository::class);
  40. $service = new JwtTokenService();
  41. try {
  42. $payload = $service->parseToken($token);
  43. } catch (ExpiredException $e) {
  44. $repository->checkToken($token);
  45. $payload = $service->decode($token);
  46. } catch (Throwable $e) {//Token 过期
  47. throw new AuthException('token 已过期');
  48. }
  49. if ('service' != $payload->jti[1])
  50. throw new AuthException('无效的 token');
  51. $admin = $repository->get($payload->jti[0]);
  52. if (!$admin)
  53. throw new AuthException('账号不存在');
  54. if (!$admin['is_open'])
  55. throw new AuthException('账号未开启');
  56. if (!$admin['status'])
  57. throw new AuthException('账号已被禁用');
  58. if($admin->mer_id){
  59. if (!$admin->merchant)
  60. throw new AuthException('商户不存在');
  61. if (!$admin->merchant['status'])
  62. throw new ValidateException('商户已被锁定');
  63. }
  64. } catch (Throwable $e) {
  65. if ($force)
  66. throw $e;
  67. $request->macro('isLogin', function () {
  68. return false;
  69. });
  70. $request->macros(['tokenInfo', 'adminId', 'adminInfo', 'token'], function () {
  71. throw new AuthException('请登录');
  72. });
  73. return;
  74. }
  75. $repository->updateToken($token);
  76. $request->macro('isLogin', function () {
  77. return true;
  78. });
  79. $request->macro('tokenInfo', function () use (&$payload) {
  80. return $payload;
  81. });
  82. $request->macro('token', function () use (&$token) {
  83. return $token;
  84. });
  85. $request->macro('adminId', function () use (&$admin) {
  86. return $admin->service_id;
  87. });
  88. $request->macro('merchantId', function () use (&$admin) {
  89. return $admin->mer_id;
  90. });
  91. $request->macro('adminInfo', function () use (&$admin) {
  92. return $admin;
  93. });
  94. }
  95. public function after(Response $response)
  96. {
  97. // TODO: Implement after() method.
  98. }
  99. }