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, }}) }