Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

UserAuthServices.php 3.0KB

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