AdminAuthServices.php 4.4 KB

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