# 衣橱-server **Repository Path**: strenger/wardrobe-server ## Basic Information - **Project Name**: 衣橱-server - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-02-11 - **Last Updated**: 2026-02-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 衣橱管理系统 - 后端服务 ## 项目概述 衣橱管理系统后端服务是一个基于Node.js + Express开发的RESTful API服务,用于支持前端衣橱管理系统的所有功能。系统提供以下核心服务: - **衣物管理**:提供衣物的增删改查操作 - **穿搭管理**:提供穿搭方案的创建、编辑、保存和删除功能 - **图片上传**:支持多图片上传和管理 - **数据存储**:使用SQLite数据库存储数据,确保数据持久化 ## 技术栈 - Node.js 14+ - Express.js 4.18+ - SQLite3 (数据库存储) - Multer (文件上传处理) - UUID (生成唯一标识符) - CORS (跨域资源共享) ## 项目结构 ``` server/ ├── routes/ # 路由目录 │ ├── clothing.js # 衣物管理接口 │ ├── outfits.js # 穿搭管理接口 │ └── upload.js # 图片上传接口 ├── utils/ # 工具函数 │ └── storage.js # SQLite数据库存储实现 ├── tests/ # 测试文件目录 ├── uploads/ # 上传文件存储目录 ├── wardrobe.db # SQLite数据库文件 ├── index.js # 服务入口文件 ├── package.json # 项目配置文件 └── README.md # 项目说明文档 ``` ## 数据模型 ### 衣物 (Clothing) ```javascript { id: "string", // 唯一标识符 name: "string", // 衣物名称 type: "top" | "pants" | "shoes" | "accessory" | "other", // 衣物类型 image: "string", // 图片(base64编码或图片URL) category: "string", // 分类 season: ["spring", "summer", "autumn", "winter"], // 适用季节 colors: ["string"], // 颜色 tags: ["string"], // 标签 createdAt: "string", // 创建时间(ISO 8601格式) updatedAt: "string" // 更新时间(ISO 8601格式) } ``` ### 穿搭 (Outfit) ```javascript { id: "string", // 唯一标识符 name: "string", // 穿搭名称 clothingIds: ["string"], // 包含的衣物ID列表 layout: { // 布局信息 "clothingId1": { x: 0, // X坐标 y: 0, // Y坐标 width: 100, // 宽度 height: 100, // 高度 zIndex: 1 // 层级 } }, createdAt: "string", // 创建时间(ISO 8601格式) updatedAt: "string" // 更新时间(ISO 8601格式) } ``` ### 上传图片响应 ```javascript { id: "string", // 唯一标识符 url: "string", // 上传后的图片URL name: "string", // 图片文件名 size: 1024, // 图片大小(字节) type: "image/jpeg" // 图片类型 } ``` ## API接口文档 ### 1. 衣物管理接口 #### 获取所有衣物 - **接口路径**:`GET /api/clothing` - **请求参数**:无 - **响应格式**: ```json { "code": 200, "data": [ { "id": "string", "name": "string", "type": "top", "image": "string", "category": "string", "season": ["spring", "autumn"], "colors": ["red", "blue"], "tags": ["casual", "formal"], "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" } ], "message": "success" } ``` #### 添加新衣物 - **接口路径**:`POST /api/clothing` - **请求体**: ```json { "name": "string", "type": "top", "image": "string", "category": "string", "season": ["spring", "autumn"], "colors": ["red", "blue"], "tags": ["casual", "formal"] } ``` - **响应格式**: ```json { "code": 200, "data": { "id": "string", "name": "string", "type": "top", "image": "string", "category": "string", "season": ["spring", "autumn"], "colors": ["red", "blue"], "tags": ["casual", "formal"], "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" }, "message": "success" } ``` #### 更新衣物信息 - **接口路径**:`PUT /api/clothing/:id` - **请求参数**: - `id`: 衣物ID - **请求体**: ```json { "name": "string", "type": "top", "image": "string", "category": "string", "season": ["spring", "autumn"], "colors": ["red", "blue"], "tags": ["casual", "formal"] } ``` - **响应格式**: ```json { "code": 200, "data": { "id": "string", "name": "string", "type": "top", "image": "string", "category": "string", "season": ["spring", "autumn"], "colors": ["red", "blue"], "tags": ["casual", "formal"], "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" }, "message": "success" } ``` #### 删除衣物 - **接口路径**:`DELETE /api/clothing/:id` - **请求参数**: - `id`: 衣物ID - **响应格式**: ```json { "code": 200, "data": null, "message": "success" } ``` ### 2. 穿搭管理接口 #### 获取所有穿搭 - **接口路径**:`GET /api/outfits` - **请求参数**:无 - **响应格式**: ```json { "code": 200, "data": [ { "id": "string", "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } }, "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" } ], "message": "success" } ``` #### 添加新穿搭 - **接口路径**:`POST /api/outfits` - **请求体**: ```json { "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } } } ``` - **响应格式**: ```json { "code": 200, "data": { "id": "string", "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } }, "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" }, "message": "success" } ``` #### 更新穿搭信息 - **接口路径**:`PUT /api/outfits/:id` - **请求参数**: - `id`: 穿搭ID - **请求体**: ```json { "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } } } ``` - **响应格式**: ```json { "code": 200, "data": { "id": "string", "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } }, "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" }, "message": "success" } ``` #### 更新穿搭布局 - **接口路径**:`PUT /api/outfits/:id/layout` - **请求参数**: - `id`: 穿搭ID - **请求体**: ```json { "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } } } ``` - **响应格式**: ```json { "code": 200, "data": { "id": "string", "name": "string", "clothingIds": ["string", "string"], "layout": { "clothingId1": { "x": 0, "y": 0, "width": 100, "height": 100, "zIndex": 1 } }, "createdAt": "2024-01-01T00:00:00Z", "updatedAt": "2024-01-01T00:00:00Z" }, "message": "success" } ``` #### 删除穿搭 - **接口路径**:`DELETE /api/outfits/:id` - **请求参数**: - `id`: 穿搭ID - **响应格式**: ```json { "code": 200, "data": null, "message": "success" } ``` ### 3. 图片上传接口 #### 上传图片(支持多图) - **接口路径**:`POST /api/upload` - **请求方式**:multipart/form-data - **请求参数**: - `files`: 图片文件数组 - **响应格式**: ```json { "code": 200, "data": [ { "id": "string", "url": "/uploads/20240101-123456.jpg", // 上传后的图片URL "name": "example.jpg", // 图片文件名 "size": 1024, // 图片大小(字节) "type": "image/jpeg" // 图片类型 } ], "message": "success" } ``` ### 4. 健康检查接口 #### 检查服务状态 - **接口路径**:`GET /health` - **请求参数**:无 - **响应格式**: ```json { "code": 200, "data": null, "message": "success" } ``` ## 开发指南 ### 安装依赖 ```bash npm install ``` ### 启动开发服务器 ```bash npm run dev # 或 npm start ``` ### 服务运行地址 服务默认运行在 `http://localhost:3000` ### 接口调用示例 #### 1. 获取所有衣物 ```javascript // 使用fetch API fetch('http://localhost:3000/api/clothing') .then(response => response.json()) .then(data => { if (data.code === 200) { console.log('衣物列表:', data.data); } }); ``` #### 2. 添加新衣物 ```javascript // 使用fetch API fetch('http://localhost:3000/api/clothing', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name: '蓝色衬衫', type: 'top', image: 'base64编码的图片数据', category: '上装', season: ['spring', 'autumn'], colors: ['blue'], tags: ['casual', 'office'] }) }) .then(response => response.json()) .then(data => { if (data.code === 200) { console.log('创建成功:', data.data); } }); ``` #### 3. 上传图片 ```javascript // 使用fetch API const formData = new FormData(); formData.append('files', fileInput.files[0]); fetch('http://localhost:3000/api/upload', { method: 'POST', body: formData }) .then(response => response.json()) .then(data => { if (data.code === 200) { console.log('上传成功:', data.data); } }); ``` ## 注意事项 1. **接口响应格式**:所有接口统一返回 `{ code, data, message }` 格式的JSON数据 2. **文件上传限制**:图片上传大小限制为5MB,只允许上传图片文件 3. **数据存储**:使用SQLite数据库存储,服务重启后数据仍然存在 4. **跨域支持**:已配置CORS,支持前端跨域调用 5. **时间格式**:所有时间字段使用ISO 8601格式(如:2024-01-01T00:00:00Z) 6. **衣物类型**:必须使用预定义的类型值之一 7. **季节字段**:必须使用预定义的季节值数组 8. **测试文件存放**:所有测试相关的文件(如测试脚本、测试数据等)必须存放在 `tests/` 目录中,不得直接存放在根目录 ## 测试文件管理 ### 测试文件存放规则 为了保持项目结构的整洁和一致性,所有测试相关的文件必须遵循以下规则: 1. **所有测试文件必须存放在 `tests/` 目录中** 2. **测试脚本命名规范**:使用 `test-` 前缀命名测试脚本文件(如 `test-clothing.js`) 3. **测试数据文件**:测试所需的数据文件也应存放在 `tests/` 目录中 4. **工具脚本**:与测试相关的工具脚本(如数据库检查、数据查看等)也应存放在 `tests/` 目录中 ### 测试目录结构示例 ``` tests/ ├── test-clothing.js # 衣物相关测试脚本 ├── test-outfits.js # 穿搭相关测试脚本 ├── test-upload.js # 文件上传测试脚本 ├── test-data/ # 测试数据目录 │ └── sample-images/ # 测试用图片 └── db-tools/ # 数据库工具脚本 ├── check-db.js # 数据库检查工具 └── view-db.js # 数据库查看工具 ``` ### 执行测试 ```bash # 运行特定测试脚本 node tests/test-clothing.js # 运行所有测试脚本 for file in tests/test-*.js; do node "$file"; done ``` ## 后续优化方向 1. **数据库升级**:可考虑升级为MongoDB或MySQL等更强大的数据库 2. **用户认证**:添加用户认证和授权机制 3. **图片处理**:添加图片压缩和尺寸调整功能 4. **API文档**:集成Swagger或Postman文档 5. **错误处理**:增强错误处理和日志记录 6. **性能优化**:添加缓存机制和请求限流 7. **部署方案**:提供Docker部署配置 ## 故障排查 ### 常见问题 1. **服务启动失败**:检查端口3000是否被占用 2. **图片上传失败**:检查文件大小是否超过限制,文件类型是否正确 3. **接口调用失败**:检查请求方法、路径和参数是否正确 4. **数据持久化**:使用SQLite数据库存储,服务重启后数据仍然存在 ### 日志查看 服务启动后,控制台会输出运行日志,可通过查看日志定位问题。 ## 联系方式 如有任何问题或建议,请联系项目维护人员。