Set up project files and add .gitignore to exclude local build/runtime artifacts. Made-with: Cursor
118 lines
2.7 KiB
Go
118 lines
2.7 KiB
Go
package response
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
// Response 统一响应结构
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// ResponseSnakeCase 统一响应结构(snake_case)
|
|
type ResponseSnakeCase struct {
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
Data interface{} `json:"data,omitempty"`
|
|
}
|
|
|
|
// Success 成功响应
|
|
func Success(c *gin.Context, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: "success",
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// SuccessWithMessage 成功响应带消息
|
|
func SuccessWithMessage(c *gin.Context, message string, data interface{}) {
|
|
c.JSON(http.StatusOK, Response{
|
|
Code: 0,
|
|
Message: message,
|
|
Data: data,
|
|
})
|
|
}
|
|
|
|
// Error 错误响应
|
|
func Error(c *gin.Context, code int, message string) {
|
|
c.JSON(http.StatusBadRequest, Response{
|
|
Code: code,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// ErrorWithStatusCode 带状态码的错误响应
|
|
func ErrorWithStatusCode(c *gin.Context, statusCode int, code int, message string) {
|
|
c.JSON(statusCode, Response{
|
|
Code: code,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// BadRequest 参数错误
|
|
func BadRequest(c *gin.Context, message string) {
|
|
ErrorWithStatusCode(c, http.StatusBadRequest, 400, message)
|
|
}
|
|
|
|
// Unauthorized 未授权
|
|
func Unauthorized(c *gin.Context, message string) {
|
|
if message == "" {
|
|
message = "unauthorized"
|
|
}
|
|
ErrorWithStatusCode(c, http.StatusUnauthorized, 401, message)
|
|
}
|
|
|
|
// Forbidden 禁止访问
|
|
func Forbidden(c *gin.Context, message string) {
|
|
if message == "" {
|
|
message = "forbidden"
|
|
}
|
|
ErrorWithStatusCode(c, http.StatusForbidden, 403, message)
|
|
}
|
|
|
|
// NotFound 资源不存在
|
|
func NotFound(c *gin.Context, message string) {
|
|
if message == "" {
|
|
message = "resource not found"
|
|
}
|
|
ErrorWithStatusCode(c, http.StatusNotFound, 404, message)
|
|
}
|
|
|
|
// InternalServerError 服务器内部错误
|
|
func InternalServerError(c *gin.Context, message string) {
|
|
if message == "" {
|
|
message = "internal server error"
|
|
}
|
|
ErrorWithStatusCode(c, http.StatusInternalServerError, 500, message)
|
|
}
|
|
|
|
// PaginatedResponse 分页响应
|
|
type PaginatedResponse struct {
|
|
List interface{} `json:"list"`
|
|
Total int64 `json:"total"`
|
|
Page int `json:"page"`
|
|
PageSize int `json:"page_size"`
|
|
TotalPages int `json:"total_pages"`
|
|
}
|
|
|
|
// Paginated 分页成功响应
|
|
func Paginated(c *gin.Context, list interface{}, total int64, page, pageSize int) {
|
|
totalPages := int(total) / pageSize
|
|
if int(total)%pageSize > 0 {
|
|
totalPages++
|
|
}
|
|
|
|
Success(c, PaginatedResponse{
|
|
List: list,
|
|
Total: total,
|
|
Page: page,
|
|
PageSize: pageSize,
|
|
TotalPages: totalPages,
|
|
})
|
|
}
|