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

@@ -25,7 +25,7 @@ func NewTOTPHandler(authService *service.AuthService, totpService *service.TOTPS
func (h *TOTPHandler) GetTOTPStatus(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -35,13 +35,13 @@ func (h *TOTPHandler) GetTOTPStatus(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"enabled": enabled})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"enabled": enabled}})
}
func (h *TOTPHandler) SetupTOTP(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -52,16 +52,20 @@ func (h *TOTPHandler) SetupTOTP(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"secret": resp.Secret,
"qr_code_base64": resp.QRCodeBase64,
"recovery_codes": resp.RecoveryCodes,
"code": 0,
"message": "success",
"data": gin.H{
"secret": resp.Secret,
"qr_code_base64": resp.QRCodeBase64,
"recovery_codes": resp.RecoveryCodes,
},
})
}
func (h *TOTPHandler) EnableTOTP(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -70,7 +74,7 @@ func (h *TOTPHandler) EnableTOTP(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
}
@@ -79,13 +83,13 @@ func (h *TOTPHandler) EnableTOTP(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "TOTP enabled"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
func (h *TOTPHandler) DisableTOTP(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -94,7 +98,7 @@ func (h *TOTPHandler) DisableTOTP(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
}
@@ -103,13 +107,13 @@ func (h *TOTPHandler) DisableTOTP(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"message": "TOTP disabled"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
func (h *TOTPHandler) VerifyTOTP(c *gin.Context) {
userID, ok := getUserIDFromContext(c)
if !ok {
c.JSON(http.StatusUnauthorized, gin.H{"error": "unauthorized"})
c.JSON(http.StatusUnauthorized, gin.H{"code": 401, "message": "unauthorized"})
return
}
@@ -119,7 +123,7 @@ func (h *TOTPHandler) VerifyTOTP(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
}
@@ -128,5 +132,5 @@ func (h *TOTPHandler) VerifyTOTP(c *gin.Context) {
return
}
c.JSON(http.StatusOK, gin.H{"verified": true})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"verified": true}})
}