From d3d0cda61675b7c82e2bba45c7de5677af148be4 Mon Sep 17 00:00:00 2001 From: ctexthuang Date: Thu, 20 Mar 2025 15:27:53 +0800 Subject: [PATCH] feat : config --- app/Cache/Redis/Api/UserCache.php | 55 ++++++++++++++++++++++ app/Cache/Redis/Common/CommonRedisKey.php | 8 ++++ app/Cache/Redis/Common/ConfigCache.php | 2 +- app/Controller/Admin/AuthController.php | 11 +++++ app/Controller/Api/UserController.php | 13 +++++ app/Middleware/Api/JwtAuthMiddleware.php | 21 ++++++++- app/Service/Admin/Login/LoginService.php | 8 ++++ app/Service/Api/Login/LogOutService.php | 38 +++++++++++++++ app/Service/Api/Login/LoginBaseService.php | 11 ++++- 9 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 app/Cache/Redis/Api/UserCache.php create mode 100644 app/Service/Api/Login/LogOutService.php diff --git a/app/Cache/Redis/Api/UserCache.php b/app/Cache/Redis/Api/UserCache.php new file mode 100644 index 0000000..cd62069 --- /dev/null +++ b/app/Cache/Redis/Api/UserCache.php @@ -0,0 +1,55 @@ +redis->hSet($key, $userId, $token); + } + + /** + * @param int $userId + * @return false|mixed|\Redis|string + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function getUserToken(int $userId): mixed + { + $key = CommonRedisKey::userTokenHashKey(); + return $this->redis->hGet($key, $userId); + } + + /** + * @param int $userId + * @return void + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + public function removeUserToken(int $userId): void + { + $key = CommonRedisKey::userTokenHashKey(); + $this->redis->hDel($key, $userId); + } +} \ No newline at end of file diff --git a/app/Cache/Redis/Common/CommonRedisKey.php b/app/Cache/Redis/Common/CommonRedisKey.php index 2af1d55..42f4e43 100644 --- a/app/Cache/Redis/Common/CommonRedisKey.php +++ b/app/Cache/Redis/Common/CommonRedisKey.php @@ -119,4 +119,12 @@ class CommonRedisKey { return '__system:yiLianYun:self:app:token'; } + + /** + * @return string + */ + public static function userTokenHashKey(): string + { + return 'user:token:online'; + } } \ No newline at end of file diff --git a/app/Cache/Redis/Common/ConfigCache.php b/app/Cache/Redis/Common/ConfigCache.php index 645e041..60be272 100644 --- a/app/Cache/Redis/Common/ConfigCache.php +++ b/app/Cache/Redis/Common/ConfigCache.php @@ -84,7 +84,7 @@ class ConfigCache }; if ($isRedis == 1) { - $this->redis->setEx($redisKey, $value, $expire,RedisCode::SYSTEM_DB); + $this->redis->setEx($redisKey, $value ?? ConfigCode::DEFAULT_VALUE[$key], $expire,RedisCode::SYSTEM_DB); } return $value ?? ConfigCode::DEFAULT_VALUE[$key]; diff --git a/app/Controller/Admin/AuthController.php b/app/Controller/Admin/AuthController.php index 97cb9f4..6a9315f 100644 --- a/app/Controller/Admin/AuthController.php +++ b/app/Controller/Admin/AuthController.php @@ -7,6 +7,7 @@ namespace App\Controller\Admin; use App\Controller\AbstractController; use App\Middleware\Admin\JwtAuthMiddleware; use App\Request\Admin\AuthRequest; +use App\Service\Admin\Login\LoginService; use App\Service\Admin\User\RoleMenuService; use App\Service\Admin\User\RoleService; use Exception; @@ -163,4 +164,14 @@ class AuthController extends AbstractController { return (new RoleService)->details(); } + + /** + * @return array + */ + #[RequestMapping(path: "logOut", methods: "GET")] + #[Scene(scene: "logOut")] + public function logOut() + { + return (new LoginService)->logOut(); + } } diff --git a/app/Controller/Api/UserController.php b/app/Controller/Api/UserController.php index 2a9e656..9d87737 100644 --- a/app/Controller/Api/UserController.php +++ b/app/Controller/Api/UserController.php @@ -7,6 +7,7 @@ namespace App\Controller\Api; use App\Controller\AbstractController; use App\Middleware\Api\JwtAuthMiddleware; use App\Request\Api\UserRequest; +use App\Service\Api\Login\LogOutService; use App\Service\Api\User\BindPhoneByWxService; use App\Service\Api\User\InviteListService; use App\Service\Api\User\SiteService; @@ -79,4 +80,16 @@ class UserController extends AbstractController { return (new InviteListService)->handle(); } + + /** + * @return array + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ + #[RequestMapping(path: 'logOut',methods: 'GET')] + #[Scene(scene: 'logOut')] + public function logOut() + { + return (new LogOutService)->handle(); + } } diff --git a/app/Middleware/Api/JwtAuthMiddleware.php b/app/Middleware/Api/JwtAuthMiddleware.php index e9feede..5521338 100644 --- a/app/Middleware/Api/JwtAuthMiddleware.php +++ b/app/Middleware/Api/JwtAuthMiddleware.php @@ -2,11 +2,14 @@ namespace App\Middleware\Api; +use App\Cache\Redis\Api\UserCache; use App\Constants\ApiCode; use App\Lib\ApiReturn; use App\Lib\Crypto\CryptoFactory; use Hyperf\Context\Context; use Hyperf\HttpServer\Contract\ResponseInterface as HttpResponse; +use Psr\Container\ContainerExceptionInterface; +use Psr\Container\NotFoundExceptionInterface; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\MiddlewareInterface; @@ -18,8 +21,16 @@ class JwtAuthMiddleware implements MiddlewareInterface protected HttpResponse $response, protected ApiReturn $apiReturn, protected CryptoFactory $cryptoFactory, + protected UserCache $userCache, ){} + /** + * @param ServerRequestInterface $request + * @param RequestHandlerInterface $handler + * @return ResponseInterface + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface + */ public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface { // 获取头部token @@ -39,7 +50,15 @@ class JwtAuthMiddleware implements MiddlewareInterface ); } - Context::set('user_id',$userJwt['data']->id); + $userId = $userJwt['data']->id; + + if ($this->userCache->getUserToken($userId) != $authorization) { + return $this->response->json( + $this->apiReturn->error(ApiCode::getMessage(ApiCode::LOGIN_TOKEN_ERROR), ApiCode::LOGIN_TOKEN_ERROR) + ); + } + + Context::set('user_id',$userId); return $handler->handle($request); } diff --git a/app/Service/Admin/Login/LoginService.php b/app/Service/Admin/Login/LoginService.php index ceb2c6f..fffd9c7 100644 --- a/app/Service/Admin/Login/LoginService.php +++ b/app/Service/Admin/Login/LoginService.php @@ -95,4 +95,12 @@ class LoginService extends BaseService ] ]); } + + /** + * @return array + */ + public function logOut(): array + { + return $this->return->success(); + } } \ No newline at end of file diff --git a/app/Service/Api/Login/LogOutService.php b/app/Service/Api/Login/LogOutService.php new file mode 100644 index 0000000..8557540 --- /dev/null +++ b/app/Service/Api/Login/LogOutService.php @@ -0,0 +1,38 @@ +userCache->removeUserToken($this->userId); + + return $this->return->success(); + } +} \ No newline at end of file diff --git a/app/Service/Api/Login/LoginBaseService.php b/app/Service/Api/Login/LoginBaseService.php index 74313e5..41e222c 100644 --- a/app/Service/Api/Login/LoginBaseService.php +++ b/app/Service/Api/Login/LoginBaseService.php @@ -11,6 +11,7 @@ declare(strict_types=1); namespace App\Service\Api\Login; use App\Cache\Redis\Api\ApiRedisKey; +use App\Cache\Redis\Api\UserCache; use App\Cache\Redis\Common\ConfigCache; use App\Cache\Redis\RedisCache; use App\Constants\Common\CouponCode; @@ -73,6 +74,12 @@ abstract class LoginBaseService extends BaseService #[Inject] protected UserInvite $userInviteModel; + /** + * @var UserCache + */ + #[Inject] + protected UserCache $userCache; + /** * 锁定注册 @@ -134,7 +141,8 @@ abstract class LoginBaseService extends BaseService /** * 返回值 * @return array - * @throws Exception + * @throws ContainerExceptionInterface + * @throws NotFoundExceptionInterface */ protected function getReturn():array { @@ -147,6 +155,7 @@ abstract class LoginBaseService extends BaseService ]; $loginReturn['token'] = $this->cryptoFactory->cryptoClass('jwt', json_encode($loginReturn))->encrypt(); + $this->userCache->setUserToken($this->userId, $loginReturn['token']); return $loginReturn;