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
+41 -1
View File
@@ -5,12 +5,52 @@ CREATE TABLE `fa_smoke_log` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`smoke_time` date DEFAULT NULL COMMENT '抽烟时间',
`smoke_at` datetime DEFAULT NULL COMMENT '真实抽烟时间(可补录,精确到时分秒;为空则可用 createtime 近似)',
`remark` text COMMENT '抽烟原因',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
`level` bigint(2) DEFAULT '1' COMMENT '烟瘾程度',
`num` int(2) DEFAULT '1' COMMENT '抽烟数量',
PRIMARY KEY (`id`,`uid`)
PRIMARY KEY (`id`,`uid`),
KEY `idx_smoke_uid_date` (`uid`,`smoke_time`),
KEY `idx_smoke_uid_at` (`uid`,`smoke_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='抽烟记录';
-- AI 戒烟建议(按天缓存,避免重复调用;通常 date=昨天)
-- 说明:
-- - access gating:会员用户直接可用;非会员需要先完成“看广告解锁”(见 fa_smoke_ai_advice_unlocks
-- - input_snapshot:建议将“昨日总量 + 时间节点列表”作为 JSON 落库,便于追溯与复现
CREATE TABLE IF NOT EXISTS `fa_smoke_ai_advice` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`advice_date` date NOT NULL COMMENT '建议针对的日期(通常=昨天)',
`prompt_version` varchar(30) NOT NULL DEFAULT 'v1' COMMENT '提示词版本',
`provider` varchar(30) DEFAULT NULL COMMENT 'AI 提供方(可选)',
`model` varchar(60) DEFAULT NULL COMMENT '模型名(可选)',
`input_snapshot` json NOT NULL COMMENT '当次输入快照(昨日总量+节点)',
`advice` mediumtext NOT NULL COMMENT 'AI 建议内容(文本/Markdown',
`tokens_in` int DEFAULT NULL,
`tokens_out` int DEFAULT NULL,
`cost_cent` int DEFAULT NULL COMMENT '成本(分,可选)',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
UNIQUE KEY `uniq_smoke_ai_advice` (`uid`,`advice_date`,`prompt_version`),
KEY `idx_smoke_ai_advice_uid_date` (`uid`,`advice_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='每日AI戒烟建议';
-- 每日广告解锁记录(非会员:当天/指定日期解锁后即可生成建议)
CREATE TABLE IF NOT EXISTS `fa_smoke_ai_advice_unlocks` (
`id` bigint unsigned NOT NULL AUTO_INCREMENT,
`uid` int(11) NOT NULL COMMENT '用户ID',
`unlock_date` date NOT NULL COMMENT '解锁的建议日期(通常=昨天)',
`ad_watched_at` datetime NOT NULL COMMENT '完成观看时间',
`createtime` int(11) DEFAULT NULL COMMENT '创建时间',
`updatetime` int(11) DEFAULT NULL COMMENT '修改时间',
`deletetime` int(11) DEFAULT NULL COMMENT '删除时间',
PRIMARY KEY (`id`),
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戒烟建议-广告解锁';