Files
lgp-admin-plus-api/app/Http/Controllers/Api/LoginController.php
LQ bcf54c2727 初始化
缺陷:主题配色需要优化整体的同风格
2026-08-14 23:21:21 +08:00

90 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
namespace App\Http\Controllers\Api;
use App\BaseApp\BaseController;
use App\Service\LoginService;
use Illuminate\Http\JsonResponse;
class LoginController extends BaseController
{
/**
* 登录控制器注册在免登录组里,继承来的 CRUD 会变成 /api/list、/api/create 这种
* 无需鉴权、指向 LoginService根本没有这些方法的路由必须排除
*/
protected array $exceptRoute = ['list', 'option', 'detail', 'create', 'update', 'delete'];
public function __construct()
{
parent::__construct();
$this->service = LoginService::getInstance();
}
/**
* 登录
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
public function login()
{
$this->insertField = [
'account', 'password'
];
$param = $this->checkRequiredFields(request()->post());
return jok(
$this->service->login($param['account'], $param['password']),
'登录成功'
);
}
/**
* 注册
* @Method POST
* @return JsonResponse
* @throws \Exception
*/
public function register()
{
$this->insertField = [
'phone', 'password', 'email', 'code'
];
$param = $this->checkRequiredFields(request()->post());
return jok(
$this->service->register($param['phone'], $param['password'], $param['email'], $param['code']),
'登录成功'
);
}
/**
* 退出登录
* @Method POST
* @throws \Exception
*/
public function logout(): JsonResponse
{
return jok(
$this->service->logout(),
'退出成功'
);
}
/**
* 续签 token
*
* 前端拦截器在 401 时调用,此时 token 已过期,所以只能放在免登录组里,
* 由 LoginService::refresh 自己校验签名与 Redis 会话。
* @Method POST
* @throws \Exception
*/
public function refresh(): JsonResponse
{
return jok(
$this->service->refresh(),
'续签成功'
);
}
}