Enhance AI and Redis integration for smoke logging features

- Added AI configuration options to .env.example and config.go for OpenAI integration.
- Implemented Redis caching for session management in main.go and auth middleware.
- Updated smoke logging service to support real smoking time (`smoke_at`) and AI advice retrieval.
- Enhanced API routes to include endpoints for AI advice and unlock functionality for non-members.
- Improved database schema with new tables for AI advice and unlock records.
- Expanded documentation to cover new AI features and Redis caching implementation.
This commit is contained in:
nepiedg
2026-01-03 02:14:21 +00:00
parent 1c48fbdeaf
commit 16844d4a42
30 changed files with 1662 additions and 9 deletions
+54 -1
View File
@@ -12,6 +12,7 @@
```json
{
"smoke_time": "2025-12-31",
"smoke_at": "2025-12-31 08:30:00",
"remark": "压力大",
"level": 2,
"num": 3
@@ -20,6 +21,7 @@
说明:
- `smoke_time` 可选;不传则默认“当天”。
- `smoke_at` 可选;真实抽烟时间(格式 `YYYY-MM-DD HH:MM:SS`)。用于“按时间节点分析/AI 建议”;不传则可用 `createtime` 近似。
- `level/num` 可选;不传或传 `<=0` 时会按 `1` 处理。
curl 示例:
@@ -28,7 +30,7 @@ curl 示例:
curl -X POST 'http://127.0.0.1:8080/api/v1/smoke/logs' \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer wx-session-key' \
-d '{"smoke_time":"2025-12-31","remark":"压力大","level":2,"num":3}'
-d '{"smoke_time":"2025-12-31","smoke_at":"2025-12-31 08:30:00","remark":"压力大","level":2,"num":3}'
```
成功响应示例(字段以实际为准):
@@ -40,6 +42,7 @@ curl -X POST 'http://127.0.0.1:8080/api/v1/smoke/logs' \
"data": {
"id": 5202,
"smoke_time": "2025-12-31T00:00:00+08:00",
"smoke_at": "2025-12-31T08:30:00+08:00",
"remark": "压力大",
"createtime": 1735600000,
"updatetime": 1735600000,
@@ -94,6 +97,7 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
```json
{
"smoke_time": "2026-01-01",
"smoke_at": "2026-01-01 21:10:00",
"remark": "聚会",
"level": 3,
"num": 1
@@ -102,6 +106,7 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
注意:
- 如果你想“清空 smoke_time”,请传空字符串:`{"smoke_time":""}`
- 如果你想“清空 smoke_at”,请传空字符串:`{"smoke_at":""}`
- 如果传 `null` 或者不传 `smoke_time`,后端会认为你没有修改该字段。
## 5) 删除记录(软删除)
@@ -120,3 +125,51 @@ curl -X GET 'http://127.0.0.1:8080/api/v1/smoke/logs/5202' \
}
```
## 6) 获取 AI 戒烟建议(会员 + 广告解锁并行)
`GET /api/v1/smoke/ai/advice?date=2026-01-02`
说明:
- `date` 可选,默认“昨天”(建议针对哪一天的数据)。
- 权限:会员用户直接可用;非会员需要先对该 `date` 完成“看广告解锁”(见下一个接口)。
- 建议结果会按 `uid + date + prompt_version` 缓存(表:`fa_smoke_ai_advice`)。
未满足权限时的建议响应(示例):
```json
{
"code": 403,
"message": "需要会员或观看广告解锁后才可生成建议",
"data": {
"date": "2026-01-02",
"need": "vip_or_ad"
}
}
```
成功响应(示例):
```json
{
"code": 200,
"message": "success",
"data": {
"date": "2026-01-02",
"advice": "..."
}
}
```
## 7) 看广告解锁(用于非会员)
`POST /api/v1/smoke/ai/advice_unlocks`
请求体:
```json
{
"date": "2026-01-02",
"ad_watched_at": "2026-01-03 09:00:00"
}
```
说明:
- 该接口用于记录“已完成观看广告”,落库到 `fa_smoke_ai_advice_unlocks``uid + unlock_date` 唯一)。
- `ad_watched_at` 可由后端取当前时间;如需审计/对账可保留前端上报并做校验。
+32
View File
@@ -15,3 +15,35 @@
- 该表使用旧系统字段:`createtime/updatetime/deletetime`(秒级时间戳),并非 GORM 默认的 `created_at/updated_at/deleted_at`
- 接口层通过 Token 识别用户,`uid` 由后端从登录用户推导,不允许前端传入。
### 真实抽烟时间(推荐使用 `smoke_at`
为支持“按时间节点分析”(例如:昨天哪些时段更容易想抽),建议在 `fa_smoke_log` 中新增:
- `smoke_at`:真实抽烟时间(精确到时分秒,可补录)
数据约定建议:
- 前端若提供了真实时间:写入 `smoke_at`;并同步写 `smoke_time = date(smoke_at)`,方便沿用现有按天筛选。
- 前端未提供真实时间:`smoke_at=NULL`,时间节点可用 `createtime` 作为近似(但补录会造成偏差)。
### AI 戒烟建议(会员 + 广告解锁并行)
面向会员用户提供“昨日 AI 建议”;非会员用户在完成当日/指定日期的“看广告解锁”后也可生成建议。
涉及表(DDL 见:`docs/sql/smoke.sql`):
- `fa_smoke_ai_advice`:按 `uid + advice_date + prompt_version` 缓存建议结果,避免重复调用 AI。
- `fa_smoke_ai_advice_unlocks`:非会员用户的“每日解锁”记录(按 `uid + unlock_date` 唯一)。
建议的权限判断顺序:
1) 若用户是会员:直接允许生成/获取建议。
2) 否则:查询 `fa_smoke_ai_advice_unlocks` 是否已对 `unlock_date=昨天(或请求 date` 解锁;已解锁则允许。
3) 否则:拒绝并提示“开通会员或观看广告解锁”。
会员判断建议使用通用会员表:
- `user_memberships`DDL 见:`docs/sql/users.sql`
无支付系统时,可用兑换码开通会员:
- API`docs/membership/API.md`
- DDL`docs/sql/membership.sql`
给 AI 的输入(最小必需)建议包含:
- 昨日总量:`SUM(num)`
- 时间节点列表:按 `COALESCE(smoke_at, FROM_UNIXTIME(createtime))` 排序的多条记录(每条带 `num/level/remark`