feat: admin login
This commit is contained in:
@@ -10,10 +10,27 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin;
|
||||
|
||||
use App\Lib\AdminReturn;
|
||||
use Hyperf\Context\Context;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\HttpServer\Contract\RequestInterface;
|
||||
|
||||
abstract class BaseService
|
||||
{
|
||||
/**
|
||||
* 请求对象
|
||||
* @var RequestInterface
|
||||
*/
|
||||
#[Inject]
|
||||
protected RequestInterface $request;
|
||||
|
||||
/**
|
||||
* 通用返回对象
|
||||
* @var AdminReturn $return
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminReturn $return;
|
||||
|
||||
/**
|
||||
* 管理员id
|
||||
* @var int $adminId
|
||||
|
||||
@@ -10,13 +10,68 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin\User;
|
||||
|
||||
use App\Constants\Admin\UserCode;
|
||||
use App\Constants\AdminCode;
|
||||
use App\Exception\AdminException;
|
||||
use App\Extend\SystemUtil;
|
||||
use App\Lib\AdminReturn;
|
||||
use App\Lib\Crypto\CryptoFactory;
|
||||
use App\Model\AdminUser;
|
||||
use App\Service\Admin\BaseService;
|
||||
use App\Service\Common\AppMakeService;
|
||||
use Exception;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
class LoginService extends BaseService
|
||||
{
|
||||
|
||||
/**
|
||||
* 注入管理员模型
|
||||
* @var AdminUser $adminUserModel
|
||||
*/
|
||||
#[Inject]
|
||||
protected AdminUser $adminUserModel;
|
||||
|
||||
/**
|
||||
* 注入加密工厂
|
||||
* @var CryptoFactory $cryptoFactory
|
||||
*/
|
||||
#[Inject]
|
||||
protected CryptoFactory $cryptoFactory;
|
||||
|
||||
/**
|
||||
* 后台登录
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
return AdminReturn::success();
|
||||
$userInfo = $this->adminUserModel->getAdminInfoByAccount($this->request->input('account'));
|
||||
if (!$userInfo) throw new AdminException('账号不存在');
|
||||
|
||||
if ($userInfo->status == UserCode::DISABLE) throw new AdminException(UserCode::getMessage($userInfo->status),AdminCode::LOGIN_ERROR);
|
||||
|
||||
if ($this->cryptoFactory->cryptoClass('admin-password',$this->request->input('password'),$userInfo->salt) != $userInfo->password) throw new AdminException('密码错误!');
|
||||
|
||||
$userInfo->last_login_time = date('Y-m-d H:i:s');
|
||||
$userInfo->last_login_ip = SystemUtil::getClientIp();
|
||||
$userInfo->save();
|
||||
|
||||
if (!$userInfo->save()) throw new AdminException('登录失败');
|
||||
|
||||
$token = $this->cryptoFactory->cryptoClass('jwt',json_encode([
|
||||
'id' => $userInfo->id,
|
||||
'role' => $userInfo->role_id,
|
||||
]))->encrypt();
|
||||
|
||||
return $this->return->success('success',[
|
||||
'token' => $token,
|
||||
'info' => [
|
||||
'admin_id' => $userInfo->id,
|
||||
'avatar' => $userInfo->avatar,
|
||||
'name' => $userInfo->chinese_name,
|
||||
'role_id' => $userInfo->role_id,
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
46
app/Service/Common/AppMakeService.php
Normal file
46
app/Service/Common/AppMakeService.php
Normal file
@@ -0,0 +1,46 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Common;
|
||||
|
||||
use Exception;
|
||||
use Hyperf\Context\Context;
|
||||
|
||||
class AppMakeService
|
||||
{
|
||||
/**
|
||||
* 生成类对象
|
||||
* @param $className
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function make($className): mixed
|
||||
{
|
||||
if (!class_exists($className)) throw new Exception('The instantiated class does not exist');
|
||||
|
||||
$array = Context::get('obj');
|
||||
if (!isset($array[$className])) {
|
||||
$array = $array ?? [];
|
||||
$array[$className] = new $className;
|
||||
Context::set('obj', $array);
|
||||
}
|
||||
|
||||
return $array[$className];
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象类
|
||||
* @return mixed
|
||||
*/
|
||||
public static function getObj(): mixed
|
||||
{
|
||||
return Context::get('obj');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user