refactor: 把 resp_helper 放在 gweb 下
This commit is contained in:
parent
4485790236
commit
2757e4f3c3
@ -1,11 +1,11 @@
|
|||||||
//
|
//
|
||||||
// resp_helper.go
|
// resp.go
|
||||||
// Copyright (C) 2022 tiglog <me@tiglog.com>
|
// Copyright (C) 2023 tiglog <me@tiglog.com>
|
||||||
//
|
//
|
||||||
// Distributed under terms of the MIT license.
|
// Distributed under terms of the MIT license.
|
||||||
//
|
//
|
||||||
|
|
||||||
package helper
|
package gweb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"net/http"
|
"net/http"
|
||||||
@ -13,7 +13,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"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{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"code": code,
|
"code": code,
|
||||||
"msg": msg,
|
"msg": msg,
|
||||||
@ -22,38 +22,38 @@ func RenderJson(c *gin.Context, code int, msg string, data interface{}) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 成功时,返回的 code 为 0
|
// 成功时,返回的 code 为 0
|
||||||
func RenderOk(c *gin.Context, data interface{}) {
|
func RenderOk(c *Context, data interface{}) {
|
||||||
RenderJson(c, 0, "success", data)
|
RenderJson(c, 0, "success", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
// 失败时,返回的 code 为指定的 code
|
// 失败时,返回的 code 为指定的 code
|
||||||
// 一般会比 http status 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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
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)
|
RenderJson(c, http.StatusNotFound, msg, nil)
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user