mirror of
https://gitee.com/ctexthuang/hyperf-micro-svc.git
synced 2026-02-08 02:10:17 +08:00
feat : login first
This commit is contained in:
10
app/Common/Interface/LoginInterface.php
Normal file
10
app/Common/Interface/LoginInterface.php
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace App\Common\Interface;
|
||||
|
||||
interface LoginInterface
|
||||
{
|
||||
public function authenticate(string $username, string $password): array;
|
||||
|
||||
public function supports(string $type): bool;
|
||||
}
|
||||
24
app/Controller/Api/LoginController.php
Normal file
24
app/Controller/Api/LoginController.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Api;
|
||||
|
||||
use App\Annotation\ResponseFormat;
|
||||
use App\Controller\AbstractController;
|
||||
use Hyperf\HttpServer\Annotation\Controller;
|
||||
use Hyperf\HttpServer\Annotation\RequestMapping;
|
||||
|
||||
#[Controller(prefix: "api/login")]
|
||||
#[ResponseFormat('api')]
|
||||
class LoginController extends AbstractController
|
||||
{
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
#[RequestMapping(path: "list", methods: "GET")]
|
||||
public function login()
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
16
app/Service/Api/BaseApiService.php
Normal file
16
app/Service/Api/BaseApiService.php
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api;
|
||||
|
||||
abstract class BaseApiService
|
||||
{
|
||||
abstract public function handle();
|
||||
}
|
||||
47
app/Service/Api/Login/AbstractLoginService.php
Normal file
47
app/Service/Api/Login/AbstractLoginService.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Login;
|
||||
|
||||
use App\Common\Interface\LoginInterface;
|
||||
use App\Lib\Log\Logger;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
|
||||
abstract class AbstractLoginService implements LoginInterface
|
||||
{
|
||||
/**
|
||||
* @var Logger
|
||||
*/
|
||||
#[Inject]
|
||||
protected Logger $logger;
|
||||
|
||||
/**
|
||||
* @param string $identifier
|
||||
* @return void
|
||||
*/
|
||||
protected function logAttempt(string $identifier): void
|
||||
{
|
||||
$this->logger->request()->info(
|
||||
sprintf('[%s] Login Attempt by %s',
|
||||
static::class,
|
||||
$identifier
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateCredentials(array $data): bool
|
||||
{
|
||||
return !empty($data);
|
||||
}
|
||||
}
|
||||
50
app/Service/Api/Login/LoginFactory.php
Normal file
50
app/Service/Api/Login/LoginFactory.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Login;
|
||||
|
||||
use App\Exception\ErrException;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\ContainerInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class LoginFactory
|
||||
{
|
||||
/**
|
||||
* 策略映射 对应 request type
|
||||
*/
|
||||
private const array HANDLER_MAP = [
|
||||
'password' => PasswordLoginService::class
|
||||
];
|
||||
|
||||
/**
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ContainerInterface $container
|
||||
) {}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return PasswordLoginService
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function get(string $type):
|
||||
PasswordLoginService
|
||||
{
|
||||
$handlerClass = self::HANDLER_MAP[$type] ?? null;
|
||||
|
||||
return match (true) {
|
||||
$handlerClass !== null => $this->container->get($handlerClass),
|
||||
default => throw new ErrException('类不存在')
|
||||
};
|
||||
}
|
||||
}
|
||||
43
app/Service/Api/Login/LoginService.php
Normal file
43
app/Service/Api/Login/LoginService.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Api\Login;
|
||||
|
||||
use App\Service\Api\BaseApiService;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
class LoginService extends BaseApiService
|
||||
{
|
||||
/**
|
||||
* @var LoginFactory
|
||||
*/
|
||||
#[Inject]
|
||||
protected LoginFactory $loginFactory;
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function handle(): array
|
||||
{
|
||||
try {
|
||||
$type = '1';
|
||||
$username= '1';
|
||||
$password = '1';
|
||||
$res = $this->loginFactory->get($type)->authenticate($username, $password);
|
||||
return ['success'];
|
||||
} catch (\Exception $e) {
|
||||
return ['error'];
|
||||
}
|
||||
}
|
||||
}
|
||||
46
app/Service/Api/Login/PasswordLoginService.php
Normal file
46
app/Service/Api/Login/PasswordLoginService.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\Api\Login;
|
||||
|
||||
use App\Common\Interface\LoginInterface;
|
||||
use App\Exception\ErrException;
|
||||
|
||||
class PasswordLoginService extends AbstractLoginService implements LoginInterface
|
||||
{
|
||||
/**
|
||||
* @param string $username
|
||||
* @param string $password
|
||||
* @return string[]
|
||||
*/
|
||||
public function authenticate(string $username, string $password): array
|
||||
{
|
||||
if (!$this->validateCredentials([
|
||||
'username' => $username,
|
||||
'password' => $password,
|
||||
])) throw new ErrException('error');
|
||||
|
||||
$this->logAttempt($username);
|
||||
|
||||
//
|
||||
return [
|
||||
'code' => 'success'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @return bool
|
||||
*/
|
||||
public function supports(string $type): bool
|
||||
{
|
||||
return $type === 'password';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user