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.
63 lines
1.6 KiB
63 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"` // 响应数据 |
|
Error string `json:"error,omitempty"` // 错误信息 |
|
} |
|
|
|
// Success 成功响应 |
|
func Success(c *gin.Context, message string, data interface{}) { |
|
c.JSON(http.StatusOK, Response{ |
|
Code: http.StatusOK, |
|
Message: message, |
|
Data: data, |
|
}) |
|
} |
|
|
|
// Error 错误响应 |
|
func Error(c *gin.Context, code int, message string, error string) { |
|
c.JSON(code, Response{ |
|
Code: code, |
|
Message: message, |
|
Error: error, |
|
}) |
|
} |
|
|
|
// BadRequest 400 错误响应 |
|
func BadRequest(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusBadRequest, message, error) |
|
} |
|
|
|
// Unauthorized 401 错误响应 |
|
func Unauthorized(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusUnauthorized, message, error) |
|
} |
|
|
|
// Forbidden 403 错误响应 |
|
func Forbidden(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusForbidden, message, error) |
|
} |
|
|
|
// NotFound 404 错误响应 |
|
func NotFound(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusNotFound, message, error) |
|
} |
|
|
|
// InternalServerError 500 错误响应 |
|
func InternalServerError(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusInternalServerError, message, error) |
|
} |
|
|
|
// Locked 423 错误响应(账户被锁定) |
|
func Locked(c *gin.Context, message string, error string) { |
|
Error(c, http.StatusLocked, message, error) |
|
}
|
|
|