112 lines
2.2 KiB
Go
112 lines
2.2 KiB
Go
// pkg/response/response.go
|
|
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// 错误码定义
|
|
const (
|
|
CodeSuccess = 0
|
|
CodeParamError = 1001
|
|
CodeUnauthorized = 1002
|
|
CodeForbidden = 1003
|
|
CodeNotFound = 1004
|
|
CodeServerError = 5000
|
|
CodeDatabaseError = 5001
|
|
)
|
|
|
|
// Response 统一响应结构
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// PageData 分页数据结构
|
|
type PageData struct {
|
|
List interface{} `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"page_size"`
|
|
}
|
|
|
|
// Success 成功响应
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: CodeSuccess,
|
|
Msg: "success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// SuccessWithMessage 带消息的成功响应
|
|
func SuccessWithMessage(c *gin.Context, msg string, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: CodeSuccess,
|
|
Msg: msg,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// SuccessPage 分页成功响应
|
|
func SuccessPage(c *gin.Context, list interface{}, total int64, page, pageSize int) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: CodeSuccess,
|
|
Msg: "success",
|
|
Data: PageData{
|
|
List: list,
|
|
Total: total,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
},
|
|
})
|
|
}
|
|
|
|
// Error 错误响应
|
|
func Error(c *gin.Context, code int, msg string) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: code,
|
|
Msg: msg,
|
|
})
|
|
}
|
|
|
|
// ParamError 参数错误
|
|
func ParamError(c *gin.Context, msg string) {
|
|
Error(c, CodeParamError, msg)
|
|
}
|
|
|
|
// Unauthorized 未授权
|
|
func Unauthorized(c *gin.Context, msg string) {
|
|
c.JSON(http.StatusUnauthorized, Response{
|
|
Code: CodeUnauthorized,
|
|
Msg: msg,
|
|
})
|
|
}
|
|
|
|
// Forbidden 禁止访问
|
|
func Forbidden(c *gin.Context, msg string) {
|
|
c.JSON(http.StatusForbidden, Response{
|
|
Code: CodeForbidden,
|
|
Msg: msg,
|
|
})
|
|
}
|
|
|
|
// NotFound 未找到
|
|
func NotFound(c *gin.Context, msg string) {
|
|
c.JSON(http.StatusNotFound, Response{
|
|
Code: CodeNotFound,
|
|
Msg: msg,
|
|
})
|
|
}
|
|
|
|
// ServerError 服务器错误
|
|
func ServerError(c *gin.Context, msg string) {
|
|
c.JSON(http.StatusInternalServerError, Response{
|
|
Code: CodeServerError,
|
|
Msg: msg,
|
|
})
|
|
}
|