# API 接口文档 ## 基础信息 - **基础URL**: `http://localhost:8080` - **数据格式**: JSON - **字符编码**: UTF-8 ## 统一响应格式 ```json { "code": 200, "message": "操作成功", "data": {} } ``` ### 状态码说明 - `200`: 操作成功 - `400`: 参数验证失败 - `500`: 服务器错误 --- ## 1. 用户相关接口 ### 1.1 用户注册 **POST** `/api/user/register` **请求体:** ```json { "username": "testuser", "password": "123456", "nickname": "测试用户", "phone": "13800138000", "email": "test@example.com" } ``` **响应示例:** ```json { "code": 200, "message": "注册成功", "data": { "id": 1, "username": "testuser", "nickname": "测试用户", "phone": "13800138000", "email": "test@example.com", "isVip": false, "status": 1 } } ``` ### 1.2 用户登录 **POST** `/api/user/login` **请求体:** ```json { "username": "testuser", "password": "123456" } ``` **响应示例:** ```json { "code": 200, "message": "登录成功", "data": { "user": { "id": 1, "username": "testuser", "nickname": "测试用户", "isVip": false, "status": 1 }, "token": "mock_token_1" } } ``` ### 1.3 查询用户信息 **GET** `/api/user/{id}` **响应示例:** ```json { "code": 200, "message": "操作成功", "data": { "id": 1, "username": "testuser", "nickname": "测试用户", "avatar": null, "phone": "13800138000", "email": "test@example.com", "isVip": false, "status": 1 } } ``` --- ## 2. 书籍相关接口 ### 2.1 分页查询书籍 **GET** `/api/book/list` **请求参数:** | 参数 | 类型 | 必填 | 说明 | 默认值 | |------|------|------|------|--------| | page | Integer | 否 | 页码 | 1 | | size | Integer | 否 | 每页数量 | 10 | | keyword | String | 否 | 搜索关键词(书名、作者、简介) | - | | categoryId | Integer | 否 | 分类ID | - | | status | Integer | 否 | 状态(0-下架,1-上架) | - | | isVip | Boolean | 否 | 是否VIP专享 | - | **响应示例:** ```json { "code": 200, "message": "操作成功", "data": { "list": [ { "id": 1, "title": "西游记", "author": "(明) 吴承恩", "cover": "https://picsum.photos/seed/book1/200/300", "brief": "《西游记》是中国古代第一部神魔题材的长篇章回小说...", "price": 0.00, "isFree": true, "isVip": false, "categoryId": 1, "status": 1, "viewCount": 0, "likeCount": 0, "readCount": 0 } ], "total": 100, "page": 1, "size": 10, "totalPages": 10 } } ``` ### 2.2 根据ID查询书籍详情 **GET** `/api/book/{id}` **响应示例:** ```json { "code": 200, "message": "操作成功", "data": { "id": 1, "title": "西游记", "author": "(明) 吴承恩", "cover": "https://picsum.photos/seed/book1/200/300", "image": "https://picsum.photos/seed/book1/200/300", "brief": "《西游记》是中国古代第一部神魔题材的长篇章回小说...", "desc": "中国古代第一部浪漫主义章回体长篇神魔小说...", "introduction": "本书《西游记》是中国古代第一部浪漫主义章回体长篇神魔小说...", "price": 0.00, "isFree": true, "isVip": false, "categoryId": 1, "status": 1, "viewCount": 0, "likeCount": 0, "readCount": 0 } } ``` ### 2.3 创建书籍 **POST** `/api/book` **请求体:** ```json { "title": "新书标题", "author": "作者名称", "cover": "https://example.com/cover.jpg", "image": "https://example.com/image.jpg", "brief": "简短简介", "desc": "描述", "introduction": "详细介绍", "price": 29.90, "isFree": false, "isVip": false, "categoryId": 1, "status": 1 } ``` **响应示例:** ```json { "code": 200, "message": "创建成功", "data": { "id": 10, "title": "新书标题", "author": "作者名称", "status": 1 } } ``` ### 2.4 更新书籍 **PUT** `/api/book/{id}` **请求体:** 同创建书籍 **响应示例:** ```json { "code": 200, "message": "更新成功", "data": { "id": 1, "title": "更新后的书名", "status": 1 } } ``` ### 2.5 删除书籍 **DELETE** `/api/book/{id}` **响应示例:** ```json { "code": 200, "message": "删除成功", "data": null } ``` ### 2.6 批量删除书籍 **DELETE** `/api/book/batch` **请求体:** ```json [1, 2, 3] ``` **响应示例:** ```json { "code": 200, "message": "批量删除成功", "data": null } ``` --- ## 使用示例 ### 使用 curl 命令 ```bash # 用户注册 curl -X POST "http://localhost:8080/api/user/register" \ -H "Content-Type: application/json" \ -d '{ "username": "testuser", "password": "123456", "nickname": "测试用户" }' # 用户登录 curl -X POST "http://localhost:8080/api/user/login" \ -H "Content-Type: application/json" \ -d '{ "username": "testuser", "password": "123456" }' # 查询书籍列表 curl -X GET "http://localhost:8080/api/book/list?page=1&size=10" # 查询书籍详情 curl -X GET "http://localhost:8080/api/book/1" # 创建书籍 curl -X POST "http://localhost:8080/api/book" \ -H "Content-Type: application/json" \ -d '{ "title": "新书", "author": "作者", "status": 1 }' ``` ### 使用 JavaScript (fetch) ```javascript // 用户注册 fetch('http://localhost:8080/api/user/register', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'testuser', password: '123456', nickname: '测试用户' }) }) .then(response => response.json()) .then(data => console.log(data)); // 用户登录 fetch('http://localhost:8080/api/user/login', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ username: 'testuser', password: '123456' }) }) .then(response => response.json()) .then(data => console.log(data)); // 查询书籍列表 fetch('http://localhost:8080/api/book/list?page=1&size=10') .then(response => response.json()) .then(data => console.log(data)); ``` --- ## 注意事项 1. 所有POST/PUT请求需要设置 `Content-Type: application/json` 头 2. 密码在注册和登录时使用MD5加密(生产环境建议使用BCrypt) 3. 已配置跨域支持,允许所有来源的请求 4. 参数验证失败会返回详细的错误信息 --- ## 数据库配置 执行 `book/src/main/resources/db/schema.sql` 文件创建数据库和表结构。 确保 `application.properties` 中的数据库配置正确: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/books_db?... spring.datasource.username=root spring.datasource.password=root ```