Files
photography/backend-old/pkg/response/response.go
xujiang 010fe2a8c7
Some checks failed
部署后端服务 / 🧪 测试后端 (push) Failing after 5m8s
部署后端服务 / 🚀 构建并部署 (push) Has been skipped
部署后端服务 / 🔄 回滚部署 (push) Has been skipped
fix
2025-07-10 18:09:11 +08:00

165 lines
3.6 KiB
Go

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),
},
}
}