fix
This commit is contained in:
165
backend-old/pkg/response/response.go
Normal file
165
backend-old/pkg/response/response.go
Normal file
@ -0,0 +1,165 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Response 统一响应结构
|
||||
type Response struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Meta 元数据
|
||||
type Meta struct {
|
||||
Timestamp string `json:"timestamp"`
|
||||
RequestID string `json:"request_id,omitempty"`
|
||||
}
|
||||
|
||||
// PaginatedResponse 分页响应
|
||||
type PaginatedResponse struct {
|
||||
Success bool `json:"success"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data"`
|
||||
Pagination *Pagination `json:"pagination"`
|
||||
Meta *Meta `json:"meta,omitempty"`
|
||||
}
|
||||
|
||||
// Pagination 分页信息
|
||||
type Pagination struct {
|
||||
Page int `json:"page"`
|
||||
Limit int `json:"limit"`
|
||||
Total int64 `json:"total"`
|
||||
TotalPages int `json:"total_pages"`
|
||||
HasNext bool `json:"has_next"`
|
||||
HasPrev bool `json:"has_prev"`
|
||||
}
|
||||
|
||||
// Success 成功响应
|
||||
func Success(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Error 错误响应
|
||||
func Error(code int, message string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: code,
|
||||
Message: message,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Created 创建成功响应
|
||||
func Created(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusCreated,
|
||||
Message: "Created successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Updated 更新成功响应
|
||||
func Updated(data interface{}) *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Updated successfully",
|
||||
Data: data,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Deleted 删除成功响应
|
||||
func Deleted() *Response {
|
||||
return &Response{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Deleted successfully",
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// Paginated 分页响应
|
||||
func Paginated(data interface{}, page, limit int, total int64) *PaginatedResponse {
|
||||
totalPages := int((total + int64(limit) - 1) / int64(limit))
|
||||
|
||||
return &PaginatedResponse{
|
||||
Success: true,
|
||||
Code: http.StatusOK,
|
||||
Message: "Success",
|
||||
Data: data,
|
||||
Pagination: &Pagination{
|
||||
Page: page,
|
||||
Limit: limit,
|
||||
Total: total,
|
||||
TotalPages: totalPages,
|
||||
HasNext: page < totalPages,
|
||||
HasPrev: page > 1,
|
||||
},
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// BadRequest 400错误
|
||||
func BadRequest(message string) *Response {
|
||||
return Error(http.StatusBadRequest, message)
|
||||
}
|
||||
|
||||
// Unauthorized 401错误
|
||||
func Unauthorized(message string) *Response {
|
||||
return Error(http.StatusUnauthorized, message)
|
||||
}
|
||||
|
||||
// Forbidden 403错误
|
||||
func Forbidden(message string) *Response {
|
||||
return Error(http.StatusForbidden, message)
|
||||
}
|
||||
|
||||
// NotFound 404错误
|
||||
func NotFound(message string) *Response {
|
||||
return Error(http.StatusNotFound, message)
|
||||
}
|
||||
|
||||
// InternalServerError 500错误
|
||||
func InternalServerError(message string) *Response {
|
||||
return Error(http.StatusInternalServerError, message)
|
||||
}
|
||||
|
||||
// ValidationError 验证错误
|
||||
func ValidationError(errors map[string]string) *Response {
|
||||
return &Response{
|
||||
Success: false,
|
||||
Code: http.StatusUnprocessableEntity,
|
||||
Message: "Validation failed",
|
||||
Data: errors,
|
||||
Meta: &Meta{
|
||||
Timestamp: time.Now().Format(time.RFC3339),
|
||||
},
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user