123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116 |
- <?php
- declare (strict_types=1);
- namespace app\services\user;
- use app\services\BaseServices;
- use app\dao\user\UserAuthDao;
- use app\services\work\WorkClientServices;
- use crmeb\exceptions\AuthException;
- use crmeb\services\CacheService;
- use crmeb\services\wechat\config\WorkConfig;
- use crmeb\utils\JwtAuth;
- class UserAuthServices extends BaseServices
- {
-
- public function __construct(UserAuthDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function parseToken($token): array
- {
- $md5Token = is_null($token) ? '' : md5($token);
- if ($token === 'undefined') {
- throw new AuthException('请登录', 410000);
- }
- if (!$token || !$tokenData = CacheService::getTokenBucket($md5Token))
- throw new AuthException('请登录', 410000);
- if (!is_array($tokenData) || empty($tokenData) || !isset($tokenData['uid'])) {
- throw new AuthException('请登录', 410000);
- }
-
- $jwtAuth = app()->make(JwtAuth::class);
-
- [$id, $type, $auth] = $jwtAuth->parseToken($token);
- try {
- $jwtAuth->verifyToken();
- } catch (\Throwable $e) {
- if (!request()->isCli()) CacheService::clearToken($md5Token);
- throw new AuthException('登录已过期,请重新登录', 410001);
- }
-
- $userService = app()->make(UserServices::class);
- $user = $userService->getUserCacheInfo($id);
- if (!$user) throw new AuthException('用户不存在,请重新登陆', 410001);
- if (!$user['status'])
- throw new AuthException('您已被禁止登录,请联系管理员', 410002);
- if (!$user || $user->uid != $tokenData['uid']) {
- if (!request()->isCli()) CacheService::clearToken($md5Token);
- throw new AuthException('登录状态有误,请重新登录', 410002);
- }
-
- if ($user['pwd'] != md5('123456') && $auth !== md5($user['pwd'])) {
- throw new AuthException('登录已过期,请重新登录', 410001);
- }
- $tokenData['type'] = $type;
- return compact('user', 'tokenData');
- }
-
- public function parseClient(string $userid)
- {
-
- $workConfig = app()->make(WorkConfig::class);
- $corpId = $workConfig->get('corpId');
- if (!$corpId) {
- throw new AuthException('请先配置企业微信');
- }
-
- $service = app()->make(WorkClientServices::class);
- $clientInfo = $service->get(['corp_id' => $corpId, 'external_userid' => $userid]);
- if (!$clientInfo) {
- throw new AuthException('客户信息不存在');
- }
- return $clientInfo->toArray();
- }
- }
|