-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathresponse.go
73 lines (64 loc) · 1.89 KB
/
response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package response
import (
"github.com/gofiber/fiber/v2"
)
type JSONResponse struct {
ErrCode Code `json:"errcode"`
RequestID string `json:"requestid"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// JSON 基础返回
func JSON(ctx *fiber.Ctx, status int, errcode Code, message string, data interface{}) error {
if message == "" {
message = CodeMap[errcode]
}
return ctx.Status(status).JSON(JSONResponse{
ErrCode: errcode,
Message: message,
RequestID: ctx.GetRespHeader("X-Request-Id"),
Data: data,
})
}
// SuccessJSON 成功返回
func SuccessJSON(ctx *fiber.Ctx, message string, data interface{}) error {
if message == "" {
message = Success.Msg()
}
return JSON(ctx, fiber.StatusOK, Success, message, data)
}
// BadRequestException 400错误
func BadRequestException(ctx *fiber.Ctx, message string) error {
if message == "" {
message = CodeMap[RequestParamErr]
}
return JSON(ctx, fiber.StatusBadRequest, RequestParamErr, message, nil)
}
// UnauthorizedException 401错误
func UnauthorizedException(ctx *fiber.Ctx, message string) error {
if message == "" {
message = CodeMap[UnAuthed]
}
return JSON(ctx, fiber.StatusUnauthorized, UnAuthed, message, nil)
}
// ForbiddenException 403错误
func ForbiddenException(ctx *fiber.Ctx, message string) error {
if message == "" {
message = CodeMap[Failed]
}
return JSON(ctx, fiber.StatusForbidden, Failed, message, nil)
}
// NotFoundException 404错误
func NotFoundException(ctx *fiber.Ctx, message string) error {
if message == "" {
message = CodeMap[RequestMethodErr]
}
return JSON(ctx, fiber.StatusNotFound, RequestMethodErr, message, nil)
}
// InternalServerException 500错误
func InternalServerException(ctx *fiber.Ctx, message string) error {
if message == "" {
message = CodeMap[InternalErr]
}
return JSON(ctx, fiber.StatusInternalServerError, InternalErr, message, nil)
}