Files
mini_tp/app/api/controller/v1/Auth.php
T
nepiedg e0733cf672 refactor: restructure API authentication system and remove legacy files
- Updated API routes to use a unified versioning scheme under `/api/v1/auth`.
- Implemented new authentication controller for login, registration, and token management.
- Removed legacy user and index controllers, along with associated models and validation files.
- Updated documentation to reflect new API endpoints and usage.
- Cleaned up unused service and middleware files to streamline the application structure.
2026-04-02 03:05:44 +00:00

180 lines
5.0 KiB
PHP

<?php
declare(strict_types=1);
namespace app\api\controller\v1;
use app\api\common\Response;
use app\api\controller\BaseController;
use app\api\service\AuthService;
use think\App;
use think\exception\ValidateException;
/**
* v1 认证控制器
*/
class Auth extends BaseController
{
protected AuthService $authService;
public function __construct(App $app)
{
parent::__construct($app);
$this->authService = new AuthService();
}
/**
* 用户登录
* POST /api/v1/auth/login
*/
public function login()
{
try {
$data = $this->request->post();
validate([
'username' => 'require',
'password' => 'require',
], [
'username.require' => '用户名不能为空',
'password.require' => '密码不能为空',
])->check($data);
$result = $this->authService->login(
$data['username'],
$data['password']
);
return Response::success($result, '登录成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 用户注册
* POST /api/v1/auth/register
*/
public function register()
{
try {
$data = $this->request->post();
validate([
'username' => 'require|length:3,20|alphaNum',
'password' => 'require|length:6,20',
'email' => 'email',
], [
'username.require' => '用户名不能为空',
'username.length' => '用户名长度3-20位',
'username.alphaNum' => '用户名只能包含字母和数字',
'password.require' => '密码不能为空',
'password.length' => '密码长度6-20位',
'email.email' => '邮箱格式不正确',
])->check($data);
$result = $this->authService->register(
$data['username'],
$data['password'],
$data['email'] ?? null,
$data['formtypeid'] ?? null
);
return Response::success($result, '注册成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 刷新 Token
* POST /api/v1/auth/refresh
*/
public function refresh()
{
try {
$data = $this->request->post();
if (empty($data['refresh_token'])) {
return Response::error('刷新令牌不能为空', 400);
}
$result = $this->authService->refreshToken($data['refresh_token']);
return Response::success($result, '刷新成功');
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 获取当前用户信息
* GET /api/v1/auth/me
*/
public function me()
{
try {
$payload = $this->request->payload ?? null;
if (!$payload) {
return Response::error('未登录', 401);
}
$result = $this->authService->getUserInfo($payload['userid']);
return Response::success($result);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
/**
* 退出登录
* POST /api/v1/auth/logout
*/
public function logout()
{
return Response::success([], '退出成功');
}
/**
* 修改密码
* POST /api/v1/auth/password
*/
public function password()
{
try {
$payload = $this->request->payload ?? null;
if (!$payload) {
return Response::error('未登录', 401);
}
$data = $this->request->post();
validate([
'old_password' => 'require',
'new_password' => 'require|length:6,20|confirm:confirm_password',
], [
'old_password.require' => '原密码不能为空',
'new_password.require' => '新密码不能为空',
'new_password.length' => '新密码长度6-20位',
'new_password.confirm' => '两次密码输入不一致',
])->check($data);
$this->authService->changePassword(
$payload['userid'],
$data['old_password'],
$data['new_password']
);
return Response::success([], '密码修改成功');
} catch (ValidateException $e) {
return Response::error($e->getMessage(), 400);
} catch (\Exception $e) {
return Response::error($e->getMessage(), $e->getCode() ?: 500);
}
}
}