增加用户管理

This commit is contained in:
wtz
2026-02-20 23:39:49 +08:00
parent a272dad5f1
commit af3b805dbf
18 changed files with 1493 additions and 53 deletions

167
API.md
View File

@@ -27,7 +27,110 @@
---
## HTTP API
## 认证相关 API
### 获取验证码
**GET** `/api/auth/captcha`
响应:
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": {
"captchaId": "abc123",
"image": "data:image/png;base64,..."
}
}
```
### 用户注册
**POST** `/api/auth/register`
请求体:
```json
{
"username": "testuser",
"password": "123456",
"nickname": "测试玩家",
"captcha": "ABCD",
"captchaId": "abc123"
}
```
| 参数 | 类型 | 必填 | 说明 |
|------|------|------|------|
| username | string | 是 | 用户名3-20字符 |
| password | string | 是 | 密码至少6位 |
| nickname | string | 是 | 昵称 |
| captcha | string | 是 | 验证码 |
| captchaId | string | 是 | 验证码ID |
响应:
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": {
"token": "xxx",
"userId": "xxx",
"username": "testuser",
"nickname": "测试玩家"
}
}
```
### 用户登录
**POST** `/api/auth/login`
请求体:
```json
{
"username": "testuser",
"password": "123456",
"captcha": "ABCD",
"captchaId": "abc123"
}
```
响应: 同注册
### 验证Token
**GET** `/api/auth/validate`
请求头: `Authorization: Bearer {token}`
响应:
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": {
"userId": "xxx",
"username": "testuser",
"nickname": "测试玩家"
}
}
```
### 退出登录
**POST** `/api/auth/logout`
请求头: `Authorization: Bearer {token}`
---
## 房间相关 API
> 注意: 所有房间相关 API 需要在请求头中携带 `Authorization: Bearer {token}`
### 创建房间
@@ -83,6 +186,53 @@
}
```
### 离开房间
**POST** `/api/rooms/leave`
请求头: `Authorization: Bearer {token}`
响应:
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": null
}
```
### 获取当前房间
**GET** `/api/rooms/current`
请求头: `Authorization: Bearer {token}`
用于检查用户当前是否在房间中。如果用户之前创建了房间但未离开,可以获取房间信息并自动重新连接。
响应(在房间中):
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": {
"roomId": "a1b2c3d4",
"playerId": "i9j0k1l2"
}
}
```
响应(不在房间中):
```json
{
"status": 200,
"code": 0,
"message": "success",
"data": null
}
```
### 错误响应示例
```json
@@ -124,6 +274,7 @@
| chat | 双向 | 聊天消息 |
| state | 服务器→客户端 | 游戏状态更新 |
| gameOver | 服务器→客户端 | 游戏结束 |
| leave | 服务器→客户端 | 玩家离开房间 |
| error | 服务器→客户端 | 错误消息 |
### 准备
@@ -169,6 +320,7 @@
### 聊天
客户端发送:
```json
{
"type": "chat",
@@ -178,6 +330,19 @@
}
```
服务器推送:
```json
{
"type": "chat",
"data": {
"playerId": "xxx",
"playerName": "玩家昵称",
"message": "消息内容"
},
"timestamp": 1708123456
}
```
### 游戏状态 (服务器推送)
```json