From 262ce70998fa52dec3120589f51c47e96fb96df4 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Tue, 03 Nov 2026 17:04:19 +0800
Subject: [PATCH] feat: 完成游戏盒子前后端基础功能开发

---
 server/handlers/games.go |  221 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 221 insertions(+), 0 deletions(-)

diff --git a/server/handlers/games.go b/server/handlers/games.go
new file mode 100644
index 0000000..a4de35d
--- /dev/null
+++ b/server/handlers/games.go
@@ -0,0 +1,221 @@
+package handlers
+
+import (
+	"net/http"
+	"strconv"
+	"time"
+
+	"gamebox-server/config"
+	"gamebox-server/models"
+
+	"github.com/gin-gonic/gin"
+)
+
+func fillCategoryName(games *[]models.Game) {
+	var cats []models.Category
+	config.DB.Find(&cats)
+	catMap := map[uint]string{}
+	for _, c := range cats {
+		catMap[c.ID] = c.Name
+	}
+	for i := range *games {
+		(*games)[i].CategoryName = catMap[(*games)[i].CategoryID]
+	}
+}
+
+// GetGames 游戏列表 platform: all/pc/switch
+func GetGames(c *gin.Context) {
+	platform := c.DefaultQuery("platform", "all")
+	categoryID := c.DefaultQuery("category", "0")
+	keyword := c.Query("keyword")
+	sort := c.DefaultQuery("sort", "updated")
+
+	query := config.DB.Model(&models.Game{})
+	if platform == "pc" || platform == "switch" {
+		query = query.Where("platform = ?", platform)
+	}
+	if catID, err := strconv.Atoi(categoryID); err == nil && catID > 0 {
+		query = query.Where("category_id = ?", catID)
+	}
+	if keyword != "" {
+		query = query.Where("title LIKE ?", "%"+keyword+"%")
+	}
+	switch sort {
+	case "updated":
+		query = query.Order("updated_at DESC")
+	case "rank":
+		query = query.Order("download_count DESC")
+	case "size":
+		query = query.Order("updated_at DESC")
+	case "hot":
+		query = query.Order("download_count DESC")
+	default:
+		query = query.Order("updated_at DESC")
+	}
+	var games []models.Game
+	query.Find(&games)
+	fillCategoryName(&games)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": games})
+}
+
+// GetBanners 首页轮播
+func GetBanners(c *gin.Context) {
+	var games []models.Game
+	config.DB.Where("is_banner = ?", true).Order("updated_at DESC").Limit(5).Find(&games)
+	fillCategoryName(&games)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": games})
+}
+
+// GetGameDetail 游戏详情
+func GetGameDetail(c *gin.Context) {
+	id := c.Param("id")
+	var game models.Game
+	if err := config.DB.First(&game, id).Error; err != nil {
+		c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "游戏不存在"})
+		return
+	}
+	fillCategoryName(&[]models.Game{game})
+	// 用户维度附加信息
+	resp := gin.H{"game": game}
+	if uid := c.GetUint("user_id"); uid > 0 {
+		var favCount, libCount int64
+		config.DB.Model(&models.Favorite{}).Where("user_id = ? AND game_id = ?", uid, game.ID).Count(&favCount)
+		config.DB.Model(&models.LibraryItem{}).Where("user_id = ? AND game_id = ?", uid, game.ID).Count(&libCount)
+		resp["is_favorite"] = favCount > 0
+		resp["in_library"] = libCount > 0
+	}
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": resp})
+}
+
+// GetCategories 类别列表
+func GetCategories(c *gin.Context) {
+	var cats []models.Category
+	config.DB.Order("sort ASC").Find(&cats)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": cats})
+}
+
+// GetTools 工具列表
+func GetTools(c *gin.Context) {
+	var tools []models.Tool
+	config.DB.Order("id ASC").Find(&tools)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": tools})
+}
+
+// GetAnnouncements 公告
+func GetAnnouncements(c *gin.Context) {
+	var anns []models.Announcement
+	config.DB.Order("id DESC").Limit(10).Find(&anns)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": anns})
+}
+
+// ToggleFavorite 收藏/取消收藏
+func ToggleFavorite(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	var req struct {
+		GameID uint `json:"game_id"`
+	}
+	if err := c.ShouldBindJSON(&req); err != nil || req.GameID == 0 {
+		c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "参数错误"})
+		return
+	}
+	var fav models.Favorite
+	err := config.DB.Where("user_id = ? AND game_id = ?", userID, req.GameID).First(&fav).Error
+	if err == nil {
+		config.DB.Delete(&fav)
+		c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{"favorited": false}, "msg": "已取消收藏"})
+		return
+	}
+	config.DB.Create(&models.Favorite{UserID: userID, GameID: req.GameID})
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{"favorited": true}, "msg": "加入收藏成功!"})
+}
+
+// GetFavorites 我的收藏
+func GetFavorites(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	var favs []models.Favorite
+	config.DB.Where("user_id = ?", userID).Order("id DESC").Find(&favs)
+	gameIDs := make([]uint, 0, len(favs))
+	for _, f := range favs {
+		gameIDs = append(gameIDs, f.GameID)
+	}
+	games := []models.Game{}
+	if len(gameIDs) > 0 {
+		config.DB.Where("id IN ?", gameIDs).Find(&games)
+		fillCategoryName(&games)
+	}
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": games})
+}
+
+// AddToLibrary 加入资源库
+func AddToLibrary(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	var req struct {
+		GameID uint `json:"game_id"`
+	}
+	if err := c.ShouldBindJSON(&req); err != nil || req.GameID == 0 {
+		c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "参数错误"})
+		return
+	}
+	var count int64
+	config.DB.Model(&models.LibraryItem{}).Where("user_id = ? AND game_id = ?", userID, req.GameID).Count(&count)
+	if count > 0 {
+		c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已在资源库中"})
+		return
+	}
+	config.DB.Create(&models.LibraryItem{UserID: userID, GameID: req.GameID})
+	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "兑换成功!"})
+}
+
+// GetLibrary 我的资源库
+func GetLibrary(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	var items []models.LibraryItem
+	config.DB.Where("user_id = ?", userID).Order("id DESC").Find(&items)
+	gameIDs := make([]uint, 0, len(items))
+	for _, f := range items {
+		gameIDs = append(gameIDs, f.GameID)
+	}
+	games := []models.Game{}
+	if len(gameIDs) > 0 {
+		config.DB.Where("id IN ?", gameIDs).Find(&games)
+		fillCategoryName(&games)
+	}
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": games})
+}
+
+// RemoveFromLibrary 移出资源库(卸载)
+func RemoveFromLibrary(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	gameID := c.Param("gameId")
+	config.DB.Where("user_id = ? AND game_id = ?", userID, gameID).Delete(&models.LibraryItem{})
+	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已移出资源库"})
+}
+
+// GetDownloadInfo 获取游戏下载地址(需要有效会员)
+func GetDownloadInfo(c *gin.Context) {
+	userID := c.GetUint("user_id")
+	gameID := c.Param("id")
+	var game models.Game
+	if err := config.DB.First(&game, gameID).Error; err != nil {
+		c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "游戏不存在"})
+		return
+	}
+	// 校验会员是否有效
+	var keys []models.MemberKey
+	config.DB.Where("user_id = ? AND expired_at > ?", userID, time.Now()).Find(&keys)
+	if len(keys) == 0 {
+		c.JSON(http.StatusOK, gin.H{"code": 2, "msg": "您的会员已到期,无法下载游戏。请激活新的会员密钥!"})
+		return
+	}
+	// 加入资源库 & 下载计数
+	var count int64
+	config.DB.Model(&models.LibraryItem{}).Where("user_id = ? AND game_id = ?", userID, game.ID).Count(&count)
+	if count == 0 {
+		config.DB.Create(&models.LibraryItem{UserID: userID, GameID: game.ID})
+	}
+	config.DB.Model(&game).UpdateColumn("download_count", game.DownloadCount+1)
+	c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
+		"title": game.Title,
+		"files": game.Files,
+	}})
+}

--
Gitblit v1.10.0