905086
2026-11-08 6cc9b12b81d184bb2df9dc68f93ca45c3d47d229
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
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,
    }})
}