feat : config

This commit is contained in:
2025-03-20 15:27:53 +08:00
parent ef9f663fb2
commit d3d0cda616
9 changed files with 164 additions and 3 deletions

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Cache\Redis\Api;
use App\Cache\Redis\Common\CommonRedisKey;
use App\Cache\Redis\RedisCache;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class UserCache
{
/**
* @var RedisCache
*/
#[Inject]
protected RedisCache $redis;
/**
* @param int $userId
* @param string $token
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function setUserToken(int $userId,string $token): void
{
$key = CommonRedisKey::userTokenHashKey();
$this->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);
}
}

View File

@@ -119,4 +119,12 @@ class CommonRedisKey
{
return '__system:yiLianYun:self:app:token';
}
/**
* @return string
*/
public static function userTokenHashKey(): string
{
return 'user:token:online';
}
}

View File

@@ -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];

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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);
}

View File

@@ -95,4 +95,12 @@ class LoginService extends BaseService
]
]);
}
/**
* @return array
*/
public function logOut(): array
{
return $this->return->success();
}
}

View File

@@ -0,0 +1,38 @@
<?php
/**
* This service file is part of item.
*
* @author ctexthuang
* @contact ctexthuang@qq.com
*/
declare(strict_types=1);
namespace App\Service\Api\Login;
use App\Cache\Redis\Api\UserCache;
use App\Service\Api\BaseService;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class LogOutService extends BaseService
{
/**
* @var UserCache
*/
#[Inject]
protected UserCache $userCache;
/**
* @return array
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function handle(): array
{
$this->userCache->removeUserToken($this->userId);
return $this->return->success();
}
}

View File

@@ -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;