You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
80 lines
1.6 KiB
80 lines
1.6 KiB
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response 统一响应结构
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// Success 成功响应
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// SuccessWithMessage 成功响应带消息
|
|
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// Error 错误响应
|
|
func Error(c *gin.Context, code int, message string) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: code,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// BadRequest 参数错误
|
|
func BadRequest(c *gin.Context, message string) {
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
Code: 400,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// Unauthorized 未授权
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
c.JSON(http.StatusUnauthorized, Response{
|
|
Code: 401,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// Forbidden 禁止访问
|
|
func Forbidden(c *gin.Context, message string) {
|
|
c.JSON(http.StatusForbidden, Response{
|
|
Code: 403,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// NotFound 资源不存在
|
|
func NotFound(c *gin.Context, message string) {
|
|
c.JSON(http.StatusNotFound, Response{
|
|
Code: 404,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// ServerError 服务器错误
|
|
func ServerError(c *gin.Context, message string) {
|
|
c.JSON(http.StatusInternalServerError, Response{
|
|
Code: 500,
|
|
Message: message,
|
|
})
|
|
}
|
|
|