Initial commit
This commit is contained in:
204
server/pkg/response/response.go
Normal file
204
server/pkg/response/response.go
Normal file
@@ -0,0 +1,204 @@
|
||||
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"`
|
||||
}
|
||||
|
||||
// PageData 分页数据结构
|
||||
type PageData struct {
|
||||
List interface{} `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
Page int `json:"page"`
|
||||
PageSize int `json:"page_size"`
|
||||
Pages int `json:"pages"`
|
||||
}
|
||||
|
||||
// 响应码定义
|
||||
const (
|
||||
SUCCESS = 200
|
||||
ERROR = 500
|
||||
|
||||
// 用户相关错误码
|
||||
ERROR_USER_NOT_FOUND = 10001
|
||||
ERROR_USER_ALREADY_EXIST = 10002
|
||||
ERROR_USER_DISABLED = 10003
|
||||
ERROR_INVALID_TOKEN = 10004
|
||||
ERROR_TOKEN_EXPIRED = 10005
|
||||
|
||||
// 商品相关错误码
|
||||
ERROR_PRODUCT_NOT_FOUND = 20001
|
||||
ERROR_PRODUCT_DISABLED = 20002
|
||||
ERROR_INSUFFICIENT_STOCK = 20003
|
||||
|
||||
// 订单相关错误码
|
||||
ERROR_ORDER_NOT_FOUND = 30001
|
||||
ERROR_ORDER_STATUS_ERROR = 30002
|
||||
ERROR_ORDER_CANNOT_PAY = 30003
|
||||
|
||||
// 参数相关错误码
|
||||
ERROR_INVALID_PARAMS = 40001
|
||||
ERROR_MISSING_PARAMS = 40002
|
||||
|
||||
// 权限相关错误码
|
||||
ERROR_UNAUTHORIZED = 40101
|
||||
ERROR_FORBIDDEN = 40103
|
||||
)
|
||||
|
||||
// 错误信息映射
|
||||
var codeMsg = map[int]string{
|
||||
SUCCESS: "操作成功",
|
||||
ERROR: "操作失败",
|
||||
|
||||
ERROR_USER_NOT_FOUND: "用户不存在",
|
||||
ERROR_USER_ALREADY_EXIST: "用户已存在",
|
||||
ERROR_USER_DISABLED: "用户已被禁用",
|
||||
ERROR_INVALID_TOKEN: "无效的token",
|
||||
ERROR_TOKEN_EXPIRED: "token已过期",
|
||||
|
||||
ERROR_PRODUCT_NOT_FOUND: "商品不存在",
|
||||
ERROR_PRODUCT_DISABLED: "商品已下架",
|
||||
ERROR_INSUFFICIENT_STOCK: "库存不足",
|
||||
|
||||
ERROR_ORDER_NOT_FOUND: "订单不存在",
|
||||
ERROR_ORDER_STATUS_ERROR: "订单状态错误",
|
||||
ERROR_ORDER_CANNOT_PAY: "订单无法支付",
|
||||
|
||||
ERROR_INVALID_PARAMS: "参数错误",
|
||||
ERROR_MISSING_PARAMS: "缺少必要参数",
|
||||
|
||||
ERROR_UNAUTHORIZED: "未授权",
|
||||
ERROR_FORBIDDEN: "权限不足",
|
||||
}
|
||||
|
||||
// GetMsg 获取错误信息
|
||||
func GetMsg(code int) string {
|
||||
msg, ok := codeMsg[code]
|
||||
if ok {
|
||||
return msg
|
||||
}
|
||||
return codeMsg[ERROR]
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: SUCCESS,
|
||||
Message: GetMsg(SUCCESS),
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// SuccessWithMessage 成功响应(自定义消息)
|
||||
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: SUCCESS,
|
||||
Message: message,
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func Error(c *gin.Context, code int) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: code,
|
||||
Message: GetMsg(code),
|
||||
})
|
||||
}
|
||||
|
||||
// ErrorWithMessage 错误响应(自定义消息)
|
||||
func ErrorWithMessage(c *gin.Context, code int, message string) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: code,
|
||||
Message: message,
|
||||
})
|
||||
}
|
||||
|
||||
// ErrorWithData 错误响应(带数据)
|
||||
func ErrorWithData(c *gin.Context, code int, data interface{}) {
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: code,
|
||||
Message: GetMsg(code),
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// Page 分页响应
|
||||
func Page(c *gin.Context, list interface{}, total int64, page, pageSize int) {
|
||||
pages := int(total) / pageSize
|
||||
if int(total)%pageSize > 0 {
|
||||
pages++
|
||||
}
|
||||
|
||||
Success(c, PageData{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Pages: pages,
|
||||
})
|
||||
}
|
||||
|
||||
// PageQuery 查询分页响应(不显示成功消息)
|
||||
func PageQuery(c *gin.Context, list interface{}, total int64, page, pageSize int) {
|
||||
pages := int(total) / pageSize
|
||||
if int(total)%pageSize > 0 {
|
||||
pages++
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, Response{
|
||||
Code: SUCCESS,
|
||||
Message: "",
|
||||
Data: PageData{
|
||||
List: list,
|
||||
Total: total,
|
||||
Page: page,
|
||||
PageSize: pageSize,
|
||||
Pages: pages,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// Unauthorized 未授权响应
|
||||
func Unauthorized(c *gin.Context) {
|
||||
c.JSON(http.StatusUnauthorized, Response{
|
||||
Code: ERROR_UNAUTHORIZED,
|
||||
Message: GetMsg(ERROR_UNAUTHORIZED),
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// Forbidden 权限不足响应
|
||||
func Forbidden(c *gin.Context) {
|
||||
c.JSON(http.StatusForbidden, Response{
|
||||
Code: ERROR_FORBIDDEN,
|
||||
Message: GetMsg(ERROR_FORBIDDEN),
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// BadRequest 参数错误响应
|
||||
func BadRequest(c *gin.Context, message string) {
|
||||
c.JSON(http.StatusBadRequest, Response{
|
||||
Code: ERROR_INVALID_PARAMS,
|
||||
Message: message,
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
|
||||
// InternalServerError 服务器内部错误响应
|
||||
func InternalServerError(c *gin.Context, message string) {
|
||||
c.JSON(http.StatusInternalServerError, Response{
|
||||
Code: ERROR,
|
||||
Message: message,
|
||||
})
|
||||
c.Abort()
|
||||
}
|
||||
Reference in New Issue
Block a user