62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package utils
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"goalfymax-admin/internal/models"
|
|
)
|
|
|
|
// Response 统一响应处理
|
|
type Response struct{}
|
|
|
|
// Success 成功响应
|
|
func (r *Response) Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, models.NewSuccessResponse(data))
|
|
}
|
|
|
|
// Error 错误响应
|
|
func (r *Response) Error(c *gin.Context, code int, message string) {
|
|
c.JSON(code, models.NewResponse(code, message, nil))
|
|
}
|
|
|
|
// BadRequest 400错误
|
|
func (r *Response) BadRequest(c *gin.Context, message string) {
|
|
r.Error(c, http.StatusBadRequest, message)
|
|
}
|
|
|
|
// Unauthorized 401错误
|
|
func (r *Response) Unauthorized(c *gin.Context, message string) {
|
|
r.Error(c, http.StatusUnauthorized, message)
|
|
}
|
|
|
|
// Forbidden 403错误
|
|
func (r *Response) Forbidden(c *gin.Context, message string) {
|
|
r.Error(c, http.StatusForbidden, message)
|
|
}
|
|
|
|
// NotFound 404错误
|
|
func (r *Response) NotFound(c *gin.Context, message string) {
|
|
r.Error(c, http.StatusNotFound, message)
|
|
}
|
|
|
|
// InternalServerError 500错误
|
|
func (r *Response) InternalServerError(c *gin.Context, message string) {
|
|
r.Error(c, http.StatusInternalServerError, message)
|
|
}
|
|
|
|
// Page 分页响应
|
|
func (r *Response) Page(c *gin.Context, data interface{}, total int64, page, size int) {
|
|
c.JSON(http.StatusOK, models.NewPageResponse(data, total, page, size))
|
|
}
|
|
|
|
// ValidateError 验证错误响应
|
|
func (r *Response) ValidateError(c *gin.Context, err error) {
|
|
r.BadRequest(c, err.Error())
|
|
}
|
|
|
|
// NewResponse 创建响应实例
|
|
func NewResponse() *Response {
|
|
return &Response{}
|
|
}
|