fd097729d7
- 成就系统、连续打卡天数计算、管理后台成就 CRUD - 梦想目标图标预设 DreamPreset 与用户端 dream-presets 接口 - 管理后台梦想图标 CRUD;戒烟打卡 summary 修正 - 忽略根目录编译产物 /api Made-with: Cursor
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
package handler
|
|
|
|
import (
|
|
"log"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"wx_service/internal/achievement"
|
|
"wx_service/internal/middleware"
|
|
"wx_service/internal/model"
|
|
)
|
|
|
|
func (h *SmokeHandler) ListAchievementThemes(c *gin.Context) {
|
|
themes, err := h.achievementService.ListActiveThemes(c.Request.Context())
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取成就主题失败"))
|
|
return
|
|
}
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"themes": themes}))
|
|
}
|
|
|
|
func (h *SmokeHandler) GetAchievement(c *gin.Context) {
|
|
user := middleware.MustCurrentUser(c)
|
|
ctx := c.Request.Context()
|
|
uid := int(user.ID)
|
|
now := time.Now()
|
|
|
|
profile, err := h.smokeProfileService.Get(ctx, uid)
|
|
if err != nil {
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取用户信息失败"))
|
|
return
|
|
}
|
|
if profile == nil || profile.AchievementThemeID == nil {
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"achievement": nil}))
|
|
return
|
|
}
|
|
|
|
var days int
|
|
if profile.Mode == "quit" {
|
|
homeData, err := h.quitCheckinService.Home(ctx, uid, now)
|
|
if err != nil {
|
|
log.Printf("achievement: quitcheckin home err uid=%d: %v", uid, err)
|
|
days = 0
|
|
} else {
|
|
days = homeData.Summary.CurrentStreakDays
|
|
}
|
|
} else {
|
|
streakDays, err := h.smokeLogService.GetStreakDays(ctx, uid, now)
|
|
if err != nil {
|
|
log.Printf("achievement: smoke streak err uid=%d: %v", uid, err)
|
|
days = 0
|
|
}
|
|
days = streakDays
|
|
}
|
|
|
|
ach, err := h.achievementService.GetUserAchievement(ctx, *profile.AchievementThemeID, days)
|
|
if err != nil {
|
|
if err == achievement.ErrThemeNotFound {
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"achievement": nil}))
|
|
return
|
|
}
|
|
c.JSON(http.StatusInternalServerError, model.Error(http.StatusInternalServerError, "获取成就信息失败"))
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, model.Success(gin.H{"achievement": ach}))
|
|
}
|