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
+101
View File
@@ -0,0 +1,101 @@
package handler
import (
"errors"
"io"
"net/http"
"time"
"github.com/gin-gonic/gin"
"wx_service/internal/middleware"
"wx_service/internal/model"
smokeservice "wx_service/internal/smoke/service"
)
type unlockAIAdviceRequest struct {
Date string `json:"date"`
AdWatchedAt string `json:"ad_watched_at"`
}
func (h *SmokeHandler) GetAIAdvice(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
dateStr := c.Query("date")
adviceDate := yesterdayDate()
if dateStr != "" {
parsed, err := time.ParseInLocation(dateLayout, dateStr, time.Local)
if err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "date 格式错误,应为 YYYY-MM-DD"))
return
}
adviceDate = parsed
}
record, err := h.smokeAIAdviceService.GetOrGenerate(c.Request.Context(), user, adviceDate, smokeservice.DefaultAdvicePromptVersion)
if err != nil {
switch {
case errors.Is(err, smokeservice.ErrAIAdviceLocked):
c.JSON(http.StatusForbidden, model.Error(http.StatusForbidden, "需要会员或观看广告解锁后才可生成建议"))
return
case errors.Is(err, smokeservice.ErrAIServiceDisabled):
c.JSON(http.StatusServiceUnavailable, model.Error(http.StatusServiceUnavailable, "AI 服务暂不可用,请联系管理员"))
return
case errors.Is(err, smokeservice.ErrNoSmokeLogs):
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "该日期没有抽烟记录,无法生成建议"))
return
default:
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "生成建议失败,请稍后重试"))
return
}
}
c.JSON(http.StatusOK, model.Success(gin.H{
"date": record.AdviceDate.Format(dateLayout),
"advice": record.Advice,
}))
}
func (h *SmokeHandler) UnlockAIAdvice(c *gin.Context) {
user, ok := middleware.CurrentUser(c)
if !ok {
c.JSON(http.StatusUnauthorized, model.Error(http.StatusUnauthorized, "未登录或登录已过期"))
return
}
var req unlockAIAdviceRequest
if err := c.ShouldBindJSON(&req); err != nil && !errors.Is(err, io.EOF) {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "请求参数错误"))
return
}
unlockDate := yesterdayDate()
if req.Date != "" {
parsed, err := time.ParseInLocation(dateLayout, req.Date, time.Local)
if err != nil {
c.JSON(http.StatusBadRequest, model.Error(http.StatusBadRequest, "date 格式错误,应为 YYYY-MM-DD"))
return
}
unlockDate = parsed
}
if err := h.smokeAIAdviceService.Unlock(c.Request.Context(), user, unlockDate); err != nil {
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "解锁失败,请稍后重试"))
return
}
c.JSON(http.StatusOK, model.Success(gin.H{
"unlocked": true,
"date": unlockDate.Format(dateLayout),
}))
}
func yesterdayDate() time.Time {
now := time.Now().In(time.Local)
y := now.AddDate(0, 0, -1)
return time.Date(y.Year(), y.Month(), y.Day(), 0, 0, 0, 0, time.Local)
}
+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,