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
@@ -25,6 +25,7 @@ func NewSmokeLogService(db *gorm.DB) *SmokeLogService {
type CreateSmokeLogRequest struct {
SmokeTime *time.Time
SmokeAt *time.Time
Remark string
Level int64
Num int
@@ -44,7 +45,14 @@ func (s *SmokeLogService) Create(ctx context.Context, uid int, req CreateSmokeLo
num = 1
}
smokeAt := req.SmokeAt
// smokeTime 用于“按天筛选”;如果传了 smokeAt,建议用其日期部分回填 smokeTime,避免出现不一致。
smokeTime := req.SmokeTime
if smokeAt != nil {
day := time.Date(smokeAt.Year(), smokeAt.Month(), smokeAt.Day(), 0, 0, 0, 0, smokeAt.Location())
smokeTime = &day
}
if smokeTime == nil {
today := time.Now()
startOfDay := time.Date(today.Year(), today.Month(), today.Day(), 0, 0, 0, 0, today.Location())
@@ -54,6 +62,7 @@ func (s *SmokeLogService) Create(ctx context.Context, uid int, req CreateSmokeLo
record := smokemodel.SmokeLog{
UID: uid,
SmokeTime: smokeTime,
SmokeAt: smokeAt,
Remark: req.Remark,
CreateTime: &createTime,
UpdateTime: &updateTime,
@@ -148,6 +157,11 @@ type UpdateSmokeLogRequest struct {
// - true:前端传了 smoke_time(可以设置为具体日期,也可以清空为 NULL)
SmokeTimeProvided bool
SmokeTime *time.Time
// SmokeAtProvided 用于区分:
// - false:前端没传 smoke_at(不修改)
// - true:前端传了 smoke_at(可以设置为具体时间,也可以清空为 NULL)
SmokeAtProvided bool
SmokeAt *time.Time
Remark *string
Level *int64
Num *int
@@ -163,6 +177,13 @@ func (s *SmokeLogService) Update(ctx context.Context, uid int, id int, req Updat
if req.SmokeTimeProvided {
updates["smoke_time"] = req.SmokeTime
}
if req.SmokeAtProvided {
updates["smoke_at"] = req.SmokeAt
if req.SmokeAt != nil {
day := time.Date(req.SmokeAt.Year(), req.SmokeAt.Month(), req.SmokeAt.Day(), 0, 0, 0, 0, req.SmokeAt.Location())
updates["smoke_time"] = &day
}
}
if req.Remark != nil {
updates["remark"] = *req.Remark
}