🎮 游戏管理 API
通过 HTTP API 管理游戏数据
游戏的CRUD操作。
📥 获取游戏信息
GET
/api/games参数:
name - 游戏名称(可选)
developer - 开发商(可选)
publisher - 发行商(可选)
备注:不传参数即可查询所有游戏
响应:200 OK,返回游戏数组。例如:
{
"status": 200,
"msg": "success",
"total": 1,
"data": [
{
"id": "1767042792152n1yc9qrf3",
"resourceType": "game",
"name": "游戏名称",
"nickname": "",
"nameZh": "",
"nameEn": "",
"nameJa": "",
"description": "游戏描述",
"developers": ["开发商1", "开发商2"],
"publisher": "发行商",
"tags": ["标签1", "标签2"],
"engine": "RPGMaker",
"coverPath": "C:/Games/Screenshots/cover.png",
"resourcePath": "C:/Games/GameFolder/game.exe",
"playTime": 3600,
"playCount": 10,
"visitedSessions": [
"2025-12-29T22:05:34.528Z",
"2026-01-21T08:53:13.157Z"
],
"addedDate": "2025-12-29T21:13:12.152Z",
"rating": 5,
"comment": "好玩",
"isFavorite": true
}
]
}JavaScript代码示例
const params = new URLSearchParams({'name': '游戏名称'}) // 按需添加查询条件,条件为空时查询所有游戏
const res = await fetch(`http://127.0.0.1:8765/api/games?${params}`)
const games = await res.json()Python代码示例
import requests
params = {'name': '游戏名称'} # 按需添加查询条件,条件为空时查询所有游戏
response = requests.get('http://127.0.0.1:8765/api/games', params=params)
games = response.json()📥 获取单个游戏
GET
/api/games/:id参数:id - 游戏ID
响应:200 OK 返回游戏对象,不存在则 404。例如:
{
"id": "1767042792152n1yc9qrf3",
"resourceType": "game",
"name": "游戏名称",
"nickname": "",
"nameZh": "",
"nameEn": "",
"nameJa": "",
"description": "游戏描述",
"developers": ["开发商1", "开发商2"],
"publisher": "发行商",
"tags": ["标签1", "标签2"],
"engine": "RPGMaker",
"coverPath": "C:/Games/Screenshots/cover.png",
"resourcePath": "C:/Games/GameFolder/game.exe",
"playTime": 3600,
"playCount": 10,
"visitedSessions": [
"2025-12-29T22:05:34.528Z",
"2026-01-21T08:53:13.157Z"
],
"addedDate": "2025-12-29T21:13:12.152Z",
"rating": 5,
"comment": "好玩",
"isFavorite": true
}JavaScript代码示例
const res = await fetch('http://127.0.0.1:8765/api/games/1234567890abc')
const game = await res.json()Python代码示例
import requests
response = requests.get('http://127.0.0.1:8765/api/games/1234567890abc')
game = response.json()➕ 创建游戏
POST
/api/games请求体:
{
"name": "游戏名称",
"nickname": "",
"nameZh": "",
"nameEn": "",
"nameJa": "",
"description": "游戏描述",
"developers": ["开发商"],
"publisher": "发行商",
"tags": ["标签1", "标签2"],
"engine": "RPGMaker",
"coverPath": "C:/Games/Screenshots/cover.png",
"resourcePath": "C:/Games/GameFolder/game.exe",
"playTime": 0,
"playCount": 0,
"visitedSessions": [],
"rating": null,
"comment": "",
"isFavorite": false
}响应:201 Created,返回创建的游戏对象。例如:
注意:ID 和 addedDate 会自动生成
JavaScript代码示例
const res = await fetch('http://127.0.0.1:8765/api/games', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: '新游戏',
developers: ['开发商'],
resourcePath: 'C:/Games/game.exe'
})
})
const createdGame = await res.json()Python代码示例
import requests
new_game = {
'name': '新游戏',
'developers': ['开发商'],
'resourcePath': 'C:/Games/game.exe'
}
response = requests.post('http://127.0.0.1:8765/api/games', json=new_game)
created_game = response.json()✏️ 更新游戏
PUT
/api/games/:id参数:id - 游戏ID
请求体:要更新的字段(JSON 对象,只需传要修改的字段)
{
"name": "更新后的游戏名",
"description": "更新后的描述",
"rating": 5,
"tags": ["新标签1", "新标签2"],
"isFavorite": true
}响应:200 OK,返回更新后的游戏对象。例如:
注意:ID 和 addedDate 字段不允许修改
JavaScript代码示例
const res = await fetch('http://127.0.0.1:8765/api/games/1234567890abc', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ name: '更新后的游戏名' })
})
const updatedGame = await res.json()Python代码示例
import requests
update_data = {'name': '更新后的游戏名'}
response = requests.put('http://127.0.0.1:8765/api/games/1234567890abc', json=update_data)
updated_game = response.json()🗑️ 删除游戏
DELETE
/api/games/:id参数:id - 游戏ID
响应:204 No Content(成功)或 404 Not Found(不存在)
注意:删除操作不会删除本地游戏文件,仅移除管理器中的引用
JavaScript
await fetch('http://127.0.0.1:8765/api/games/1234567890abc', { method: 'DELETE' })
// 成功返回 204,无响应体Python
import requests
response = requests.delete('http://127.0.0.1:8765/api/games/1234567890abc')
# 成功返回 204,无响应体📊 游戏数据字段说明
游戏对象包含以下字段:
id- 游戏唯一标识符(自动生成,不可修改)resourceType- 资源类型,固定为 "game"name- 游戏名称(必需)nickname- 游戏昵称nameZh- 中文名nameEn- 英文名nameJa- 日文名description- 游戏描述developers- 开发商数组(注意是数组)publisher- 发行商tags- 标签数组engine- 游戏引擎resourcePath- 游戏可执行文件路径coverPath- 封面图片路径playTime- 游戏时长(秒)playCount- 运行次数visitedSessions- 访问会话时间数组(ISO 字符串数组,记录每次启动时间)addedDate- 添加日期(自动生成,不可修改)rating- 评分(1-5 或 null)comment- 备注isFavorite- 是否收藏
⚠️ 错误处理
API 使用标准的 HTTP 状态码:
- 200 OK:请求成功
- 201 Created:资源创建成功
- 204 No Content:删除成功(无响应体)
- 400 Bad Request:请求参数错误(如缺少必需字段)
- 404 Not Found:资源不存在
- 500 Internal Server Error:服务器内部错误
错误响应格式:
{
"error": "错误信息"
}