123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace app\services\system\admin;
- use app\dao\system\admin\AdminAuthDao;
- use app\services\BaseServices;
- use app\services\other\CacheServices;
- use crmeb\exceptions\AuthException;
- use crmeb\services\CacheService;
- use crmeb\utils\ApiErrorCode;
- use crmeb\utils\JwtAuth;
- use Firebase\JWT\ExpiredException;
- class AdminAuthServices extends BaseServices
- {
-
- public function __construct(AdminAuthDao $dao)
- {
- $this->dao = $dao;
- }
-
- public function parseToken(string $token): array
- {
-
- $cacheService = app()->make(CacheService::class);
- if (!$token || $token === 'undefined') {
- throw new AuthException(ApiErrorCode::ERR_LOGIN);
- }
-
- $jwtAuth = app()->make(JwtAuth::class);
-
- [$id, $type, $auth] = $jwtAuth->parseToken($token);
-
- $md5Token = md5($token);
- if (!$cacheService->hasToken($md5Token) || !($cacheToken = $cacheService->getTokenBucket($md5Token))) {
- $this->authFailAfter($id, $type);
- throw new AuthException(ApiErrorCode::ERR_LOGIN);
- }
-
- if (isset($cacheToken['invalidNum']) && $cacheToken['invalidNum'] >= 3) {
- if (!request()->isCli()) {
- $cacheService->clearToken($md5Token);
- }
- $this->authFailAfter($id, $type);
- throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
- }
-
- try {
- $jwtAuth->verifyToken();
- $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
- } catch (ExpiredException $e) {
- $cacheToken['invalidNum'] = isset($cacheToken['invalidNum']) ? $cacheToken['invalidNum']++ : 1;
- $cacheService->setTokenBucket($md5Token, $cacheToken, $cacheToken['exp']);
- } catch (\Throwable $e) {
- if (!request()->isCli()) {
- $cacheService->clearToken($md5Token);
- }
- $this->authFailAfter($id, $type);
- throw new AuthException(ApiErrorCode::ERR_LOGIN_INVALID);
- }
-
- $adminInfo = $this->dao->get($id);
- if (!$adminInfo || !$adminInfo->id) {
- if (!request()->isCli()) {
- $cacheService->clearToken($md5Token);
- }
- $this->authFailAfter($id, $type);
- throw new AuthException(ApiErrorCode::ERR_LOGIN_STATUS);
- }
-
- if ($auth !== md5($adminInfo['pwd'])) {
- throw new AuthException(ApiErrorCode::ERR_LOGIN_STATUS);
- }
- $adminInfo->type = $type;
- return $adminInfo->hidden(['pwd', 'is_del', 'status'])->toArray();
- }
-
- protected function authFailAfter($id, $type)
- {
- try {
- $postData = request()->post();
- $rule = trim(strtolower(request()->rule()->getRule()));
- $method = trim(strtolower(request()->method()));
-
- if ($rule === 'product/product/<id>' && $method === 'post') {
- $this->saveProduct($id, $postData);
- }
- } catch (\Throwable $e) {
- }
- }
-
- protected function saveProduct($adminId, $postData)
- {
-
- $cacheService = app()->make(CacheServices::class);
- $cacheService->setDbCache($adminId . '_product_data', $postData, 68400);
- }
- }
|