AdminAuthServices.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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. * @param int $code
  39. * @return array
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function parseToken(string $token, int $code = 110003): array
  45. {
  46. /** @var CacheService $cacheService */
  47. $cacheService = app()->make(CacheService::class);
  48. if (!$token || $token === 'undefined') {
  49. throw new AuthException($code);
  50. }
  51. /** @var JwtAuth $jwtAuth */
  52. $jwtAuth = app()->make(JwtAuth::class);
  53. //设置解析token
  54. [$id, $type, $pwd] = $jwtAuth->parseToken($token);
  55. //检测token是否过期
  56. $md5Token = md5($token);
  57. if (!$cacheService->has($md5Token) || !$cacheService->get($md5Token, '', NULL, 'admin')) {
  58. $this->authFailAfter($id, $type);
  59. throw new AuthException($code);
  60. }
  61. //验证token
  62. try {
  63. $jwtAuth->verifyToken();
  64. } catch (\Throwable $e) {
  65. if (!request()->isCli()) {
  66. $cacheService->delete($md5Token);
  67. }
  68. $this->authFailAfter($id, $type);
  69. throw new AuthException($code);
  70. }
  71. //获取管理员信息
  72. $adminInfo = $this->dao->get($id);
  73. if (!$adminInfo || !$adminInfo->id) {
  74. if (!request()->isCli()) {
  75. $cacheService->delete($md5Token);
  76. }
  77. $this->authFailAfter($id, $type);
  78. throw new AuthException($code);
  79. }
  80. if ($pwd !== '' && $pwd !== md5($adminInfo->pwd)) {
  81. throw new AuthException($code);
  82. }
  83. $adminInfo->type = $type;
  84. return $adminInfo->hidden(['pwd', 'is_del', 'status'])->toArray();
  85. }
  86. /**
  87. * token验证失败后事件
  88. */
  89. protected function authFailAfter($id, $type)
  90. {
  91. try {
  92. $postData = request()->post();
  93. $rule = trim(strtolower(request()->rule()->getRule()));
  94. $method = trim(strtolower(request()->method()));
  95. //添加商品退出后事件
  96. if ($rule === 'product/product/<id>' && $method === 'post') {
  97. $this->saveProduct($id, $postData);
  98. }
  99. } catch (\Throwable $e) {
  100. }
  101. }
  102. /**
  103. * 保存提交数据
  104. * @param $adminId
  105. * @param $postData
  106. */
  107. protected function saveProduct($adminId, $postData)
  108. {
  109. /** @var CacheServices $cacheService */
  110. $cacheService = app()->make(CacheServices::class);
  111. $cacheService->setDbCache($adminId . '_product_data', $postData, 68400);
  112. }
  113. }