package handlers
|
|
import (
|
"net/http"
|
"strconv"
|
"strings"
|
"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})
|
}
|
|
// ToolDownloadInfo 工具下载信息(无需会员校验,返回格式与游戏下载一致)
|
func ToolDownloadInfo(c *gin.Context) {
|
var tool models.Tool
|
if err := config.DB.First(&tool, c.Param("id")).Error; err != nil {
|
c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "工具不存在"})
|
return
|
}
|
if tool.FileURL == "" {
|
c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "该工具暂无下载文件,请联系管理员上传"})
|
return
|
}
|
// 压缩包文件名取 URL 最后一段
|
fileName := tool.FileURL
|
if idx := strings.LastIndex(tool.FileURL, "/"); idx >= 0 && idx < len(tool.FileURL)-1 {
|
fileName = tool.FileURL[idx+1:]
|
}
|
if q := strings.Index(fileName, "?"); q > 0 {
|
fileName = fileName[:q]
|
}
|
files := []gin.H{{"name": fileName, "url": tool.FileURL, "size": tool.Size}}
|
c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
|
"name": tool.Name,
|
"files": files,
|
"launch_exe": tool.LaunchExe,
|
}})
|
}
|
|
// 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
|
}
|
// 校验会员是否有效(永久卡 expired_at 为 NULL;冻结的密钥视为无效)
|
var keys []models.MemberKey
|
config.DB.Where("user_id = ? AND frozen = ? AND (expired_at IS NULL OR expired_at > ?)", userID, false, time.Now()).Find(&keys)
|
if len(keys) == 0 {
|
// 用户绑定的密钥被冻结时给出精确提示
|
var frozenCount int64
|
config.DB.Model(&models.MemberKey{}).Where("user_id = ? AND frozen = ?", userID, true).Count(&frozenCount)
|
if frozenCount > 0 {
|
c.JSON(http.StatusOK, gin.H{"code": 2, "msg": "您的密钥已被冻结,无法下载,请联系管理员激活密钥!"})
|
} else {
|
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,
|
"launch_exe": game.LaunchExe,
|
}})
|
}
|