# D04 - API对接规范 ## 通用约定 ### 请求 - 前端请求前缀: `/api` → Nuxt server proxy → BinV API - 所有 User 请求路径: `/api/user/*` - Content-Type: `application/json` - 分页: `page` (1-indexed) + `page_size` (default=20, max=100) ### 响应格式 ```typescript interface ApiResponse { code: number; msg: string; data: T; } interface PaginatedData { list: T[]; total: number; page: number; page_size: number; } ``` ### 错误码 | 范围 | 说明 | |---|---| | 0 | 成功 | | 40001-40099 | 鉴权错误(登录失败、token过期) | | 40100-40199 | 参数校验错误 | | 40200-40299 | 业务逻辑错误 | | 50000-50099 | 服务器内部错误 | ### 鉴权 | 端 | 方式 | Header | |---|---|---| | 公开接口 | 无需 | - | | 需登录接口 | JWT | `Authorization: Bearer {user_jwt}` | --- ## API接口清单 ### 用户鉴权 — /user/auth | 方法 | 路径 | 说明 | 需登录 | |---|---|---|---| | POST | /user/auth/register | 注册 | ❌ | | POST | /user/auth/login | 登录 | ❌ | | GET | /user/auth/profile | 获取用户信息 | ✅ | ### 影片浏览 — /user/vod | 方法 | 路径 | 说明 | 需登录 | |---|---|---|---| | GET | /user/vod | 影片列表 | ❌ | | GET | /user/vod/search | 搜索 | ❌ | | GET | /user/vod/{id} | 详情+播放源 | ❌ | | GET | /user/vod/recommended | 推荐列表 | ❌ | | GET | /user/vod/latest | 最新更新 | ❌ | ### 分类 — /user/category | 方法 | 路径 | 说明 | 需登录 | |---|---|---|---| | GET | /user/category | 分类树 | ❌ | ### 收藏 — /user/favorite | 方法 | 路径 | 说明 | 需登录 | |---|---|---|---| | GET | /user/favorite | 收藏列表 | ✅ | | POST | /user/favorite | 添加收藏 | ✅ | | DELETE | /user/favorite/{vod_id} | 取消收藏 | ✅ | ### 播放历史 — /user/history | 方法 | 路径 | 说明 | 需登录 | |---|---|---|---| | GET | /user/history | 历史列表 | ✅ | | POST | /user/history | 记录/更新 | ✅ | | DELETE | /user/history/{id} | 删除 | ✅ | --- ## 前端类型定义 ### 用户 ```typescript interface UserLoginParams { username: string; password: string; } interface UserRegisterParams { username: string; password: string; email?: string; } interface LoginResult { token: string; expires_in: number; user: UserInfo; } interface UserInfo { id: number; username: string; email: string | null; nickname: string | null; avatar: string | null; favorite_count: number; created_at: string; } ``` ### 影片 ```typescript interface VodUserItem { id: number; name: string; sub_name: string | null; category_id: number; category_name: string; cover: string | null; actor: string | null; director: string | null; area: string | null; lang: string | null; year: string | null; score: number | null; remarks: string | null; is_end: boolean; source_count: number; updated_at: string; } interface VodUserDetail extends VodUserItem { duration: string | null; blurb: string | null; content: string | null; view_count: number; sources: VodPlaySource[]; } interface VodPlaySource { source_id: number; source_name: string; source_code: string; play_from: string; play_from_label: string; episodes: EpisodeItem[]; } interface EpisodeItem { name: string; url: string; parse_url: string | null; } interface VodListParams { category_id?: number; area?: string; lang?: string; year?: string; sort?: 'updated_at' | 'view_count' | 'score' | 'created_at'; order?: 'desc' | 'asc'; page?: number; page_size?: number; } interface VodSearchParams { keyword: string; category_id?: number; page?: number; page_size?: number; } interface VodLatestParams { hours?: number; page?: number; page_size?: number; } ``` ### 分类 ```typescript interface CategoryUserNode { id: number; name: string; children: CategoryUserNode[] | null; } ``` ### 收藏 ```typescript interface FavoriteListResult { list: VodUserItem[]; total: number; } interface FavoriteAddParams { vod_id: number; } ``` ### 播放历史 ```typescript interface HistoryItem { id: number; vod_id: number; vod_name: string; vod_cover: string | null; source_name: string; episode: string | null; progress: number; duration: number; updated_at: string; } interface HistoryRecordParams { vod_id: number; source_id: number; episode?: string; play_url: string; progress?: number; duration?: number; } ``` --- ## 页面 × API 调用映射 | 页面 | SSR/CSR | API 调用 | |---|---|---| | 首页 | SSR | `GET /user/vod/recommended` + `GET /user/vod/latest` + `GET /user/category` | | 分类列表 | SSR | `GET /user/vod?category_id=X` + `GET /user/category` | | 搜索 | CSR | `GET /user/vod/search?keyword=X` | | 影片详情 | SSR | `GET /user/vod/{id}` | | 登录 | CSR | `POST /user/auth/login` | | 注册 | CSR | `POST /user/auth/register` | | 收藏 | CSR | `GET /user/favorite` | | 历史 | CSR | `GET /user/history` | --- ## Nuxt Server Proxy 配置 ```typescript // server/api/[...].ts import { defineEventHandler, proxyRequest } from 'h3'; export default defineEventHandler(async (event) => { const config = useRuntimeConfig(); const path = event.path.replace(/^\/api/, ''); const target = `${config.apiBaseUrl}${path}`; return proxyRequest(event, target); }); ``` ### 环境变量 ```bash # .env NUXT_PUBLIC_API_BASE=/api # 前端请求前缀 NUXT_API_BASE_URL=http://localhost:9999 # Server proxy 目标 ``` ### nuxt.config.ts ```typescript export default defineNuxtConfig({ runtimeConfig: { apiBaseUrl: process.env.NUXT_API_BASE_URL || 'http://localhost:9999', public: { apiBase: process.env.NUXT_PUBLIC_API_BASE || '/api', }, }, }); ```