53 lines
1.2 KiB
Go
53 lines
1.2 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Body struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
func OK(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Body{Code: 0, Message: "ok", Data: data})
|
|
}
|
|
|
|
func Fail(c *gin.Context, httpStatus int, code int, message string) {
|
|
c.JSON(httpStatus, Body{Code: code, Message: message})
|
|
}
|
|
|
|
func BadRequest(c *gin.Context, message string) {
|
|
Fail(c, http.StatusBadRequest, 400, message)
|
|
}
|
|
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
Fail(c, http.StatusUnauthorized, 401, message)
|
|
}
|
|
|
|
func Forbidden(c *gin.Context, message string) {
|
|
Fail(c, http.StatusForbidden, 403, message)
|
|
}
|
|
|
|
func NotFound(c *gin.Context, message string) {
|
|
Fail(c, http.StatusNotFound, 404, message)
|
|
}
|
|
|
|
func ServerError(c *gin.Context, message string) {
|
|
Fail(c, http.StatusInternalServerError, 500, message)
|
|
}
|
|
|
|
type PageData struct {
|
|
List interface{} `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
Size int `json:"size"`
|
|
}
|
|
|
|
func Page(c *gin.Context, list interface{}, total int64, page, size int) {
|
|
OK(c, PageData{List: list, Total: total, Page: page, Size: size})
|
|
}
|