From fd8a4c4895bbd0730476e1468487d0df1b32e47d Mon Sep 17 00:00:00 2001 From: Akizon77 Date: Thu, 5 Jun 2025 18:59:17 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BB=9F=E4=B8=80API=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E5=A4=84=E7=90=86=EF=BC=8C=E7=AE=80=E5=8C=96=E9=94=99=E8=AF=AF?= =?UTF-8?q?=E5=92=8C=E6=88=90=E5=8A=9F=E5=93=8D=E5=BA=94=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/admin/settings.go | 4 ++-- api/latestRecord.go | 7 ++----- api/login.go | 14 +++++++------- api/public.go | 25 ++++++++++--------------- api/version.go | 2 +- 5 files changed, 22 insertions(+), 30 deletions(-) diff --git a/api/admin/settings.go b/api/admin/settings.go index bd20de1..db4bc19 100644 --- a/api/admin/settings.go +++ b/api/admin/settings.go @@ -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 更新自定义配置 diff --git a/api/latestRecord.go b/api/latestRecord.go index 879392e..a8c25b1 100644 --- a/api/latestRecord.go +++ b/api/latestRecord.go @@ -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]) } diff --git a/api/login.go b/api/login.go index 6ec3c6b..45518a7 100644 --- a/api/login.go +++ b/api/login.go @@ -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") } } diff --git a/api/public.go b/api/public.go index 1619d94..16b15fe 100644 --- a/api/public.go +++ b/api/public.go @@ -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, }) + } diff --git a/api/version.go b/api/version.go index d6a43ab..6baeb3d 100644 --- a/api/version.go +++ b/api/version.go @@ -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",