| | |
| | | CardType string `json:"card_type"` // day/month/year |
| | | Count int `json:"count"` |
| | | } |
| | | if err := c.ShouldBindJSON(&req); err != nil || cardDuration(req.CardType) == 0 { |
| | | if err := c.ShouldBindJSON(&req); err != nil || !validCardType(req.CardType) { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "卡密类型错误"}) |
| | | return |
| | | } |
| | |
| | | // ListKeys 密钥列表 |
| | | func ListKeys(c *gin.Context) { |
| | | keyword := c.Query("keyword") |
| | | status := c.Query("status") // ""全部 0未使用 1已使用 2已过期 |
| | | status := c.Query("status") // ""全部 0未使用 1已使用(含永久) 2已过期 |
| | | query := config.DB.Model(&models.MemberKey{}) |
| | | if keyword != "" { |
| | | query = query.Where("code LIKE ?", "%"+keyword+"%") |
| | | // 同时支持按激活码或绑定用户手机号搜索 |
| | | var userIDs []uint |
| | | config.DB.Model(&models.User{}).Where("phone LIKE ?", "%"+keyword+"%").Pluck("id", &userIDs) |
| | | if len(userIDs) > 0 { |
| | | query = query.Where("code LIKE ? OR user_id IN ?", "%"+keyword+"%", userIDs) |
| | | } else { |
| | | query = query.Where("code LIKE ?", "%"+keyword+"%") |
| | | } |
| | | } |
| | | switch status { |
| | | case "0": |
| | | query = query.Where("status = ?", 0) |
| | | case "1": |
| | | query = query.Where("status = ? AND expired_at > ?", 1, time.Now()) |
| | | // 已使用且未过期(永久卡 expired_at 为 NULL) |
| | | query = query.Where("status = ? AND (expired_at IS NULL OR expired_at > ?)", 1, time.Now()) |
| | | case "2": |
| | | query = query.Where("status = ? AND expired_at <= ?", 1, time.Now()) |
| | | } |
| | |
| | | vo.Expired = k.ExpiredAt.Before(time.Now()) |
| | | s := k.ExpiredAt.Format("2006-01-02 15:04:05") |
| | | vo.ExpiredAtStr = &s |
| | | } else if k.Status == 1 { |
| | | // 已使用的永久卡 |
| | | s := "永久" |
| | | vo.ExpiredAtStr = &s |
| | | } |
| | | list = append(list, vo) |
| | | } |
| | |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已删除"}) |
| | | } |
| | | |
| | | // FreezeKey 冻结/解冻密钥(冻结后无法激活使用) |
| | | func FreezeKey(c *gin.Context) { |
| | | id := c.Param("id") |
| | | var key models.MemberKey |
| | | if err := config.DB.First(&key, id).Error; err != nil { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "密钥不存在"}) |
| | | return |
| | | } |
| | | target := !key.Frozen |
| | | config.DB.Model(&key).Update("frozen", target) |
| | | msg := "密钥已冻结,无法使用" |
| | | if !target { |
| | | msg = "密钥已解冻,可正常使用" |
| | | } |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "msg": msg}) |
| | | } |
| | | |
| | | // ---------- 工具管理 ---------- |
| | | |
| | | // AdminListTools 工具列表 |
| | | func AdminListTools(c *gin.Context) { |
| | | var tools []models.Tool |
| | | config.DB.Order("id DESC").Find(&tools) |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "data": tools}) |
| | | } |
| | | |
| | | // AdminSaveTool 新增/编辑工具 |
| | | func AdminSaveTool(c *gin.Context) { |
| | | var req struct { |
| | | ID uint `json:"id"` |
| | | Name string `json:"name"` |
| | | Size string `json:"size"` |
| | | Cover string `json:"cover"` |
| | | Description string `json:"description"` |
| | | FileURL string `json:"file_url"` |
| | | LaunchExe string `json:"launch_exe"` |
| | | } |
| | | if err := c.ShouldBindJSON(&req); err != nil || strings.TrimSpace(req.Name) == "" { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请填写工具名称"}) |
| | | return |
| | | } |
| | | var tool models.Tool |
| | | if req.ID > 0 { |
| | | if err := config.DB.First(&tool, req.ID).Error; err != nil { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "工具不存在"}) |
| | | return |
| | | } |
| | | } else { |
| | | tool = models.Tool{} |
| | | } |
| | | tool.Name = req.Name |
| | | tool.Size = req.Size |
| | | tool.Cover = req.Cover |
| | | tool.Description = req.Description |
| | | tool.FileURL = req.FileURL |
| | | tool.LaunchExe = req.LaunchExe |
| | | if req.ID > 0 { |
| | | config.DB.Save(&tool) |
| | | } else { |
| | | config.DB.Create(&tool) |
| | | } |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已保存"}) |
| | | } |
| | | |
| | | // AdminDeleteTool 删除工具 |
| | | func AdminDeleteTool(c *gin.Context) { |
| | | config.DB.Delete(&models.Tool{}, c.Param("id")) |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已删除"}) |
| | | } |
| | | |
| | | // ---------- 游戏管理 ---------- |
| | | |
| | | // isPlainNumber 校验非空纯数字(支持小数,如 232.6) |
| | | func isPlainNumber(s string) bool { |
| | | if s == "" { |
| | | return false |
| | | } |
| | | for _, r := range s { |
| | | if (r < '0' || r > '9') && r != '.' { |
| | | return false |
| | | } |
| | | } |
| | | return true |
| | | } |
| | | |
| | | func AdminListGames(c *gin.Context) { |
| | | var games []models.Game |
| | | config.DB.Order("id DESC").Find(&games) |
| | |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请填写游戏名称"}) |
| | | return |
| | | } |
| | | // 游戏大小/磁盘占用统一为GB,只允许纯数字(如 232.6) |
| | | if !isPlainNumber(game.Size) { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "游戏大小请填写纯数字(单位GB),如 232.6"}) |
| | | return |
| | | } |
| | | if !isPlainNumber(game.InstallSize) { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "磁盘占用请填写纯数字(单位GB),如 411.6"}) |
| | | return |
| | | } |
| | | if game.Screenshots == "" { |
| | | game.Screenshots = "[]" |
| | | } |
| | |
| | | }}) |
| | | } |
| | | |
| | | // ---------- 公告管理 ---------- |
| | | |
| | | // AdminGetAnnouncement 获取当前首页公告(最新一条,无则返回空内容) |
| | | func AdminGetAnnouncement(c *gin.Context) { |
| | | var ann models.Announcement |
| | | if err := config.DB.Order("id DESC").First(&ann).Error; err != nil { |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{"content": ""}}) |
| | | return |
| | | } |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "data": ann}) |
| | | } |
| | | |
| | | // AdminSaveAnnouncement 保存首页公告(更新最新一条,无则新建;内容为空表示不弹出) |
| | | func AdminSaveAnnouncement(c *gin.Context) { |
| | | var req struct { |
| | | Content string `json:"content"` |
| | | } |
| | | if err := c.ShouldBindJSON(&req); err != nil { |
| | | c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "参数错误"}) |
| | | return |
| | | } |
| | | var ann models.Announcement |
| | | if err := config.DB.Order("id DESC").First(&ann).Error; err == nil { |
| | | config.DB.Model(&ann).Update("content", req.Content) |
| | | } else { |
| | | config.DB.Create(&models.Announcement{Content: req.Content}) |
| | | } |
| | | c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "公告已保存"}) |
| | | } |
| | | |
| | | var _ = io.Discard |