Baby Care Log Backend
运行
# Windows PowerShell
./mvnw spring-boot:run
服务默认地址:http://localhost:8080/api
主要接口
GET /api/healthPOST /api/auth/register - 用户注册POST /api/auth/login - 用户登录POST /api/auth/logout - 用户登出GET /api/auth/me - 获取用户信息PUT /api/auth/profile - 更新用户个人信息POST /api/records/feedingPOST /api/records/diaperPOST /api/records/sleepPOST /api/records/cryPOST /api/records/by-datePOST /api/timeline/by-datePOST /api/stats/overviewPOST /api/stats/trendsGET /api/auth/captcha/imagePOST /api/family/create - 创建家庭GET /api/family/{id} - 获取家庭信息POST /api/family/{id}/settings - 更新家庭设置POST /api/family/{familyId}/baby - 新增/更新宝宝档案GET /api/family/{familyId}/babies - 查询家庭下的所有宝宝DELETE /api/family/{familyId}/baby/{babyId} - 删除宝宝POST /api/family/{familyId}/invite - 发送家庭邀请POST /api/family/invite/{inviteCode}/accept - 接受家庭邀请GET /api/family/{familyId}/members - 查询家庭成员DELETE /api/family/{familyId}/members/{memberId} - 移除家庭成员POST /api/media/upload - 上传媒体文件GET /api/media/list - 查询媒体列表DELETE /api/media/{id} - 删除媒体文件示例请求
{
"babyId": 1,
"feedingType": "FORMULA",
"formulaMl": 150
}
{
"babyId": 1,
"date": "2025-10-03"
}
用户认证接口
GET /api/auth/captcha/image{
"success": true,
"image": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAA...",
"token": "captcha_token_123456789",
"expires": 300
}
POST /api/auth/register{
"account": "13800138000",
"password": "password123",
"nickname": "爸爸",
"captcha": "A3B7",
"captchaToken": "captcha_token_123456789"
}
{
"token": "jwt_token_string",
"user": {
"id": 1,
"familyId": 1,
"role": "PARENT",
"nickname": "爸爸"
}
}
错误响应:
{"error": "验证码错误或已过期,请重新获取", "errorType": "CAPTCHA_INVALID"}{"error": "账号已存在", "errorType": "REGISTER_FAILED"}用户登录
POST /api/auth/login{
"account": "13800138000",
"password": "password123",
"captcha": "A3B7",
"captchaToken": "captcha_token_123456789"
}
成功响应:200(同注册接口)
错误响应:
{"error": "验证码错误或已过期,请重新获取", "errorType": "CAPTCHA_INVALID"}{"error": "账号或密码错误", "errorType": "LOGIN_FAILED"}用户登出
POST /api/auth/logoutAuthorization: Bearer <token>{
"message": "登出成功"
}
GET /api/auth/meAuthorization: Bearer <token>{
"user": {
"id": 1,
"familyId": 1,
"role": "PARENT",
"nickname": "爸爸",
"account": "13800138000"
},
"family": {
"id": 1,
"name": "张家的家庭",
"babies": [
{
"id": 1,
"nickname": "宝宝",
"birthday": "2023-01-01",
"gender": "M",
"avatarUrl": "https://example.com/avatar.jpg"
}
]
}
}
PUT /api/user/profileAuthorization: Bearer <token>{
"nickname": "新昵称"
}
{
"user": {
"id": 1,
"familyId": 1,
"role": "PARENT",
"nickname": "新昵称",
"account": "13800138000"
}
}
数据库
jdbc:mysql://106.12.220.77:12606/babycare?createDatabaseIfNotExist=true&useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTCroot/di&@Hv^23(可在 application.yml 修改)create-drop(开发环境,每次启动会重新创建表)说明
SampleDataLoader)完整接口说明
通用说明
/apiapplication/json2025-10-03T10:30:00ZAuthorization: Bearer <token>健康检查
GET /api/health{
"status": "UP",
"service": "baby-care-log-backend"
}
用户认证接口
POST /api/auth/register{
"account": "13800138000",
"password": "password123",
"nickname": "爸爸"
}
{
"token": "jwt_token_string",
"user": {
"id": 1,
"familyId": 1,
"role": "PARENT",
"nickname": "爸爸"
}
}
POST /api/auth/login{
"account": "13800138000",
"password": "password123"
}
记录创建接口
POST /api/records/feedingAuthorization: Bearer <token>{
"babyId": 1,
"eventTime": "2025-10-03T10:30:00Z",
"feedingType": "FORMULA",
"leftMinutes": 10,
"rightMinutes": 12,
"formulaMl": 150,
"note": "上午加餐"
}
feedingType 可选 BREAST_MILK 或 FORMULA;母乳时使用 leftMinutes/rightMinutes,配方奶使用 formulaMl。babyId 必须属于当前用户的家庭。POST /api/records/diaperAuthorization: Bearer <token>{
"babyId": 1,
"eventTime": "2025-10-03T09:20:00Z",
"diaperType": "WET",
"note": "清尿"
}
diaperType 可选 WET, DIRTY, MIXED。babyId 必须属于当前用户的家庭。POST /api/records/sleepAuthorization: Bearer <token>{
"babyId": 1,
"sleepStart": "2025-10-03T01:00:00Z",
"sleepEnd": "2025-10-03T02:15:00Z",
"note": "夜醒一次"
}
sleepEnd 可为空(表示仍在睡眠中)。服务会将 sleepStart 同步写入事件时间。babyId 必须属于当前用户的家庭。POST /api/records/cryAuthorization: Bearer <token>{
"babyId": 1,
"startTime": "2025-10-03T03:00:00Z",
"endTime": "2025-10-03T03:05:00Z",
"reasons": ["HUNGRY"],
"sootheWays": ["FEED"],
"note": "吃完好了"
}
reasons 可选 HUNGRY,SLEEPY,DIAPER,COMFORT,STOMACH,TOO_HOT,TOO_COLD,OTHER;sootheWays 可选 FEED,HUG,CHANGE_DIAPER,PACIFIER,PLAY,OTHER。babyId 必须属于当前用户的家庭。记录编辑接口
PUT /api/records/feeding/{id}Authorization: Bearer <token>{
"babyId": 1,
"eventTime": "2025-10-03T10:30:00Z",
"feedingType": "FORMULA",
"formulaMl": 200,
"note": "更新后的备注"
}
PUT /api/records/diaper/{id}Authorization: Bearer <token>{
"babyId": 1,
"eventTime": "2025-10-03T09:20:00Z",
"diaperType": "MIXED",
"note": "更新后的备注"
}
PUT /api/records/sleep/{id}Authorization: Bearer <token>{
"babyId": 1,
"sleepStart": "2025-10-03T01:00:00Z",
"sleepEnd": "2025-10-03T02:15:00Z",
"note": "更新后的备注"
}
PUT /api/records/cry/{id}Authorization: Bearer <token>{
"babyId": 1,
"startTime": "2025-10-03T03:00:00Z",
"endTime": "2025-10-03T03:05:00Z",
"reasons": ["HUNGRY"],
"sootheWays": ["FEED"],
"note": "更新后的备注"
}
记录删除接口
DELETE /api/records/feeding/{id}Authorization: Bearer <token>{
"success": true,
"message": "Feeding record deleted successfully"
}
DELETE /api/records/diaper/{id}Authorization: Bearer <token>DELETE /api/records/sleep/{id}Authorization: Bearer <token>DELETE /api/records/cry/{id}Authorization: Bearer <token>记录编辑删除通用错误处理
AI智能对话接口
POST /api/ai/chat/streamAuthorization: Bearer <token>{
"userInput": "为什么宝宝不饿也没拉粑粑总是哭"
}
event: message data: {"content":"三个月","isFinish":false,"chatId":1} event: message data: {"content":"大的宝宝不饿也没拉粑粑却总是","isFinish":false,"chatId":1} event: message data: {"content":"哭,可能有以下一些原因。","isFinish":true,"chatId":1}
GET /api/ai/chat/history?page=0&size=10Authorization: Bearer <token>page: 页码(从0开始,默认0)size: 每页大小(默认10){
"chats": [
{
"id": 1,
"babyInfo": "宝宝信息:小明,男,出生日期:2024-10-03,当前年龄:3个月\n今日动态:\n- 喂奶记录:5次,配方奶300ml,母乳120分钟\n- 尿布记录:8次(湿5次,脏3次)\n- 睡眠记录:3次,总睡眠480分钟\n- 哭闹记录:2次,总哭闹15分钟",
"userInput": "为什么宝宝不饿也没拉粑粑总是哭",
"aiResponse": "三个月大的宝宝不饿也没拉粑粑却总是哭,可能有以下一些原因...",
"createdAt": "2025-01-03T10:30:00Z"
}
],
"totalElements": 25,
"totalPages": 3,
"currentPage": 0,
"size": 10
}
GET /api/ai/chat/search?babyInfo=三个月Authorization: Bearer <token>babyInfo: 宝宝信息关键词GET /api/ai/chat/{chatId}Authorization: Bearer <token>DELETE /api/ai/chat/{chatId}Authorization: Bearer <token>{
"success": true,
"message": "对话记录删除成功"
}
AI对话错误处理
按日期聚合查询(分类型返回)
POST /api/records/by-dateAuthorization: Bearer <token>{
"babyId": 1,
"date": "2025-10-03"
}
babyId 必须属于当前用户的家庭{
"feedings": [ { "id": 10, "eventTime": "2025-10-03T10:30:00Z", "feedingType": "FORMULA", "formulaMl": 150 } ],
"diapers": [ { "id": 11, "eventTime": "2025-10-03T09:20:00Z", "diaperType": "WET" } ],
"sleeps": [ { "id": 12, "eventTime": "2025-10-03T01:00:00Z", "sleepStart": "2025-10-03T01:00:00Z", "sleepEnd": "2025-10-03T02:15:00Z" } ],
"cries": [ { "id": 13, "eventTime": "2025-10-03T03:00:00Z", "startTime": "2025-10-03T03:00:00Z", "endTime": "2025-10-03T03:05:00Z" } ]
}
时光轴(跨类型合并排序)
POST /api/timeline/by-dateAuthorization: Bearer <token>records/by-dateeventTime 倒序,type 指示类型):[
{ "type": "FEEDING", "eventTime": "2025-10-03T10:30:00Z", "data": { "id": 10, "feedingType": "FORMULA", "formulaMl": 150 } },
{ "type": "DIAPER", "eventTime": "2025-10-03T09:20:00Z", "data": { "id": 11, "diaperType": "WET" } },
{ "type": "CRY", "eventTime": "2025-10-03T03:00:00Z", "data": { "id": 13, "startTime": "2025-10-03T03:00:00Z" } },
{ "type": "SLEEP", "eventTime": "2025-10-03T01:00:00Z", "data": { "id": 12, "sleepStart": "2025-10-03T01:00:00Z" } }
]
统计概览
POST /api/stats/overviewAuthorization: Bearer <token>{
"babyId": 1,
"startDate": "2025-10-01",
"endDate": "2025-10-07"
}
babyId 必须属于当前用户的家庭{
"totalFormulaMl": 900,
"totalBreastMinutes": 120,
"totalSleepMinutes": 380
}
趋势分析
POST /api/stats/trendsAuthorization: Bearer <token>{
"babyId": 1,
"startDate": "2025-10-01",
"endDate": "2025-10-07",
"type": "WEEKLY"
}
babyId 必须属于当前用户的家庭,type 支持 WEEKLY(周趋势)和 MONTHLY(月趋势){
"period": "2025-10-01 to 2025-10-07",
"dailyStats": [
{
"date": "2025-10-01",
"feedings": {
"count": 6,
"totalFormulaMl": 450,
"totalBreastMinutes": 60
},
"diapers": {
"count": 8,
"wetCount": 5,
"dirtyCount": 3
},
"sleeps": {
"count": 3,
"totalMinutes": 480
},
"cries": {
"count": 4,
"totalMinutes": 20
}
},
{
"date": "2025-10-02",
"feedings": {
"count": 7,
"totalFormulaMl": 500,
"totalBreastMinutes": 80
},
"diapers": {
"count": 9,
"wetCount": 6,
"dirtyCount": 3
},
"sleeps": {
"count": 4,
"totalMinutes": 520
},
"cries": {
"count": 3,
"totalMinutes": 15
}
}
],
"averages": {
"dailyFeedings": 6.5,
"dailyFormulaMl": 475,
"dailyBreastMinutes": 70,
"dailyDiapers": 8.5,
"dailySleepMinutes": 500,
"dailyCryMinutes": 17.5
}
}
状态码与错误
家庭管理接口
POST /api/family/createAuthorization: Bearer <token>{
"name": "我的家庭"
}
{
"success": true,
"message": "家庭创建成功",
"family": {
"id": 1,
"name": "我的家庭",
"babies": []
}
}
错误响应:401(未授权)、400(家庭名称为空)
删除宝宝
DELETE /api/family/{familyId}/baby/{babyId}Authorization: Bearer <token>发送家庭邀请
POST /api/family/{familyId}/inviteAuthorization: Bearer <token>{
"email": "family@example.com",
"role": "PARENT",
"message": "邀请您加入我们的家庭"
}
{
"inviteCode": "abc123def456",
"expiresAt": "2025-10-10T10:30:00Z"
}
错误响应:401(未授权)、403(无权访问)、409(邀请已存在)
接受家庭邀请
POST /api/family/invite/{inviteCode}/acceptAuthorization: Bearer <token>{
"accept": true
}
{
"success": true,
"message": "成功加入家庭",
"user": {
"id": 1,
"familyId": 1,
"role": "PARENT",
"nickname": "用户"
},
"family": {
"id": 1,
"name": "张家的家庭",
"babies": [
{
"id": 1,
"nickname": "宝宝",
"birthday": "2023-01-01",
"gender": "M",
"avatarUrl": "https://example.com/avatar.jpg"
}
]
}
}
错误响应:401(未授权)、410(邀请已过期或已处理)
查询家庭成员
GET /api/family/{familyId}/membersAuthorization: Bearer <token>[
{
"id": 1,
"nickname": "爸爸",
"role": "PARENT",
"account": "dad@example.com",
"joinedAt": "2025-01-15T08:30:00Z"
}
]
DELETE /api/family/{familyId}/members/{memberId}Authorization: Bearer <token>美好时刻(媒体)接口
POST /api/media/uploadAuthorization: Bearer <token>multipart/form-datababyId:数字,必填(必须属于当前用户的家庭)file:文件,必填(图片/视频,遵循后端上传大小限制)description:字符串,选填MediaAsset 对象(包含 id/objectKey/contentType/description/createdAt)GET /api/media/list?babyId=1Authorization: Bearer <token>MediaAsset[]DELETE /api/media/{id}Authorization: Bearer <token>MinIO 使用说明
application.yml 配置:minio.endpoint/accessKey/secretKey/bucketobjectKey 示例:baby-1/550e8400-e29b-41d4-a716-446655440000/api/media/presign?objectKey=...){endpoint}/{bucket}/{objectKey}(需桶策略允许匿名读)家庭与设置接口
GET /api/family/{id}Authorization: Bearer <token>Family(已屏蔽成员列表的序列化)POST /api/family/{id}/settingsAuthorization: Bearer <token>{
"notifyFeeding": true,
"notifySleep": false,
"notifyDiaper": true
}
FamilySettingPOST /api/family/{familyId}/babyAuthorization: Bearer <token>id):{
"id": 1,
"nickname": "小月亮",
"birthday": "2025-05-01",
"gender": "F",
"avatarUrl": "https://example.com/avatar.jpg"
}
BabyGET /api/family/{familyId}/babiesAuthorization: Bearer <token>Baby[] 数组[
{
"id": 1,
"nickname": "小月亮",
"birthday": "2025-05-01",
"gender": "F",
"avatarUrl": "https://example.com/avatar.jpg",
"createdAt": "2025-01-15T08:30:00Z"
},
{
"id": 2,
"nickname": "小太阳",
"birthday": "2025-03-15",
"gender": "M",
"avatarUrl": null,
"createdAt": "2025-01-16T10:20:00Z"
}
]
注意事项(后端)
@JsonIgnoreProperties 与 @JsonIgnore;若需更严格控制返回数据,请改用 DTO。createDatabaseIfNotExist=true 会自动建库;无建库权限时请手动执行 CREATE DATABASE babycare DEFAULT CHARSET utf8mb4;。Authorization: Bearer <token>。babyId 都会验证权限。create-drop 模式,每次启动会重新创建数据库表。update 或 validate 模式。/api 前缀,与 context-path 配置保持一致。