AdminAuthServices.php 4.6 KB

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