mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
72 lines
1.8 KiB
PHP
72 lines
1.8 KiB
PHP
<?php
|
|
/**
|
|
* This service file is part of item.
|
|
*
|
|
* @author ctexthuang
|
|
* @contact ctexthuang@qq.com
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Service\Admin\Login;
|
|
|
|
use App\Constants\Model\AdminUser\AdminUserStatusCode;
|
|
use App\Exception\ErrException;
|
|
use App\Interface\JwtInterface;
|
|
use App\Lib\Jwt\JwtFactory;
|
|
use App\Repository\AdminUserRepository;
|
|
use App\Service\Admin\BaseAdminService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class LoginService extends BaseAdminService
|
|
{
|
|
/**
|
|
* @var string jwt场景
|
|
*/
|
|
private string $jwt = 'admin';
|
|
|
|
/**
|
|
* @var AdminUserRepository
|
|
*/
|
|
#[Inject]
|
|
protected AdminUserRepository $userRepository;
|
|
|
|
/**
|
|
* @var JwtFactory
|
|
*/
|
|
#[Inject]
|
|
protected JwtFactory $jwtFactory;
|
|
|
|
/**
|
|
* @return array
|
|
*/
|
|
public function handle(): array
|
|
{
|
|
$adminInfo = $this->userRepository->findByUserName((string)$this->request->input('username'));
|
|
|
|
if (!$adminInfo) throw new ErrException('后台管理员不存在');
|
|
|
|
if (! $adminInfo->verifyPassword((string) $this->request->input('password'))) {
|
|
// $this->dispatcher->dispatch(new UserLoginEvent($user, $ip, $os, $browser, false));
|
|
throw new ErrException('密码错误');
|
|
}
|
|
|
|
if ($adminInfo->status == AdminUserStatusCode::DISABLE) throw new ErrException('用户已禁用');
|
|
|
|
$jwtHandle = $this->getJwt();
|
|
|
|
return [
|
|
'access_token' => $jwtHandle->builderAccessToken((string) $adminInfo->id)->toString(),
|
|
'refresh_token' => $jwtHandle->builderRefreshToken((string) $adminInfo->id)->toString(),
|
|
'expire_at' => (int) $jwtHandle->getConfig('ttl', 0),
|
|
];
|
|
}
|
|
|
|
/**
|
|
* @return JwtInterface
|
|
*/
|
|
private function getJwt(): JwtInterface
|
|
{
|
|
return $this->jwtFactory->get($this->jwt);
|
|
}
|
|
} |