mirror of
https://gitee.com/ctexthuang/hyperf_rbac_framework_server_ctexthuang.git
synced 2025-12-25 17:07:49 +08:00
first commit
This commit is contained in:
52
app/Service/Admin/BaseAdminService.php
Normal file
52
app/Service/Admin/BaseAdminService.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin;
|
||||
|
||||
use App\Lib\Return\AdminReturn;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
abstract class BaseAdminService
|
||||
{
|
||||
/**
|
||||
* 请求对象注入
|
||||
* @var RequestInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
/**
|
||||
* 返回对象注入
|
||||
* @var AdminReturn
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminReturn $adminReturn;
|
||||
|
||||
/**
|
||||
* 管理员 id
|
||||
* @var int
|
||||
*/
|
||||
protected int $adminId = 0;
|
||||
|
||||
/**
|
||||
* 主构造函数
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->adminId = Context::get('admin_id',0);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主函数抽象类
|
||||
*/
|
||||
abstract public function handle();
|
||||
}
|
||||
72
app/Service/Admin/Login/LoginService.php
Normal file
72
app/Service/Admin/Login/LoginService.php
Normal file
@@ -0,0 +1,72 @@
|
||||
<?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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user