905086
2026-11-04 9593973fb0ff112cf26266bfedbf45e6460b10bf
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
package handlers
 
import (
    "net/http"
    "time"
 
    "gamebox-server/config"
    "gamebox-server/models"
 
    "github.com/gin-gonic/gin"
)
 
func cardDuration(cardType string) time.Duration {
    switch cardType {
    case "day":
        return 24 * time.Hour
    case "month":
        return 30 * 24 * time.Hour
    case "year":
        return 365 * 24 * time.Hour
    }
    return 0
}
 
// ActivateKey 激活会员密钥(绑定机器)
func ActivateKey(c *gin.Context) {
    userID := c.GetUint("user_id")
    var req struct {
        Code        string `json:"code"`
        MachineCode string `json:"machine_code"`
    }
    if err := c.ShouldBindJSON(&req); err != nil || req.Code == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请输入激活码"})
        return
    }
    if req.MachineCode == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "获取机器码失败"})
        return
    }
    var key models.MemberKey
    if err := config.DB.Where("code = ?", req.Code).First(&key).Error; err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "激活码不存在,请检查后重新输入"})
        return
    }
 
    now := time.Now()
    if key.Status == 0 {
        // 未使用,直接激活
        expired := now.Add(cardDuration(key.CardType))
        config.DB.Model(&key).Updates(map[string]interface{}{
            "status":       1,
            "user_id":      userID,
            "machine_code": req.MachineCode,
            "activated_at": now,
            "expired_at":   expired,
        })
        c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "激活成功!", "data": gin.H{
            "expire_at": expired.Format("2006-01-02 15:04:05"),
        }})
        return
    }
 
    // 已使用的密钥
    if key.MachineCode != req.MachineCode {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "该激活码已在其他电脑上使用,无法在此设备激活!"})
        return
    }
    if key.ExpiredAt != nil && key.ExpiredAt.Before(now) {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "该激活码已过期,请使用新的激活码!"})
        return
    }
    // 同机器重复激活,返回当前有效期
    c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "该激活码已在此设备激活", "data": gin.H{
        "expire_at": key.ExpiredAt.Format("2006-01-02 15:04:05"),
    }})
}