AdminAuthServices.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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\services\system\admin;
  12. use app\dao\system\admin\AdminAuthDao;
  13. use app\services\BaseServices;
  14. use app\services\other\CacheServices;
  15. use crmeb\exceptions\AuthException;
  16. use crmeb\services\CacheService;
  17. use crmeb\utils\ApiErrorCode;
  18. use crmeb\utils\JwtAuth;
  19. use Firebase\JWT\ExpiredException;
  20. /**
  21. * admin授权service
  22. * Class AdminAuthServices
  23. * @package app\services\system\admin
  24. */
  25. class AdminAuthServices extends BaseServices
  26. {
  27. /**
  28. * 构造方法
  29. * AdminAuthServices constructor.
  30. * @param AdminAuthDao $dao
  31. */
  32. public function __construct(AdminAuthDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取Admin授权信息
  38. * @param string $token
  39. * @return array
  40. * @throws \Psr\SimpleCache\InvalidArgumentException
  41. */
  42. public function parseToken(string $token): array
  43. {
  44. /** @var CacheService $cacheService */
  45. $cacheService = app()->make(CacheService::class);
  46. if (!$token || $token === 'undefined') {
  47. throw new AuthException(ApiErrorCode::ERR_LOGIN);
  48. }
  49. /** @var JwtAuth $jwtAuth */
  50. $jwtAuth = app()->make(JwtAuth::class);
  51. //设置解析token
  52. [$id, $type] = $jwtAuth->parseToken($token);
  53. //检测token是否过期
  54. $md5Token = md5($token);
  55. if (!$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
  56. $this->authFailAfter($id, $type);
  57. throw new AuthException(ApiErrorCode::ERR_LOGIN);
  58. }
  59. //是否超出有效次数
  60. if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
  61. if (!request()->isCli()) {
  62. $cacheService->clearToken($md5Token);
  63. }
  64. $this->authFailAfter($id, $type);
  65. throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
  66. }
  67. //验证token
  68. try {
  69. $jwtAuth->verifyToken();
  70. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  71. } catch (ExpiredException $e) {
  72. $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
  73. $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
  74. } catch (\Throwable $e) {
  75. if (!request()->isCli()) {
  76. $cacheService->clearToken($md5Token);
  77. }
  78. $this->authFailAfter($id, $type);
  79. throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
  80. }
  81. //获取管理员信息
  82. $adminInfo = $this->dao->get($id);
  83. if (!$adminInfo || !$adminInfo->id) {
  84. if (!request()->isCli()) {
  85. $cacheService->clearToken($md5Token);
  86. }
  87. $this->authFailAfter($id, $type);
  88. throw new AuthException(ApiErrorCode::ERR_LOGIN_STATUS);
  89. }
  90. $adminInfo->type = $type;
  91. return $adminInfo->hidden(['pwd', 'is_del', 'status'])->toArray();
  92. }
  93. /**
  94. * token验证失败后事件
  95. */
  96. protected function authFailAfter($id, $type)
  97. {
  98. try {
  99. $postData = request()->post();
  100. $rule = trim(strtolower(request()->rule()->getRule()));
  101. $method = trim(strtolower(request()->method()));
  102. //添加商品退出后事件
  103. if ($rule === 'product/product/<id>' && $method === 'post') {
  104. $this->saveProduct($id, $postData);
  105. }
  106. } catch (\Throwable $e) {
  107. }
  108. }
  109. /**
  110. * 保存提交数据
  111. * @param $adminId
  112. * @param $postData
  113. */
  114. protected function saveProduct($adminId, $postData)
  115. {
  116. /** @var CacheServices $cacheService */
  117. $cacheService = app()->make(CacheServices::class);
  118. $cacheService->setDbCache($adminId . '_product_data', $postData, 68400);
  119. }
  120. }