feat: 统一API响应处理,简化错误和成功响应逻辑

This commit is contained in:
Akizon77 committed 2025-06-05 18:59:17 +08:00
1 parent 039fd6b8d9
commit fd8a4c4895
5 files changed
+22 -30

No files matched your search

+2 -2
View File
@@ -19,7 +19,7 @@ func GetSettings(c *gin.Context) {
cst = models.Config{Sitename: "Komari"}
cst.ID = 1
config.Save(cst)
c.JSON(200, cst)
api.RespondSuccess(c, cst)
return
}
c.JSON(500, gin.H{
@@ -27,7 +27,7 @@ func GetSettings(c *gin.Context) {
"message": "Internal Server Error: " + err.Error(),
})
}
c.JSON(200, cst)
api.RespondSuccess(c, cst)
}
// EditSettings 更新自定义配置
+2 -5
View File
@@ -7,11 +7,8 @@ import (
func GetClientRecentRecords(c *gin.Context) {
uuid := c.Param("uuid")
if uuid == "" {
c.JSON(400, gin.H{
"status": "error",
"message": "Invalid or missing UUID",
})
RespondError(c, 400, "UUID is required")
return
}
c.JSON(200, Records[uuid])
RespondSuccess(c, Records[uuid])
}
+7 -7
View File
@@ -19,23 +19,23 @@ type LoginRequest struct {
func Login(c *gin.Context) {
conf, _ := config.Get()
if conf.DisablePasswordLogin {
c.JSON(http.StatusForbidden, gin.H{"status": "error", "message": "Password login is disabled"})
RespondError(c, http.StatusForbidden, "Password login is disabled")
return
}
bodyBytes, err := io.ReadAll(c.Request.Body)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "Invalid request body"})
RespondError(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
return
}
var data LoginRequest
err = json.Unmarshal(bodyBytes, &data)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "Invalid request body"})
RespondError(c, http.StatusBadRequest, "Invalid request body: "+err.Error())
return
}
if data.Username == "" || data.Password == "" {
c.JSON(http.StatusBadRequest, gin.H{"status": "error", "message": "Invalid request body"})
RespondError(c, http.StatusBadRequest, "Invalid request body: Username and password are required")
return
}
@@ -44,14 +44,14 @@ func Login(c *gin.Context) {
session, err := accounts.CreateSession(uuid, 2592000)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"status": "error", "message": "Failed to create session" + err.Error()})
RespondError(c, http.StatusInternalServerError, "Failed to create session: "+err.Error())
return
}
c.SetCookie("session_token", session, 2592000, "/", "", false, true)
c.JSON(200, gin.H{"status": "success", "message": "", "set-cookie": map[string]interface{}{"session_token": session}})
RespondSuccess(c, gin.H{"set-cookie": gin.H{"session_token": session}})
return
} else {
c.JSON(http.StatusUnauthorized, gin.H{"status": "error", "message": "Invalid credentials"})
RespondError(c, http.StatusUnauthorized, "Invalid credentials")
}
}
+10 -15
View File
@@ -8,23 +8,18 @@ import (
func GetPublicSettings(c *gin.Context) {
cst, err := config.Get()
if err != nil {
c.JSON(500, gin.H{
"status": "error",
"error": "Internal Server Error: " + err.Error(),
})
RespondError(c, 500, err.Error())
return
}
// Return public settings including CORS
c.JSON(200, gin.H{
"status": "success",
"data": gin.H{
"sitename": cst.Sitename,
"description": cst.Description,
"custom_head": cst.CustomHead,
"custom_body": cst.CustomBody,
"oauth_enable": cst.OAuthEnabled,
"disable_password_login": cst.DisablePasswordLogin,
"allow_cors": cst.AllowCors,
},
RespondSuccess(c, gin.H{
"sitename": cst.Sitename,
"description": cst.Description,
"custom_head": cst.CustomHead,
"custom_body": cst.CustomBody,
"oauth_enable": cst.OAuthEnabled,
"disable_password_login": cst.DisablePasswordLogin,
"allow_cors": cst.AllowCors,
})
}
+1 -1
View File
@@ -6,7 +6,7 @@ import (
)
func GetVersion(c *gin.Context) {
c.JSON(200, gin.H{
RespondSuccess(c, gin.H{
"version": utils.CurrentVersion,
"hash": utils.VersionHash,
"status": "success",