处理返回编码未设置的问题
This commit is contained in:
@@ -111,12 +111,14 @@ func handleError(c *fiber.Ctx, err error, logger *zap.Logger) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 6. 返回统一 JSON 响应
|
// 6. 返回统一 JSON 响应
|
||||||
return c.Status(httpStatus).JSON(fiber.Map{
|
errResp := c.Status(httpStatus).JSON(fiber.Map{
|
||||||
"code": code,
|
"code": code,
|
||||||
"data": nil,
|
"data": nil,
|
||||||
"msg": message,
|
"msg": message,
|
||||||
"timestamp": time.Now().Format(time.RFC3339),
|
"timestamp": time.Now().Format(time.RFC3339),
|
||||||
})
|
})
|
||||||
|
c.Set(fiber.HeaderContentType, "application/json; charset=utf-8")
|
||||||
|
return errResp
|
||||||
}
|
}
|
||||||
|
|
||||||
// safeLog 安全地记录日志,日志失败时静默处理(默认 Error 级别)
|
// safeLog 安全地记录日志,日志失败时静默处理(默认 Error 级别)
|
||||||
|
|||||||
@@ -17,22 +17,26 @@ type Response struct {
|
|||||||
|
|
||||||
// Success 返回成功响应
|
// Success 返回成功响应
|
||||||
func Success(c *fiber.Ctx, data any) error {
|
func Success(c *fiber.Ctx, data any) error {
|
||||||
return c.JSON(Response{
|
err := c.JSON(Response{
|
||||||
Code: errors.CodeSuccess,
|
Code: errors.CodeSuccess,
|
||||||
Data: data,
|
Data: data,
|
||||||
Message: "success",
|
Message: "success",
|
||||||
Timestamp: time.Now().Format(time.RFC3339),
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
})
|
})
|
||||||
|
c.Set(fiber.HeaderContentType, "application/json; charset=utf-8")
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SuccessWithMessage 返回带自定义消息的成功响应
|
// SuccessWithMessage 返回带自定义消息的成功响应
|
||||||
func SuccessWithMessage(c *fiber.Ctx, data any, message string) error {
|
func SuccessWithMessage(c *fiber.Ctx, data any, message string) error {
|
||||||
return c.JSON(Response{
|
err := c.JSON(Response{
|
||||||
Code: errors.CodeSuccess,
|
Code: errors.CodeSuccess,
|
||||||
Data: data,
|
Data: data,
|
||||||
Message: message,
|
Message: message,
|
||||||
Timestamp: time.Now().Format(time.RFC3339),
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
})
|
})
|
||||||
|
c.Set(fiber.HeaderContentType, "application/json; charset=utf-8")
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PaginationData 分页数据结构
|
// PaginationData 分页数据结构
|
||||||
@@ -45,7 +49,7 @@ type PaginationData struct {
|
|||||||
|
|
||||||
// SuccessWithPagination 返回分页响应
|
// SuccessWithPagination 返回分页响应
|
||||||
func SuccessWithPagination(c *fiber.Ctx, items any, total int64, page, size int) error {
|
func SuccessWithPagination(c *fiber.Ctx, items any, total int64, page, size int) error {
|
||||||
return c.JSON(Response{
|
err := c.JSON(Response{
|
||||||
Code: errors.CodeSuccess,
|
Code: errors.CodeSuccess,
|
||||||
Data: PaginationData{
|
Data: PaginationData{
|
||||||
Items: items,
|
Items: items,
|
||||||
@@ -56,4 +60,6 @@ func SuccessWithPagination(c *fiber.Ctx, items any, total int64, page, size int)
|
|||||||
Message: "success",
|
Message: "success",
|
||||||
Timestamp: time.Now().Format(time.RFC3339),
|
Timestamp: time.Now().Format(time.RFC3339),
|
||||||
})
|
})
|
||||||
|
c.Set(fiber.HeaderContentType, "application/json; charset=utf-8")
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user