UserAuthServices.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. declare (strict_types=1);
  12. namespace app\services\user;
  13. use Psr\SimpleCache\InvalidArgumentException;
  14. use qiniu\basic\BaseServices;
  15. use app\model\user\User;
  16. use qiniu\exceptions\AuthException;
  17. use qiniu\services\CacheService;
  18. use qiniu\utils\JwtAuth;
  19. /**
  20. *
  21. * Class UserAuthServices
  22. * @package app\services\user
  23. * @mixin User
  24. */
  25. class UserAuthServices extends BaseServices
  26. {
  27. /**
  28. * UserAuthServices constructor.
  29. * @param User $dao
  30. */
  31. public function __construct(User $model)
  32. {
  33. $this->model = $model;
  34. }
  35. /**
  36. * 获取授权信息
  37. * @param $token
  38. * @return array
  39. * @throws InvalidArgumentException
  40. */
  41. public function parseToken($token): array
  42. {
  43. $md5Token = is_null($token) ? '' : md5($token);
  44. if ($token === 'undefined') {
  45. throw new AuthException('请登录', 410000);
  46. }
  47. if (!$token || !$tokenData = CacheService::getTokenBucket($md5Token))
  48. throw new AuthException('请登录', 410000);
  49. if (!is_array($tokenData) || empty($tokenData) || !isset($tokenData['uid'])) {
  50. throw new AuthException('请登录', 410000);
  51. }
  52. /** @var JwtAuth $jwtAuth */
  53. $jwtAuth = app()->make(JwtAuth::class);
  54. //设置解析token
  55. [$id, $type, $auth] = $jwtAuth->parseToken($token);
  56. try {
  57. $jwtAuth->verifyToken();
  58. } catch (\Throwable $e) {
  59. if (!request()->isCli()) CacheService::clearToken($md5Token);
  60. throw new AuthException('登录已过期,请重新登录', 410001);
  61. }
  62. $user = $this->getUserCacheInfo($id);
  63. if (!$user) throw new AuthException('登录失效,请重新登陆', 410001);
  64. if (!$user['status'])
  65. throw new AuthException('您已被禁止登录,请联系管理员', 410002);
  66. if ($user->uid != $tokenData['uid']) {
  67. if (!request()->isCli()) CacheService::clearToken($md5Token);
  68. throw new AuthException('登录状态有误,请重新登录', 410002);
  69. }
  70. //有密码在检测
  71. if ($auth !== md5($user['pwd'])) {
  72. throw new AuthException('登录已过期,请重新登录', 410001);
  73. }
  74. $tokenData['type'] = $type;
  75. return compact('user', 'tokenData');
  76. }
  77. public function getUserCacheInfo($uid, int $expire = 60)
  78. {
  79. return $this->model->cacheTag()->remember('user_info_' . $uid, function () use ($uid) {
  80. return $this->model->get($uid);
  81. }, $expire);
  82. }
  83. }