mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 10:20:16 +08:00
58 lines
1.7 KiB
PHP
58 lines
1.7 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\Repository\AdminUserRepository;
|
|
use App\Service\Admin\BaseAdminService;
|
|
use App\Service\BaseTokenService;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
class LoginService extends BaseAdminService
|
|
{
|
|
/**
|
|
* @var AdminUserRepository
|
|
*/
|
|
#[Inject]
|
|
protected AdminUserRepository $userRepository;
|
|
|
|
/**
|
|
* @var BaseTokenService
|
|
*/
|
|
#[Inject]
|
|
protected BaseTokenService $tokenService;
|
|
|
|
/**
|
|
* @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->tokenService->setJwt('admin')->getJwt();
|
|
|
|
return $this->adminReturn->success('success',[
|
|
'access_token' => $jwtHandle->builderAccessToken((string) $adminInfo->id)->toString(),
|
|
'refresh_token' => $jwtHandle->builderRefreshToken((string) $adminInfo->id)->toString(),
|
|
'expire_at' => (int) $jwtHandle->getConfig('ttl', 0),
|
|
]);
|
|
}
|
|
} |