diff --git a/helper/resp_helper.go b/gweb/resp.go similarity index 57% rename from helper/resp_helper.go rename to gweb/resp.go index c548105..b202692 100644 --- a/helper/resp_helper.go +++ b/gweb/resp.go @@ -1,11 +1,11 @@ // -// resp_helper.go -// Copyright (C) 2022 tiglog +// resp.go +// Copyright (C) 2023 tiglog // // Distributed under terms of the MIT license. // -package helper +package gweb import ( "net/http" @@ -13,7 +13,7 @@ import ( "github.com/gin-gonic/gin" ) -func RenderJson(c *gin.Context, code int, msg string, data interface{}) { +func RenderJson(c *Context, code int, msg string, data interface{}) { c.JSON(http.StatusOK, gin.H{ "code": code, "msg": msg, @@ -22,38 +22,38 @@ func RenderJson(c *gin.Context, code int, msg string, data interface{}) { } // 成功时,返回的 code 为 0 -func RenderOk(c *gin.Context, data interface{}) { +func RenderOk(c *Context, data interface{}) { RenderJson(c, 0, "success", data) } // 失败时,返回的 code 为指定的 code // 一般会比 http status code 要详细一点 -func RenderFail(c *gin.Context, code int, msg string) { +func RenderFail(c *Context, code int, msg string) { RenderJson(c, code, msg, nil) } // 成功时,返回自定义消息和数据 -func RenderSuccess(c *gin.Context, msg string, data interface{}) { +func RenderSuccess(c *Context, msg string, data interface{}) { RenderJson(c, 0, msg, data) } -func RenderClientError(c *gin.Context, err error) { +func RenderClientError(c *Context, err error) { RenderJson(c, http.StatusBadRequest, err.Error(), nil) } -func RenderServerError(c *gin.Context, err error) { +func RenderServerError(c *Context, err error) { RenderJson(c, http.StatusInternalServerError, err.Error(), nil) } -func RenderClientFail(c *gin.Context, msg string) { +func RenderClientFail(c *Context, msg string) { RenderJson(c, http.StatusBadRequest, msg, nil) } -func RenderServerFail(c *gin.Context, msg string) { +func RenderServerFail(c *Context, msg string) { RenderJson(c, http.StatusInternalServerError, msg, nil) } // 未发现的各种情况 -func RenderNotFound(c *gin.Context, msg string) { +func RenderNotFound(c *Context, msg string) { RenderJson(c, http.StatusNotFound, msg, nil) }