fix: unify handler response format in multiple handlers

- captcha_handler.go: Fix GenerateCaptcha/VerifyCaptcha to use {code, message, data}
- password_reset_handler.go: Fix all error responses to use {code, message}
- settings_handler.go: Add missing "code" and "message" fields
- sms_handler.go: Fix error responses to use {code, message}
- sso_handler.go: Fix all error responses to use {code, message, data}
- stats_handler.go: Add missing "message" field in success responses
- theme_handler.go: Fix error responses to use {code, message}
- totp_handler.go: Fix all responses to use {code, message, data}

Standardize all JSON responses to {code: 0, message: "success", data: ...} for success
and {code: XXX, message: "..."} for errors.
This commit is contained in:
2026-04-11 13:06:58 +08:00
parent e239e95a84
commit b6aff65975
8 changed files with 79 additions and 67 deletions

View File

@@ -33,7 +33,7 @@ func (h *PasswordResetHandler) ForgotPassword(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -42,13 +42,13 @@ func (h *PasswordResetHandler) ForgotPassword(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "password reset email sent"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "password reset email sent"})
}
func (h *PasswordResetHandler) ValidateResetToken(c *gin.Context) {
token := c.Query("token")
if token == "" {
c.JSON(http.StatusBadRequest, gin.H{"error": "token is required"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "token is required"})
return
}
@@ -58,7 +58,7 @@ func (h *PasswordResetHandler) ValidateResetToken(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"valid": valid})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"valid": valid}})
}
func (h *PasswordResetHandler) ResetPassword(c *gin.Context) {
@@ -68,7 +68,7 @@ func (h *PasswordResetHandler) ResetPassword(c *gin.Context) {
}
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -77,7 +77,7 @@ func (h *PasswordResetHandler) ResetPassword(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "password reset successful"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "password reset successful"})
}
// ForgotPasswordByPhoneRequest 短信密码重置请求
@@ -88,13 +88,13 @@ type ForgotPasswordByPhoneRequest struct {
// ForgotPasswordByPhone 发送短信验证码
func (h *PasswordResetHandler) ForgotPasswordByPhone(c *gin.Context) {
if h.smsService == nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "SMS service not configured"})
c.JSON(http.StatusServiceUnavailable, gin.H{"code": 503, "message": "SMS service not configured"})
return
}
var req ForgotPasswordByPhoneRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -106,7 +106,7 @@ func (h *PasswordResetHandler) ForgotPasswordByPhone(c *gin.Context) {
}
if code == "" {
// 用户不存在,不提示
c.JSON(http.StatusOK, gin.H{"message": "verification code sent"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
return
}
@@ -121,7 +121,7 @@ func (h *PasswordResetHandler) ForgotPasswordByPhone(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "verification code sent"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
// ResetPasswordByPhoneRequest 短信验证码重置密码请求
@@ -135,7 +135,7 @@ type ResetPasswordByPhoneRequest struct {
func (h *PasswordResetHandler) ResetPasswordByPhone(c *gin.Context) {
var req ResetPasswordByPhoneRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": err.Error()})
return
}
@@ -149,5 +149,5 @@ func (h *PasswordResetHandler) ResetPasswordByPhone(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "password reset successful"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "password reset successful"})
}