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:
+79
-74
@@ -12,24 +12,26 @@
|
||||
|
||||
```
|
||||
tp/
|
||||
├── app/ # 应用目录
|
||||
│ ├── api/ # API 应用
|
||||
│ │ ├── controller/ # 控制器
|
||||
│ │ │ ├── BaseController.php # 基础控制器
|
||||
│ │ │ ├── Index.php # 首页控制器
|
||||
│ │ │ └── User.php # 用户控制器
|
||||
│ │ ├── model/ # 模型
|
||||
│ │ ├── validate/ # 验证器
|
||||
│ │ ├── middleware/ # 中间件
|
||||
│ │ │ ├── Auth.php # 认证中间件
|
||||
│ │ │ └── CrossDomain.php # 跨域中间件
|
||||
│ │ ├── common/ # 公共类
|
||||
│ │ │ └── Response.php # 统一响应类
|
||||
│ │ └── AppService.php # 应用服务
|
||||
│ └── BaseController.php # 默认基础控制器
|
||||
├── app/
|
||||
│ └── api/
|
||||
│ ├── controller/
|
||||
│ │ ├── BaseController.php
|
||||
│ │ └── v1/
|
||||
│ │ └── Auth.php
|
||||
│ ├── model/
|
||||
│ │ └── Member.php
|
||||
│ ├── service/
|
||||
│ │ └── AuthService.php
|
||||
│ ├── middleware/
|
||||
│ │ ├── Auth.php
|
||||
│ │ └── CrossDomain.php
|
||||
│ ├── common/
|
||||
│ │ ├── Jwt.php
|
||||
│ │ └── Response.php
|
||||
│ ├── middleware.php
|
||||
│ └── route/
|
||||
│ └── app.php
|
||||
├── config/ # 配置文件
|
||||
├── route/ # 路由
|
||||
│ └── api.php # API 路由配置
|
||||
├── public/ # 公共资源
|
||||
├── runtime/ # 运行时目录
|
||||
├── vendor/ # Composer 依赖
|
||||
@@ -112,43 +114,10 @@ php think run
|
||||
|
||||
## API 接口文档
|
||||
|
||||
### 公共接口(无需认证)
|
||||
### 公开接口
|
||||
|
||||
#### 1. 首页信息
|
||||
- **URL**: `/api/index`
|
||||
- **Method**: GET
|
||||
- **响应示例**:
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"name": "ThinkPHP API",
|
||||
"version": "8.1.3",
|
||||
"message": "Welcome to ThinkPHP API Application"
|
||||
},
|
||||
"time": 1640000000
|
||||
}
|
||||
```
|
||||
|
||||
#### 2. 健康检查
|
||||
- **URL**: `/api/health`
|
||||
- **Method**: GET
|
||||
- **响应示例**:
|
||||
```json
|
||||
{
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"status": "ok",
|
||||
"timestamp": "2024-01-01 12:00:00"
|
||||
},
|
||||
"time": 1640000000
|
||||
}
|
||||
```
|
||||
|
||||
#### 3. 用户登录
|
||||
- **URL**: `/api/user/login`
|
||||
#### 1. 用户登录
|
||||
- **URL**: `/api/v1/auth/login`
|
||||
- **Method**: POST
|
||||
- **参数**:
|
||||
- `username`: 用户名(必填)
|
||||
@@ -159,15 +128,23 @@ php think run
|
||||
"code": 200,
|
||||
"msg": "登录成功",
|
||||
"data": {
|
||||
"token": "example_token_xxx",
|
||||
"username": "demo"
|
||||
"token": "jwt_access_token",
|
||||
"refresh_token": "jwt_refresh_token",
|
||||
"expires_in": 604800,
|
||||
"user": {
|
||||
"userid": 1001,
|
||||
"username": "demo",
|
||||
"v_type": 0,
|
||||
"endtime": 0,
|
||||
"formtypeid": 0
|
||||
}
|
||||
},
|
||||
"time": 1640000000
|
||||
}
|
||||
```
|
||||
|
||||
#### 4. 用户注册
|
||||
- **URL**: `/api/user/register`
|
||||
#### 2. 用户注册
|
||||
- **URL**: `/api/v1/auth/register`
|
||||
- **Method**: POST
|
||||
- **参数**:
|
||||
- `username`: 用户名(3-20位)
|
||||
@@ -179,16 +156,31 @@ php think run
|
||||
"code": 200,
|
||||
"msg": "注册成功",
|
||||
"data": {
|
||||
"user_id": 1001
|
||||
"token": "jwt_access_token",
|
||||
"refresh_token": "jwt_refresh_token",
|
||||
"expires_in": 604800,
|
||||
"user": {
|
||||
"userid": 1001,
|
||||
"username": "demo",
|
||||
"v_type": 0,
|
||||
"endtime": 0,
|
||||
"formtypeid": 0
|
||||
}
|
||||
},
|
||||
"time": 1640000000
|
||||
}
|
||||
```
|
||||
|
||||
#### 3. 刷新 Token
|
||||
- **URL**: `/api/v1/auth/refresh`
|
||||
- **Method**: POST
|
||||
- **参数**:
|
||||
- `refresh_token`: 刷新令牌
|
||||
|
||||
### 认证接口(需要 token)
|
||||
|
||||
#### 5. 获取用户信息
|
||||
- **URL**: `/api/user/info`
|
||||
#### 4. 获取用户信息
|
||||
- **URL**: `/api/v1/auth/me`
|
||||
- **Method**: GET
|
||||
- **Headers**: `token: your_token`
|
||||
- **响应示例**:
|
||||
@@ -197,22 +189,41 @@ php think run
|
||||
"code": 200,
|
||||
"msg": "success",
|
||||
"data": {
|
||||
"id": 1,
|
||||
"userid": 1,
|
||||
"username": "demo_user",
|
||||
"nickname": "演示用户",
|
||||
"avatar": "",
|
||||
"email": "demo@example.com",
|
||||
"created_at": "2024-01-01 12:00:00"
|
||||
"v_type": 1,
|
||||
"endtime": 1777777777,
|
||||
"formtypeid": 0,
|
||||
"disabled": 0,
|
||||
"product": {
|
||||
"v_type": 1,
|
||||
"video_num": 100,
|
||||
"account_num": 5
|
||||
}
|
||||
},
|
||||
"time": 1640000000
|
||||
}
|
||||
```
|
||||
|
||||
#### 5. 退出登录
|
||||
- **URL**: `/api/v1/auth/logout`
|
||||
- **Method**: POST
|
||||
- **Headers**: `token: your_token`
|
||||
|
||||
#### 6. 修改密码
|
||||
- **URL**: `/api/v1/auth/password`
|
||||
- **Method**: POST
|
||||
- **Headers**: `token: your_token`
|
||||
- **参数**:
|
||||
- `old_password`: 原密码
|
||||
- `new_password`: 新密码
|
||||
- `confirm_password`: 确认新密码
|
||||
|
||||
## 功能特性
|
||||
|
||||
### 1. 多应用模式
|
||||
- 采用 ThinkPHP 多应用扩展
|
||||
- API 接口独立目录
|
||||
- API 接口集中在 `app/api`
|
||||
- 便于扩展其他应用模块
|
||||
|
||||
### 2. 统一响应格式
|
||||
@@ -238,9 +249,6 @@ $this->validate($data, [
|
||||
'username' => 'require|length:3,20',
|
||||
'password' => 'require|length:6,20',
|
||||
]);
|
||||
|
||||
// 或使用验证器
|
||||
$this->validate($data, 'app\api\validate\User.login');
|
||||
```
|
||||
|
||||
### 5. 模型自动时间戳
|
||||
@@ -287,13 +295,10 @@ php think run
|
||||
php think clear
|
||||
|
||||
# 生成控制器
|
||||
php think make:controller api@Demo
|
||||
php think make:controller api@v1/Auth
|
||||
|
||||
# 生成模型
|
||||
php think make:model api@Demo
|
||||
|
||||
# 生成验证器
|
||||
php think make:validate api@Demo
|
||||
php think make:model api@Member
|
||||
```
|
||||
|
||||
## 扩展安装
|
||||
|
||||
Reference in New Issue
Block a user