905086
2026-11-04 9975888d222206fd358d0f2c159a28a6abc1a404
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
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
    }
    hash, _ := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
    config.DB.Model(&user).Update("password", string(hash))
    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
    }
    // 查询会员有效期(取最大的未过期时间)
    var keys []models.MemberKey
    config.DB.Where("user_id = ? AND expired_at > ?", userID, time.Now()).Find(&keys)
    var expire *time.Time
    for i := range keys {
        if expire == nil || keys[i].ExpiredAt.After(*expire) {
            expire = keys[i].ExpiredAt
        }
    }
    member := gin.H{"is_member": expire != nil}
    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,
    }})
}