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.
This commit is contained in:
@@ -1,37 +0,0 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
/**
|
||||
* API 示例控制器
|
||||
*/
|
||||
class Index extends BaseController
|
||||
{
|
||||
/**
|
||||
* 首页接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$data = [
|
||||
'name' => 'ThinkPHP API',
|
||||
'version' => app()->version(),
|
||||
'message' => 'Welcome to ThinkPHP API Application',
|
||||
];
|
||||
|
||||
return $this->success($data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 健康检查接口
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function health()
|
||||
{
|
||||
return $this->success([
|
||||
'status' => 'ok',
|
||||
'timestamp' => date('Y-m-d H:i:s'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -1,81 +0,0 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\api\controller;
|
||||
|
||||
/**
|
||||
* 用户控制器示例
|
||||
*/
|
||||
class User extends BaseController
|
||||
{
|
||||
/**
|
||||
* 用户登录
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证数据
|
||||
$this->validate($data, [
|
||||
'username' => 'require',
|
||||
'password' => 'require',
|
||||
], [
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
]);
|
||||
|
||||
// TODO: 实际的登录逻辑
|
||||
return $this->success([
|
||||
'token' => 'example_token_' . md5($data['username']),
|
||||
'username' => $data['username'],
|
||||
], '登录成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户信息
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function info()
|
||||
{
|
||||
// TODO: 从 token 或 session 中获取用户信息
|
||||
$userInfo = [
|
||||
'id' => 1,
|
||||
'username' => 'demo_user',
|
||||
'nickname' => '演示用户',
|
||||
'avatar' => '',
|
||||
'email' => 'demo@example.com',
|
||||
'created_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
return $this->success($userInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户注册
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证数据
|
||||
$this->validate($data, [
|
||||
'username' => 'require|length:3,20',
|
||||
'password' => 'require|length:6,20',
|
||||
'email' => 'require|email',
|
||||
], [
|
||||
'username.require' => '用户名不能为空',
|
||||
'username.length' => '用户名长度3-20位',
|
||||
'password.require' => '密码不能为空',
|
||||
'password.length' => '密码长度6-20位',
|
||||
'email.require' => '邮箱不能为空',
|
||||
'email.email' => '邮箱格式不正确',
|
||||
]);
|
||||
|
||||
// TODO: 实际的注册逻辑
|
||||
return $this->success([
|
||||
'user_id' => rand(1000, 9999),
|
||||
], '注册成功');
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,43 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace app\api\controller;
|
||||
namespace app\api\controller\v1;
|
||||
|
||||
use app\api\common\Jwt;
|
||||
use app\api\common\Response;
|
||||
use app\api\controller\BaseController;
|
||||
use app\api\service\AuthService;
|
||||
use think\App;
|
||||
use think\exception\ValidateException;
|
||||
|
||||
/**
|
||||
* 认证控制器 (v1版本)
|
||||
* 处理用户登录、注册、Token 刷新等
|
||||
* v1 认证控制器
|
||||
*/
|
||||
class Auth extends BaseController
|
||||
{
|
||||
/**
|
||||
* @var AuthService
|
||||
*/
|
||||
protected AuthService $authService;
|
||||
|
||||
public function __construct()
|
||||
public function __construct(App $app)
|
||||
{
|
||||
parent::__construct();
|
||||
parent::__construct($app);
|
||||
$this->authService = new AuthService();
|
||||
}
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
* POST /api/v1/auth/login
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
try {
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证参数
|
||||
validate($data, [
|
||||
validate([
|
||||
'username' => 'require',
|
||||
'password' => 'require',
|
||||
], [
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
]);
|
||||
])->check($data);
|
||||
|
||||
$result = $this->authService->login(
|
||||
$data['username'],
|
||||
@@ -61,15 +55,13 @@ class Auth extends BaseController
|
||||
/**
|
||||
* 用户注册
|
||||
* POST /api/v1/auth/register
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function register()
|
||||
{
|
||||
try {
|
||||
$data = $this->request->post();
|
||||
|
||||
// 验证参数
|
||||
validate($data, [
|
||||
validate([
|
||||
'username' => 'require|length:3,20|alphaNum',
|
||||
'password' => 'require|length:6,20',
|
||||
'email' => 'email',
|
||||
@@ -80,7 +72,7 @@ class Auth extends BaseController
|
||||
'password.require' => '密码不能为空',
|
||||
'password.length' => '密码长度6-20位',
|
||||
'email.email' => '邮箱格式不正确',
|
||||
]);
|
||||
])->check($data);
|
||||
|
||||
$result = $this->authService->register(
|
||||
$data['username'],
|
||||
@@ -100,7 +92,6 @@ class Auth extends BaseController
|
||||
/**
|
||||
* 刷新 Token
|
||||
* POST /api/v1/auth/refresh
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function refresh()
|
||||
{
|
||||
@@ -122,7 +113,6 @@ class Auth extends BaseController
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
* GET /api/v1/auth/me
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function me()
|
||||
{
|
||||
@@ -143,19 +133,15 @@ class Auth extends BaseController
|
||||
/**
|
||||
* 退出登录
|
||||
* POST /api/v1/auth/logout
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
// JWT 无状态,退出只需客户端删除 Token
|
||||
// 如果需要服务端失效,可以将 Token 加入黑名单(需要 Redis 支持)
|
||||
return Response::success([], '退出成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改密码
|
||||
* POST /api/v1/auth/password
|
||||
* @return \think\response\Json
|
||||
*/
|
||||
public function password()
|
||||
{
|
||||
@@ -167,7 +153,7 @@ class Auth extends BaseController
|
||||
|
||||
$data = $this->request->post();
|
||||
|
||||
validate($data, [
|
||||
validate([
|
||||
'old_password' => 'require',
|
||||
'new_password' => 'require|length:6,20|confirm:confirm_password',
|
||||
], [
|
||||
@@ -175,7 +161,7 @@ class Auth extends BaseController
|
||||
'new_password.require' => '新密码不能为空',
|
||||
'new_password.length' => '新密码长度6-20位',
|
||||
'new_password.confirm' => '两次密码输入不一致',
|
||||
]);
|
||||
])->check($data);
|
||||
|
||||
$this->authService->changePassword(
|
||||
$payload['userid'],
|
||||
Reference in New Issue
Block a user