-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresponse.go
More file actions
104 lines (94 loc) · 2.86 KB
/
response.go
File metadata and controls
104 lines (94 loc) · 2.86 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Package gin 提供基于 Gin 的增强上下文与相关组件。
package gin
import "time"
// Response 定义标准响应结构。
type Response struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
RequestID string `json:"request_id,omitempty"`
Timestamp int64 `json:"timestamp"`
}
// PaginatedResponse 定义带分页信息的响应结构。
type PaginatedResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
Pagination *Pagination `json:"pagination"`
RequestID string `json:"request_id,omitempty"`
Timestamp int64 `json:"timestamp"`
}
// Pagination 定义分页元信息。
type Pagination struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Total int64 `json:"total"`
TotalPages int `json:"total_pages"`
}
// ValidationError 定义字段级校验错误信息。
type ValidationError struct {
Field string `json:"field"` // 字段名
Tag string `json:"tag,omitempty"` // 验证标签(如 required, min, max)
Value any `json:"value,omitempty"` // 实际值(可选,调试用)
Param string `json:"param,omitempty"` // 验证参数(如 min=5 中的 5)
Message string `json:"message"` // 错误消息
}
// ErrorResponse 定义错误响应结构。
type ErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Errors []ValidationError `json:"errors,omitempty"`
RequestID string `json:"request_id,omitempty"`
Timestamp int64 `json:"timestamp"`
}
// NewPagination 生成分页元信息。
func NewPagination(page, perPage int, total int64) *Pagination {
if page < 1 {
page = 1
}
if perPage < 1 {
perPage = 1
}
if total < 0 {
total = 0
}
totalPages := 0
if total > 0 {
// ceil(total / perPage)
totalPages = int((total + int64(perPage) - 1) / int64(perPage))
}
return &Pagination{
Page: page,
PerPage: perPage,
Total: total,
TotalPages: totalPages,
}
}
func newResponse(code int, message string, data any, requestID string) Response {
return Response{
Code: code,
Message: message,
Data: data,
RequestID: requestID,
Timestamp: time.Now().Unix(),
}
}
func newPaginatedResponse(code int, message string, data any, pagination *Pagination, requestID string) PaginatedResponse {
return PaginatedResponse{
Code: code,
Message: message,
Data: data,
Pagination: pagination,
RequestID: requestID,
Timestamp: time.Now().Unix(),
}
}
func newErrorResponse(code int, message string, errors []ValidationError, requestID string) ErrorResponse {
return ErrorResponse{
Code: code,
Message: message,
Errors: errors,
RequestID: requestID,
Timestamp: time.Now().Unix(),
}
}