Files
TaskPool/internal/utils/response.go
T
2025-12-20 09:30:16 +08:00

56 lines
908 B
Go

package utils
import (
"net/http"
"github.com/gin-gonic/gin"
)
type Response struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data interface{} `json:"data,omitempty"`
}
func Success(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, Response{
Code: 200,
Msg: "success",
Data: data,
})
}
func SuccessMsg(c *gin.Context, msg string) {
c.JSON(http.StatusOK, Response{
Code: 200,
Msg: msg,
})
}
func Error(c *gin.Context, code int, msg string) {
c.JSON(http.StatusOK, Response{
Code: code,
Msg: msg,
})
}
func BadRequest(c *gin.Context, msg string) {
Error(c, 400, msg)
}
func Unauthorized(c *gin.Context, msg string) {
Error(c, 401, msg)
}
func Forbidden(c *gin.Context, msg string) {
Error(c, 403, msg)
}
func NotFound(c *gin.Context, msg string) {
Error(c, 404, msg)
}
func ServerError(c *gin.Context, msg string) {
Error(c, 500, msg)
}