UserAuthServices.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 app\services\BaseServices;
  14. use app\dao\user\UserAuthDao;
  15. use app\services\work\WorkClientServices;
  16. use crmeb\exceptions\AuthException;
  17. use crmeb\services\CacheService;
  18. use crmeb\services\wechat\config\WorkConfig;
  19. use crmeb\utils\JwtAuth;
  20. /**
  21. *
  22. * Class UserAuthServices
  23. * @package app\services\user
  24. * @mixin UserAuthDao
  25. */
  26. class UserAuthServices extends BaseServices
  27. {
  28. /**
  29. * UserAuthServices constructor.
  30. * @param UserAuthDao $dao
  31. */
  32. public function __construct(UserAuthDao $dao)
  33. {
  34. $this->dao = $dao;
  35. }
  36. /**
  37. * 获取授权信息
  38. * @param $token
  39. * @return array
  40. * @throws \Psr\SimpleCache\InvalidArgumentException
  41. */
  42. public function parseToken($token): array
  43. {
  44. $md5Token = is_null($token) ? '' : md5($token);
  45. if ($token === 'undefined') {
  46. throw new AuthException('请登录', 410000);
  47. }
  48. if (!$token || !$tokenData = CacheService::getTokenBucket($md5Token))
  49. throw new AuthException('请登录', 410000);
  50. if (!is_array($tokenData) || empty($tokenData) || !isset($tokenData['uid'])) {
  51. throw new AuthException('请登录', 410000);
  52. }
  53. /** @var JwtAuth $jwtAuth */
  54. $jwtAuth = app()->make(JwtAuth::class);
  55. //设置解析token
  56. [$id, $type, $auth] = $jwtAuth->parseToken($token);
  57. try {
  58. $jwtAuth->verifyToken();
  59. } catch (\Throwable $e) {
  60. if (!request()->isCli()) CacheService::clearToken($md5Token);
  61. throw new AuthException('登录已过期,请重新登录', 410001);
  62. }
  63. /** @var UserServices $userService */
  64. $userService = app()->make(UserServices::class);
  65. $user = $userService->getUserCacheInfo($id);
  66. if (!$user) throw new AuthException('用户不存在,请重新登陆', 410001);
  67. if (!$user['status'])
  68. throw new AuthException('您已被禁止登录,请联系管理员', 410002);
  69. if (!$user || $user->uid != $tokenData['uid']) {
  70. if (!request()->isCli()) CacheService::clearToken($md5Token);
  71. throw new AuthException('登录状态有误,请重新登录', 410002);
  72. }
  73. //有密码在检测
  74. if ($user['pwd'] != md5('123456') && $auth !== md5($user['pwd'])) {
  75. throw new AuthException('登录已过期,请重新登录', 410001);
  76. }
  77. $tokenData['type'] = $type;
  78. return compact('user', 'tokenData');
  79. }
  80. /**
  81. * 获取企业客户
  82. * @param string $userid
  83. * @return array
  84. */
  85. public function parseClient(string $userid)
  86. {
  87. /** @var WorkConfig $workConfig */
  88. $workConfig = app()->make(WorkConfig::class);
  89. $corpId = $workConfig->get('corpId');
  90. if (!$corpId) {
  91. throw new AuthException('请先配置企业微信');
  92. }
  93. /** @var WorkClientServices $service */
  94. $service = app()->make(WorkClientServices::class);
  95. $clientInfo = $service->get(['corp_id' => $corpId, 'external_userid' => $userid]);
  96. if (!$clientInfo) {
  97. throw new AuthException('客户信息不存在');
  98. }
  99. return $clientInfo->toArray();
  100. }
  101. }