|
|
3 ماه پیش | |
|---|---|---|
| .. | ||
| database | 4 ماه پیش | |
| scripts | 4 ماه پیش | |
| src | 3 ماه پیش | |
| .gitignore | 4 ماه پیش | |
| README.md | 4 ماه پیش | |
| package.json | 4 ماه پیش | |
| tsconfig.json | 4 ماه پیش | |
数智健调系统后端API,基于Node.js + Express + TypeORM + MySQL开发,为体重管理模块提供完整的RESTful API服务。
backend/
├── database/ # 数据库脚本
│ ├── schema.sql # 表结构定义
│ ├── init.sql # 数据库初始化
│ ├── seed.sql # 测试数据
│ └── README.md # 数据库文档
├── src/ # 源代码
│ ├── config/ # 配置文件
│ │ └── database.ts # 数据库配置
│ ├── controllers/ # 控制器层
│ │ └── WeightController.ts
│ ├── dto/ # 数据传输对象
│ │ ├── weight-record.dto.ts
│ │ ├── weight-goal.dto.ts
│ │ └── tag.dto.ts
│ ├── entities/ # ORM实体模型
│ │ ├── User.ts
│ │ ├── WeightRecord.ts
│ │ ├── WeightGoal.ts
│ │ ├── Tag.ts
│ │ ├── WeightRecordTag.ts
│ │ └── AnomalyLog.ts
│ ├── middlewares/ # 中间件
│ │ ├── auth.middleware.ts
│ │ ├── error.middleware.ts
│ │ └── logger.middleware.ts
│ ├── routes/ # 路由定义
│ │ ├── index.ts
│ │ └── weight.routes.ts
│ ├── services/ # 服务层
│ │ ├── WeightRecordService.ts
│ │ ├── WeightGoalService.ts
│ │ ├── TagService.ts
│ │ └── StatsService.ts
│ └── server.ts # 服务器入口
├── .env.example # 环境变量示例
├── .gitignore # Git忽略文件
├── package.json # 项目依赖
├── tsconfig.json # TypeScript配置
└── README.md # 本文档
cd campus_health_app/backend
npm install
复制环境变量示例文件并修改配置:
cp .env.example .env
编辑 .env 文件,配置数据库连接:
NODE_ENV=development
PORT=3000
API_PREFIX=/api
DB_TYPE=mysql
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=your_password
DB_DATABASE=campus_health
DB_SYNCHRONIZE=false
DB_LOGGING=true
CORS_ORIGIN=http://localhost:4200
# 创建数据库
mysql -u root -p < database/init.sql
# 创建表结构
mysql -u root -p campus_health < database/schema.sql
# (可选)插入测试数据
mysql -u root -p campus_health < database/seed.sql
npm run dev
服务器将在 http://localhost:3000 启动。
# 编译TypeScript
npm run build
# 启动生产服务器
npm start
http://localhost:3000/apix-user-id(临时方案)成功响应:
{
"success": true,
"data": { ... },
"message": "操作成功"
}
错误响应:
{
"success": false,
"message": "错误信息",
"errors": ["详细错误1", "详细错误2"]
}
GET /api/weight/records
Headers:
x-user-id: test-user-001
Query Parameters:
- from: string (YYYY-MM-DD, optional)
- to: string (YYYY-MM-DD, optional)
- condition: string (fasting|after_meal, optional)
- tags: string[] (optional)
- page: number (default: 1)
- limit: number (default: 100)
Response 200:
{
"success": true,
"data": {
"records": [...],
"total": 90,
"page": 1,
"limit": 100
}
}
POST /api/weight/records
Headers:
x-user-id: test-user-001
Content-Type: application/json
Body:
{
"date": "2025-10-22",
"measurementTime": "08:00",
"weight": 65.5,
"bodyFat": 22.3,
"muscleMass": 28.5,
"measurementCondition": "fasting",
"notes": "早晨测量",
"tags": ["开始运动"]
}
Response 201:
{
"success": true,
"data": { ... },
"message": "体重记录添加成功"
}
PUT /api/weight/records/:id
Headers:
x-user-id: test-user-001
Content-Type: application/json
Body:
{
"weight": 65.3,
"notes": "更新备注"
}
Response 200:
{
"success": true,
"data": { ... },
"message": "更新成功"
}
DELETE /api/weight/records/:id
Headers:
x-user-id: test-user-001
Response 200:
{
"success": true,
"message": "删除成功"
}
GET /api/weight/goal
Headers:
x-user-id: test-user-001
Response 200:
{
"success": true,
"data": {
"id": "...",
"targetWeight": 65.0,
"targetBodyFat": 18.0,
"targetDate": "2025-12-31",
...
}
}
POST /api/weight/goal
Headers:
x-user-id: test-user-001
Content-Type: application/json
Body:
{
"targetWeight": 65.0,
"targetBodyFat": 18.0,
"targetDate": "2025-12-31"
}
Response 201:
{
"success": true,
"data": { ... },
"message": "目标设置成功"
}
PUT /api/weight/goal
Headers:
x-user-id: test-user-001
Content-Type: application/json
Body:
{
"targetWeight": 63.0
}
Response 200:
{
"success": true,
"data": { ... },
"message": "目标更新成功"
}
GET /api/weight/tags
Headers:
x-user-id: test-user-001
Response 200:
{
"success": true,
"data": [
{
"id": "...",
"name": "开始运动",
"type": "system",
"color": "#3b82f6"
},
...
]
}
POST /api/weight/tags
Headers:
x-user-id: test-user-001
Content-Type: application/json
Body:
{
"name": "新标签",
"color": "#f59e0b"
}
Response 201:
{
"success": true,
"data": { ... },
"message": "标签创建成功"
}
GET /api/weight/stats
Headers:
x-user-id: test-user-001
Query Parameters:
- from: string (YYYY-MM-DD, optional)
- to: string (YYYY-MM-DD, optional)
Response 200:
{
"success": true,
"data": {
"currentWeight": 65.5,
"weightChange": -4.5,
"daysTracked": 60,
"avgWeeklyChange": -0.75,
"bodyFatChange": -2.8,
"goalProgress": 75.0,
"goalETA": "2025-11-15"
}
}
# 健康检查
curl http://localhost:3000/api/health
# 获取体重记录
curl -H "x-user-id: test-user-001" \
http://localhost:3000/api/weight/records
# 添加体重记录
curl -X POST http://localhost:3000/api/weight/records \
-H "x-user-id: test-user-001" \
-H "Content-Type: application/json" \
-d '{
"date": "2025-10-22",
"weight": 65.5,
"bodyFat": 22.3,
"muscleMass": 28.5
}'
# 获取当前目标
curl -H "x-user-id: test-user-001" \
http://localhost:3000/api/weight/goal
# 获取统计数据
curl -H "x-user-id: test-user-001" \
http://localhost:3000/api/weight/stats
base_url = http://localhost:3000/apix-user-id = test-user-001(待补充)
编译并启动服务
npm run build
npm start
# 安装PM2
npm install -g pm2
# 启动服务
pm2 start dist/server.js --name campus-health-api
# 查看日志
pm2 logs campus-health-api
# 重启服务
pm2 restart campus-health-api
src/dto/ 创建DTOsrc/services/ 创建服务src/controllers/ 创建控制器方法src/routes/ 添加路由# 运行ESLint
npm run lint
# 格式化代码
npm run format
检查 .env 文件中的数据库配置是否正确,确保MySQL服务已启动。
修改 .env 中的 PORT 配置,或停止占用3000端口的进程。
确保 DB_SYNCHRONIZE=false,使用SQL脚本管理数据库结构。
MIT
如有问题或建议,请联系开发团队。