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
+39 -3
View File
@@ -14,19 +14,26 @@ import (
)
type SmokeHandler struct {
smokeLogService *smokeservice.SmokeLogService
smokeLogService *smokeservice.SmokeLogService
smokeAIAdviceService *smokeservice.SmokeAIAdviceService
}
func NewSmokeHandler(smokeLogService *smokeservice.SmokeLogService) *SmokeHandler {
return &SmokeHandler{smokeLogService: smokeLogService}
func NewSmokeHandler(smokeLogService *smokeservice.SmokeLogService, smokeAIAdviceService *smokeservice.SmokeAIAdviceService) *SmokeHandler {
return &SmokeHandler{
smokeLogService: smokeLogService,
smokeAIAdviceService: smokeAIAdviceService,
}
}
// dateLayout 用于解析前端传入的日期字符串(例如:2025-12-31)
const dateLayout = "2006-01-02"
const dateTimeLayout = "2006-01-02 15:04:05"
type createSmokeLogRequest struct {
// 只记录“日期”即可;如果不传,后端会按当天处理
SmokeTime string `json:"smoke_time"`
// 真实抽烟时间(精确到时分秒,可补录)
SmokeAt string `json:"smoke_at"`
Remark string `json:"remark"`
Level int64 `json:"level"`
Num int `json:"num"`
@@ -55,8 +62,19 @@ func (h *SmokeHandler) Create(c *gin.Context) {
smokeTime = &parsed
}
var smokeAt *time.Time
if req.SmokeAt != "" {
parsed, err := time.ParseInLocation(dateTimeLayout, req.SmokeAt, time.Local)
if err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "smoke_at 格式错误,应为 YYYY-MM-DD HH:MM:SS"))
return
}
smokeAt = &parsed
}
record, err := h.smokeLogService.Create(c.Request.Context(), int(user.ID), smokeservice.CreateSmokeLogRequest{
SmokeTime: smokeTime,
SmokeAt: smokeAt,
Remark: req.Remark,
Level: req.Level,
Num: req.Num,
@@ -145,6 +163,7 @@ func (h *SmokeHandler) List(c *gin.Context) {
type updateSmokeLogRequest struct {
SmokeTime *string `json:"smoke_time"`
SmokeAt *string `json:"smoke_at"`
Remark *string `json:"remark"`
Level *int64 `json:"level"`
Num *int `json:"num"`
@@ -184,9 +203,26 @@ func (h *SmokeHandler) Update(c *gin.Context) {
}
}
smokeAtProvided := req.SmokeAt != nil
var smokeAt *time.Time
if req.SmokeAt != nil {
if *req.SmokeAt == "" {
smokeAt = nil
} else {
parsed, err := time.ParseInLocation(dateTimeLayout, *req.SmokeAt, time.Local)
if err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "smoke_at 格式错误,应为 YYYY-MM-DD HH:MM:SS"))
return
}
smokeAt = &parsed
}
}
record, err := h.smokeLogService.Update(c.Request.Context(), int(user.ID), id, smokeservice.UpdateSmokeLogRequest{
SmokeTimeProvided: smokeTimeProvided,
SmokeTime: smokeTime,
SmokeAtProvided: smokeAtProvided,
SmokeAt: smokeAt,
Remark: req.Remark,
Level: req.Level,
Num: req.Num,