Files
joke/api/API操作文档.md
bwstudio ceed63fcb0 fix: resolve 10 code review issues
High priority:
- Fix concurrent race condition for view_count/like_count (atomic update)
- Add route request ID tracking to prevent race conditions
- Filter get_joke by status=approved (no pending content leak)
- Add error feedback for like button

Performance:
- Optimize random joke query (avoid full table sort)
- Limit page_size max to 100 (DoS prevention)

Medium:
- Add localStorage quota error handling
- Handle empty AI response gracefully
- Fix generate content title extraction

Low:
- Add rejected_jokes to stats API
- Update dashboard to show rejected count
2026-06-02 20:35:08 +08:00

125 lines
2.7 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# API 操作文档 - 笑话管理系统
## 基础信息
- **API 地址**: `http://<服务器IP>:8001`
- **默认管理员**: `admin` / `admin123`
- **认证方式**: JWT Bearer Token
---
## 一、登录获取 Token
```bash
curl -X POST http://<服务器IP>:8001/api/auth/login \
-H "Content-Type: application/json" \
-d '{"username": "admin", "password": "admin123"}'
```
**返回示例**
```json
{
"access_token": "eyJhbGciOiJIUzI1NiIs...",
"token_type": "bearer"
}
```
---
## 二、提交笑话
```bash
curl -X POST http://<服务器IP>:8001/api/admin/jokes \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <TOKEN>" \
-d '{
"title": "笑话标题",
"content": "笑话内容正文",
"type_id": 1,
"crowd_id": 2
}'
```
| 字段 | 类型 | 必填 | 说明 |
|------|------|------|------|
| `title` | string | 是 | 笑话标题 |
| `content` | string | 是 | 笑话正文 |
| `type_id` | int | 否 | 类型 ID(查分类列表获取) |
| `crowd_id` | int | 否 | 人群 ID(查分类列表获取) |
提交后 status 默认为 `pending`(待审核)。
---
## 三、查询分类 ID
```bash
# 查看所有类型
curl http://<服务器IP>:8001/api/categories/types
# 查看所有人群
curl http://<服务器IP>:8001/api/categories/crowds
```
---
## 四、完整脚本示例(批量提交)
```bash
#!/bin/bash
SERVER="http://192.168.1.10:8001"
# 登录获取 Token
TOKEN=$(curl -s -X POST "$SERVER/api/auth/login" \
-H "Content-Type: application/json" \
-d '{"username":"admin","password":"admin123"}' \
| python -c "import sys,json; print(json.load(sys.stdin)['access_token'])")
echo "Token: $TOKEN"
# 批量提交笑话
jokes=(
'{"title":"笑话1","content":"内容1","type_id":1,"crowd_id":2}'
'{"title":"笑话2","content":"内容2","type_id":1,"crowd_id":null}'
)
for joke in "${jokes[@]}"; do
curl -X POST "$SERVER/api/admin/jokes" \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $TOKEN" \
-d "$joke"
echo ""
done
```
---
## 五、其他管理接口
### 查询笑话列表
```bash
curl -H "Authorization: Bearer <TOKEN>" \
"http://<服务器IP>:8001/api/admin/jokes?page=1&page_size=20&status=pending"
```
### 审核通过
```bash
curl -X PUT http://<服务器IP>:8001/api/admin/jokes/batch-approve \
-H "Content-Type: application/json" \
-H "Authorization: Bearer <TOKEN>" \
-d '[1, 2, 3]'
```
### 删除笑话
```bash
curl -X DELETE http://<服务器IP>:8001/api/admin/jokes/1 \
-H "Authorization: Bearer <TOKEN>"
```
---
## 六、注意事项
1.`<服务器IP>` 替换为实际的服务器 IP 地址
2. 确保服务器防火墙已放行 **8001** 端口
3. Token 有过期时间,过期后需重新登录获取