From 076d5b99d238ebcdc9b9b7b136887a69fc2fa333 Mon Sep 17 00:00:00 2001
From: 905086 <k2049@qq.com>
Date: Sat, 07 Nov 2026 16:55:04 +0800
Subject: [PATCH] feat: 新增永久卡、密钥冻结、工具管理及公告功能,优化游戏安装逻辑

---
 server/handlers/admin.go |  120 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 115 insertions(+), 5 deletions(-)

diff --git a/server/handlers/admin.go b/server/handlers/admin.go
index 0dfba1c..cd59794 100644
--- a/server/handlers/admin.go
+++ b/server/handlers/admin.go
@@ -47,7 +47,7 @@
 		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
 	}
@@ -68,16 +68,24 @@
 // 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())
 	}
@@ -118,6 +126,10 @@
 			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)
 	}
@@ -149,6 +161,74 @@
 	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"`
+	}
+	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
+	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": "已删除"})
+}
+
 // ---------- 游戏管理 ----------
 
 func AdminListGames(c *gin.Context) {
@@ -182,7 +262,7 @@
 			"title": game.Title, "category_id": game.CategoryID, "platform": game.Platform,
 			"tags": game.Tags, "size": game.Size, "install_size": game.InstallSize,
 			"os_req": game.OSReq, "cpu_req": game.CPUReq, "gpu_req": game.GPUReq, "ram_req": game.RAMReq,
-			"version_note": game.VersionNote, "cover": game.Cover, "video": game.Video,
+			"version_note": game.VersionNote, "launch_exe": game.LaunchExe, "cover": game.Cover, "video": game.Video,
 			"screenshots": game.Screenshots, "description": game.Description, "files": game.Files,
 			"is_banner": game.IsBanner,
 		})
@@ -296,4 +376,34 @@
 	}})
 }
 
+// ---------- 公告管理 ----------
+
+// 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

--
Gitblit v1.10.0