From 01a2a6c53df233b5d267badbd5bd23243583023a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AA=E5=B1=B1=E4=B8=83=E5=AF=BB?= <3401460572@qq.com> Date: Sat, 31 May 2025 18:37:14 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=20API.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit api文档...? --- API.md | 164 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 API.md diff --git a/API.md b/API.md new file mode 100644 index 0000000..976f8d8 --- /dev/null +++ b/API.md @@ -0,0 +1,164 @@ +# API 文档 + +## 基础信息 +- **基础 URL**: `http://localhost:5000/api` +- **数据格式**: JSON + +## 认证方式 +使用简单的 Token 认证(实际生产环境应使用 JWT 等更安全的方式)。 +登录后返回的 Token 需在后续请求中通过 `Authorization` 头携带: +`Authorization: Bearer ` + +--- + +## API 端点 + +### 1. 用户注册 +**URL**: `/register` +**方法**: POST +**请求体**: +```json +{ +"username": "string", +"password": "string" +} +``` + +**成功响应**: +```json +{ +"success": true, +"message": "User registered successfully" +} +``` +**错误响应**: +```json +{ +"success": false, +"message": "Username already exists" +} +``` + + +--- + +### 2. 用户登录 +**URL**: `/login` +**方法**: POST +**请求体**: +```json +{ +"username": "string", +"password": "string" +} +``` + +**成功响应**: +```json +{ +"success": true, +"message": "Login successful", +"token": "string", +"user_id": "integer", +"username": "string" +} +``` +**错误响应**: +```json +{ +"success": false, +"message": "Invalid username or password" +} +``` + + +--- + +### 3. 获取用户列表 +**URL**: `/users` +**方法**: GET +**成功响应**: +```json +{ + "success": true, + "users": [ + { + "id": "integer", + "username": "string", + "is_online": "boolean", + "last_login": "string" + } + ] +} +``` + +--- + +### 4. 获取消息历史 +**URL**: `/messages` +**方法**: GET +**查询参数**: +- `user_id` (必填): 当前用户ID +- `other_id` (必填): 聊天对象用户ID +- `limit` (可选): 返回消息数量限制(默认 100) + +**成功响应**: +```json +{ + "success": true, + "messages": [ + { + "id": "integer", + "sender_id": "integer", + "receiver_id": "integer", + "content": "string", + "timestamp": "string", + "is_recalled": "boolean", + "sender_name": "string", + "receiver_name": "string" + } + ] +} +``` + + +--- + +### 5. 撤回消息 +**URL**: `/recall_message` +**方法**: POST +**请求体**: +```json +{ +"message_id": "integer", +"user_id": "integer" +} +``` + +**成功响应**: +```json +{ +"success": true, +"message": "Message recalled successfully" +} +``` +**错误响应**: +```json +{ +"success": false, +"message": "You can only recall your own messages" +} +``` + + +--- + +## 使用说明 +1. 所有时间字段使用 ISO 8601 格式(如 `"2023-10-05T14:30:00Z"`) +2. 用户操作约束: + - 用户名必须唯一 + - 只能撤回自己发送的消息 +3. 生产环境建议: + - 启用 HTTPS + - 使用 JWT 替代简单 Token + - 添加请求速率限制 \ No newline at end of file From b2fea7a26e63fdf84a9f62c7277665e8efd5d586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AA=E5=B1=B1=E4=B8=83=E5=AF=BB?= <3401460572@qq.com> Date: Sat, 31 May 2025 18:56:01 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20README.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c2c856d..e5296c0 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,3 @@ # chatserver +查看[API文档](/API.md) \ No newline at end of file From 635c033047be64662aee6122f218b06db1c7abf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BB=AA=E5=B1=B1=E4=B8=83=E5=AF=BB?= <3401460572@qq.com> Date: Sat, 31 May 2025 18:59:23 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=20API.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- API.md | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/API.md b/API.md index 976f8d8..225a682 100644 --- a/API.md +++ b/API.md @@ -6,8 +6,8 @@ ## 认证方式 使用简单的 Token 认证(实际生产环境应使用 JWT 等更安全的方式)。 -登录后返回的 Token 需在后续请求中通过 `Authorization` 头携带: -`Authorization: Bearer ` +登录后返回的 Token 需在后续请求中通过 `Authorization` 头携带: `Authorization: Bearer ` + --- @@ -19,23 +19,23 @@ **请求体**: ```json { -"username": "string", -"password": "string" + "username": "string", + "password": "string" } ``` **成功响应**: ```json { -"success": true, -"message": "User registered successfully" + "success": true, + "message": "User registered successfully" } ``` **错误响应**: ```json { -"success": false, -"message": "Username already exists" + "success": false, + "message": "Username already exists" } ``` @@ -48,26 +48,26 @@ **请求体**: ```json { -"username": "string", -"password": "string" + "username": "string", + "password": "string" } ``` **成功响应**: ```json { -"success": true, -"message": "Login successful", -"token": "string", -"user_id": "integer", -"username": "string" + "success": true, + "message": "Login successful", + "token": "string", + "user_id": "integer", + "username": "string" } ``` **错误响应**: ```json { -"success": false, -"message": "Invalid username or password" + "success": false, + "message": "Invalid username or password" } ``` @@ -130,23 +130,23 @@ **请求体**: ```json { -"message_id": "integer", -"user_id": "integer" + "message_id": "integer", + "user_id": "integer" } ``` **成功响应**: ```json { -"success": true, -"message": "Message recalled successfully" + "success": true, + "message": "Message recalled successfully" } ``` **错误响应**: ```json { -"success": false, -"message": "You can only recall your own messages" + "success": false, + "message": "You can only recall your own messages" } ```