Add user profile management for smoking data

- Introduced new API endpoints `GET /api/v1/smoke/profile` and `PUT /api/v1/smoke/profile` for retrieving and updating user smoking profiles.
- Added a new database table `fa_smoke_user_profile` to store user-specific smoking data, including daily smoking habits and motivations.
- Updated the smoke handler and service to integrate user profile data into AI advice generation.
- Enhanced documentation to reflect the new user profile features and their usage.
This commit is contained in:
nepiedg
2026-01-20 02:37:20 +00:00
parent 0b26ba6d35
commit dc54c4e934
11 changed files with 703 additions and 5 deletions
+84
View File
@@ -237,3 +237,87 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
说明:
- 该接口用于记录“已完成观看广告”,落库到 `fa_smoke_ai_advice_unlocks``uid + unlock_date` 唯一)。
- `ad_watched_at` 可由后端取当前时间;如需审计/对账可保留前端上报并做校验。
## 10) 获取用户基础信息(首次进入:判断是否需要补全)
`GET /api/v1/smoke/profile`
说明:
- 首次进入小程序建议先调用该接口:若 `exists=false``is_completed=false`,前端进入“信息补全”流程。
- `baseline_interval_minutes` 用于建立初始基准:在用户清醒时段内的“平均间隔(分钟)”。计算:`awake_minutes / baseline_cigs_per_day`
- 若未提供作息时间(起床/入睡),后端会用默认清醒时长 `16*60=960` 分钟参与计算。
成功响应(示例):
```json
{
"code": 200,
"message": "success",
"data": {
"exists": true,
"profile": {
"id": 1,
"created_at": "2026-01-05T10:00:00+08:00",
"updated_at": "2026-01-05T10:00:00+08:00",
"baseline_cigs_per_day": 20,
"smoking_years": 8,
"pack_price_cent": 2500,
"smoke_motivations": ["压力大", "社交"],
"quit_motivations": ["身体健康", "省钱"],
"wake_up_time": "07:30",
"sleep_time": "23:30",
"onboarding_completed_at": "2026-01-05T10:00:00+08:00"
},
"is_completed": true,
"awake_minutes": 960,
"baseline_interval_minutes": 48
}
}
```
`exists=false`(尚未补全)时,响应示例:
```json
{
"code": 200,
"message": "success",
"data": {
"exists": false,
"is_completed": false,
"awake_minutes": 960,
"baseline_interval_minutes": 0
}
}
```
字段用途(补全页面可参考):
- `baseline_cigs_per_day`(基础烟量/日均抽烟支数):建立初始基准,计算初始建议间隔时长。
- `smoking_years`(烟龄/年)+ `pack_price_cent`(单包价格/分):用于看板计算“已省金额”和“恢复时长”等指标(公式可在看板端实现)。
- `smoke_motivations`(抽烟动机):如 `压力大/无聊/社交/提神`,用于 AI 在分析 remark 时更有针对性。
- `quit_motivations`(戒烟动力):如 `身体健康/家人孩子/省钱`,当用户产生动摇时 AI 可用这些信息做“情感阻断/自我提醒”。
- `wake_up_time` + `sleep_time`(作息时间):用于自动规避睡眠时间,防止在用户睡觉时提醒其“坚持”。
## 11) 补全/更新用户基础信息(Upsert)
`PUT /api/v1/smoke/profile`
说明:
- 字段按需传;首次进入建议一次性补全。
- 作息时间格式:`HH:MM`24 小时制),例如 `07:30``23:10`
- `pack_price_cent` 为“分”;若前端用“元”,请乘以 100。
请求体(示例):
```json
{
"baseline_cigs_per_day": 20,
"smoking_years": 8,
"pack_price_cent": 2500,
"smoke_motivations": ["压力大", "社交"],
"quit_motivations": ["身体健康", "省钱"],
"wake_up_time": "07:30",
"sleep_time": "23:30"
}
```
成功响应:同 `GET /api/v1/smoke/profile`(返回最新 `profile` + `is_completed` + `baseline_interval_minutes`)。
+10
View File
@@ -15,6 +15,16 @@
- 该表使用旧系统字段:`createtime/updatetime/deletetime`(秒级时间戳),并非 GORM 默认的 `created_at/updated_at/deleted_at`
- 接口层通过 Token 识别用户,`uid` 由后端从登录用户推导,不允许前端传入。
### 用户基础信息(首次进入补全)
用于建立“初始基准/个性化策略”的用户信息表:
- `fa_smoke_user_profile`DDL 见:`docs/sql/smoke.sql`
典型字段:
- 日均抽烟支数、烟龄、单包价格
- 抽烟动机、戒烟动力
- 起床/入睡时间(用于规避睡眠时间)
### 真实抽烟时间(推荐使用 `smoke_at`
为支持“按时间节点分析”(例如:昨天哪些时段更容易想抽),建议在 `fa_smoke_log` 中新增:
+23
View File
@@ -54,3 +54,26 @@ CREATE TABLE IF NOT EXISTS `fa_smoke_ai_advice_unlocks` (
UNIQUE KEY `uniq_smoke_ai_unlock` (`uid`,`unlock_date`),
KEY `idx_smoke_ai_unlock_uid_date` (`uid`,`unlock_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='AI戒烟建议-广告解锁';
-- 用户基础信息(首次进入补全,用于基准/AI/看板公式等)
-- 说明:
-- - 使用 GORM 默认 created_at/updated_at/deleted_atdatetime(3)
-- - smoke_motivations/quit_motivations 建议存 JSON 数组(例如 ["压力大","社交"]
CREATE TABLE IF NOT EXISTS `fa_smoke_user_profile` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`created_at` datetime(3) NULL DEFAULT NULL,
`updated_at` datetime(3) NULL DEFAULT NULL,
`deleted_at` datetime(3) NULL DEFAULT NULL,
`uid` int NOT NULL COMMENT '用户ID',
`baseline_cigs_per_day` int NOT NULL DEFAULT 0 COMMENT '基础烟量(日均抽烟支数)',
`smoking_years` decimal(6,2) NOT NULL DEFAULT 0.00 COMMENT '烟龄(年)',
`pack_price_cent` int NOT NULL DEFAULT 0 COMMENT '单包价格(分)',
`smoke_motivations` json DEFAULT NULL COMMENT '抽烟动机(JSON数组)',
`quit_motivations` json DEFAULT NULL COMMENT '戒烟动力(JSON数组)',
`wake_up_time` varchar(5) NOT NULL DEFAULT '' COMMENT '起床时间(HH:MM)',
`sleep_time` varchar(5) NOT NULL DEFAULT '' COMMENT '入睡时间(HH:MM)',
`onboarding_completed_at` datetime(3) DEFAULT NULL COMMENT '首次补全完成时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_profile_uid` (`uid`),
KEY `idx_smoke_profile_deleted_at` (`deleted_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='戒烟-用户基础信息';