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
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
package handlers
 
import (
    "net/http"
    "time"
 
    "gamebox-server/config"
    "gamebox-server/models"
    "gamebox-server/utils"
 
    "github.com/gin-gonic/gin"
    "github.com/mojocn/base64Captcha"
    "golang.org/x/crypto/bcrypt"
)
 
var captchaStore = base64Captcha.NewMemoryStore(1024, 10*time.Minute)
 
func newCaptchaDriver() *base64Captcha.DriverString {
    return base64Captcha.NewDriverString(40, 120, 2, 2, 4,
        "1234567890", nil, nil, nil)
}
 
// GetCaptcha 获取图形验证码
func GetCaptcha(c *gin.Context) {
    drv := newCaptchaDriver()
    captcha := base64Captcha.NewCaptcha(drv, captchaStore)
    id, b64s, _, err := captcha.Generate()
    if err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "验证码生成失败"})
        return
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{"captcha_id": id, "captcha_image": b64s}})
}
 
func verifyCaptcha(id, value string) bool {
    if id == "" || value == "" {
        return false
    }
    return captchaStore.Verify(id, value, true)
}
 
// Register 注册账号
func Register(c *gin.Context) {
    var req struct {
        Phone    string `json:"phone"`
        Password string `json:"password"`
        Confirm  string `json:"confirm"`
        CaptchaID string `json:"captcha_id"`
        Captcha  string `json:"captcha"`
    }
    if err := c.ShouldBindJSON(&req); err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "参数错误"})
        return
    }
    if len(req.Phone) != 11 {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请输入正确的11位手机号"})
        return
    }
    if len(req.Password) < 6 {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "密码至少6位"})
        return
    }
    if req.Password != req.Confirm {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "两次输入的密码不一致"})
        return
    }
    if !verifyCaptcha(req.CaptchaID, req.Captcha) {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "图形验证码错误"})
        return
    }
    var count int64
    config.DB.Model(&models.User{}).Where("phone = ?", req.Phone).Count(&count)
    if count > 0 {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "该手机号已注册"})
        return
    }
    hash, _ := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
    user := models.User{Phone: req.Phone, Password: string(hash)}
    if err := config.DB.Create(&user).Error; err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "注册失败"})
        return
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "注册成功"})
}
 
// Login 登录
func Login(c *gin.Context) {
    var req struct {
        Phone    string `json:"phone"`
        Password string `json:"password"`
    }
    if err := c.ShouldBindJSON(&req); err != nil || req.Phone == "" || req.Password == "" {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "请输入账号和密码"})
        return
    }
    var user models.User
    if err := config.DB.Where("phone = ?", req.Phone).First(&user).Error; err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "账号或密码错误"})
        return
    }
    if bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.Password)) != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "账号或密码错误"})
        return
    }
    token, _ := utils.GenerateToken(user.ID, user.Phone, "user")
    c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
        "token": token,
        "user": gin.H{
            "id":    user.ID,
            "phone": user.Phone,
        },
    }})
}
 
// ResetPassword 找回密码:提交重置申请(含期望的新密码),由管理员在后台同意后生效
func ResetPassword(c *gin.Context) {
    var req struct {
        Phone     string `json:"phone"`
        Password  string `json:"password"`
        Confirm   string `json:"confirm"`
        CaptchaID string `json:"captcha_id"`
        Captcha   string `json:"captcha"`
    }
    if err := c.ShouldBindJSON(&req); err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "参数错误"})
        return
    }
    if !verifyCaptcha(req.CaptchaID, req.Captcha) {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "图形验证码错误"})
        return
    }
    var user models.User
    if err := config.DB.Where("phone = ?", req.Phone).First(&user).Error; err != nil {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "该手机号未注册"})
        return
    }
    if len(req.Password) < 6 {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "密码至少6位"})
        return
    }
    if req.Password != req.Confirm {
        c.JSON(http.StatusOK, gin.H{"code": 1, "msg": "两次输入的密码不一致"})
        return
    }
    // 已有待处理的申请则更新期望密码,不重复创建
    var apply models.PasswordResetRequest
    if err := config.DB.Where("phone = ? AND status = ?", req.Phone, 0).First(&apply).Error; err != nil {
        config.DB.Create(&models.PasswordResetRequest{Phone: req.Phone, ProposedPwd: req.Password})
    } else {
        config.DB.Model(&apply).Update("proposed_pwd", req.Password)
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "找回密码申请已提交,请联系管理员重置密码!"})
}
 
// GetUserInfo 当前用户信息(含会员状态)
func GetUserInfo(c *gin.Context) {
    userID := c.GetUint("user_id")
    var user models.User
    if err := config.DB.First(&user, userID).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)
    permanent := false
    var expire *time.Time
    for i := range keys {
        if keys[i].ExpiredAt == nil {
            permanent = true
            continue
        }
        if expire == nil || keys[i].ExpiredAt.After(*expire) {
            expire = keys[i].ExpiredAt
        }
    }
    member := gin.H{"is_member": permanent || expire != nil}
    if permanent {
        member["expire_at"] = "永久"
    } else if expire != nil {
        member["expire_at"] = expire.Format("2006-01-02 15:04:05")
    }
    c.JSON(http.StatusOK, gin.H{"code": 0, "data": gin.H{
        "id":    user.ID,
        "phone": user.Phone,
        "no":    user.ID,
        "member": member,
    }})
}