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

@@ -26,13 +26,17 @@ func (h *CaptchaHandler) GenerateCaptcha(c *gin.Context) {
}
c.JSON(http.StatusOK, gin.H{
"captcha_id": result.CaptchaID,
"image": result.ImageData,
"code": 0,
"message": "success",
"data": gin.H{
"captcha_id": result.CaptchaID,
"image": result.ImageData,
},
})
}
func (h *CaptchaHandler) GetCaptchaImage(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"message": "captcha image endpoint"})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success"})
}
func (h *CaptchaHandler) VerifyCaptcha(c *gin.Context) {
@@ -42,13 +46,13 @@ func (h *CaptchaHandler) VerifyCaptcha(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
}
if h.captchaService.Verify(c.Request.Context(), req.CaptchaID, req.Answer) {
c.JSON(http.StatusOK, gin.H{"verified": true})
c.JSON(http.StatusOK, gin.H{"code": 0, "message": "success", "data": gin.H{"verified": true}})
} else {
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid captcha"})
c.JSON(http.StatusBadRequest, gin.H{"code": 400, "message": "invalid captcha"})
}
}