hyperf-view/app/System/Controller/LoginController.php

94 lines
2.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\System\Controller;
use App\System\Request\SystemUserRequest;
use App\System\Service\SystemUserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;
use Builder\Annotation\Auth;
use Builder\Helper\LoginUser;
use Builder\Interfaces\UserServiceInterface;
use Builder\MineController;
use Builder\Vo\UserServiceVo;
/**
* Class LoginController
* @package App\System\Controller
*/
#[Controller(prefix: "system")]
class LoginController extends MineController
{
#[Inject]
protected SystemUserService $systemUserService;
#[Inject]
protected UserServiceInterface $userService;
public function index(){
}
/**
* @param SystemUserRequest $request
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
#[PostMapping("login")]
public function login(SystemUserRequest $request): ResponseInterface
{
$requestData = $request->validated();
$vo = new UserServiceVo();
$vo->setUsername($requestData['username']);
$vo->setPassword($requestData['password']);
return $this->success(['token' => $this->userService->login($vo)]);
}
/**
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
#[PostMapping("logout"), Auth]
public function logout(): ResponseInterface
{
$this->userService->logout();
return $this->success();
}
/**
* 用户信息
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
*/
#[GetMapping("getInfo"), Auth]
public function getInfo(): ResponseInterface
{
return $this->success($this->systemUserService->getInfo());
}
/**
* 刷新token
* @param LoginUser $user
* @return ResponseInterface
* @throws \Psr\Container\ContainerExceptionInterface
* @throws \Psr\Container\NotFoundExceptionInterface
* @throws \Psr\SimpleCache\InvalidArgumentException
*/
#[PostMapping("refresh")]
public function refresh(LoginUser $user): ResponseInterface
{
return $this->success(['token' => $user->refresh()]);
}
}