feat: jwt

This commit is contained in:
2024-10-27 19:34:20 +08:00
parent 10037e11fd
commit a60c6ea29e
13 changed files with 697 additions and 8 deletions

View File

@@ -10,6 +10,7 @@ declare(strict_types=1);
namespace App\Service\Admin\User;
use App\Cache\Redis\Admin\UserCache;
use App\Constants\Admin\UserCode;
use App\Constants\AdminCode;
use App\Exception\AdminException;
@@ -19,6 +20,9 @@ use App\Model\AdminUser;
use App\Service\Admin\BaseService;
use Exception;
use Hyperf\Di\Annotation\Inject;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use function Hyperf\Config\config;
class LoginService extends BaseService
{
@@ -37,10 +41,19 @@ class LoginService extends BaseService
#[Inject]
protected CryptoFactory $cryptoFactory;
/**
* 注入用户缓存
* @var UserCache $userCache
*/
#[Inject]
protected UserCache $userCache;
/**
* 后台登录
* @return array
* @throws Exception
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws \RedisException
*/
public function handle(): array
{
@@ -60,11 +73,14 @@ class LoginService extends BaseService
if (!$userInfo->save()) throw new AdminException('登录失败');
//生成 token
$token = $this->cryptoFactory->cryptoClass('jwt',json_encode([
$token = $this->cryptoFactory->cryptoClass('admin-jwt',json_encode([
'id' => $userInfo->id,
'role' => $userInfo->role_id,
]))->encrypt();
//单点登录
$this->userCache->setAdminToken($userInfo->id, $token, (int)config('system.admin_jwt_expire'));
return $this->return->success('success',[
'token' => $token,
'info' => [