905086
2026-11-09 cbb26873e88a7a13e868448c1cc578ce70b8c6ae
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
package handlers
 
import (
    "net/http"
    "strings"
 
    "gamebox-server/config"
    "gamebox-server/models"
 
    "github.com/gin-gonic/gin"
)
 
// ---------- 管理端:版本管理 ----------
 
// AdminListVersions 客户端版本列表
func AdminListVersions(c *gin.Context) {
    var versions []models.ClientVersion
    config.DB.Order("id DESC").Find(&versions)
    c.JSON(http.StatusOK, gin.H{"code": 0, "data": versions})
}
 
// AdminSaveVersion 新增/编辑客户端版本
func AdminSaveVersion(c *gin.Context) {
    var req struct {
        ID        uint   `json:"id"`
        Version   string `json:"version"`
        Notes     string `json:"notes"`
        FileURL   string `json:"file_url"`
        FileSize  string `json:"file_size"`
    }
    if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.Version) == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请填写版本号"})
        return
    }
    if strings.TrimSpace(req.FileURL) == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请上传客户端安装包"})
        return
    }
    var v models.ClientVersion
    if req.ID > 0 {
        if err := config.DB.First(&v, req.ID).Error; err != nil {
            c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "版本不存在"})
            return
        }
    } else {
        v = models.ClientVersion{}
    }
    v.Version = strings.TrimSpace(req.Version)
    v.Notes = req.Notes
    v.FileURL = req.FileURL
    v.FileSize = req.FileSize
    if req.ID > 0 {
        config.DB.Save(&v)
    } else {
        config.DB.Create(&v)
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已保存"})
}
 
// AdminDeleteVersion 删除客户端版本
func AdminDeleteVersion(c *gin.Context) {
    config.DB.Delete(&models.ClientVersion{}, c.Param("id"))
    c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已删除"})
}
 
// ---------- 公开端:最新版本查询与下载(无需登录) ----------
 
// GetClientLatest 最新客户端版本信息(下载页使用)
func GetClientLatest(c *gin.Context) {
    var v models.ClientVersion
    if err := config.DB.Order("id DESC").First(&v).Error; err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "暂无可用版本"})
        return
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "data": v})
}
 
// ClientDownload 客户端安装包下载(302 跳转到文件并累计下载次数)
func ClientDownload(c *gin.Context) {
    var v models.ClientVersion
    if err := config.DB.Order("id DESC").First(&v).Error; err != nil || v.FileURL == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "暂无可下载的客户端"})
        return
    }
    config.DB.Model(&v).UpdateColumn("download_count", v.DownloadCount+1)
    c.Redirect(http.StatusFound, v.FileURL)
}