2025-07-11 00:49:41 +08:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2025-07-11 17:45:16 +08:00
|
|
|
"net/http"
|
|
|
|
|
|
2025-07-11 00:49:41 +08:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
)
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
// Response 统一响应格式
|
|
|
|
|
type Response struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Message string `json:"message"`
|
|
|
|
|
Data interface{} `json:"data"`
|
|
|
|
|
Code int `json:"code"`
|
2025-07-11 00:49:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// SuccessResponse 成功响应
|
2025-07-11 17:45:16 +08:00
|
|
|
func SuccessResponse(c *gin.Context, data interface{}) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
2025-07-11 00:49:41 +08:00
|
|
|
Success: true,
|
2025-07-11 17:45:16 +08:00
|
|
|
Message: "操作成功",
|
2025-07-11 00:49:41 +08:00
|
|
|
Data: data,
|
2025-07-11 17:45:16 +08:00
|
|
|
Code: 200,
|
2025-07-11 00:49:41 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ErrorResponse 错误响应
|
2025-07-11 17:45:16 +08:00
|
|
|
func ErrorResponse(c *gin.Context, message string, code int) {
|
|
|
|
|
if code == 0 {
|
|
|
|
|
code = 500
|
|
|
|
|
}
|
|
|
|
|
c.JSON(code, Response{
|
2025-07-11 00:49:41 +08:00
|
|
|
Success: false,
|
2025-07-11 17:45:16 +08:00
|
|
|
Message: message,
|
|
|
|
|
Data: nil,
|
|
|
|
|
Code: code,
|
2025-07-11 00:49:41 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
// ListResponse 列表响应
|
|
|
|
|
func ListResponse(c *gin.Context, data interface{}, total int64) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
2025-07-11 00:49:41 +08:00
|
|
|
Success: true,
|
2025-07-11 17:45:16 +08:00
|
|
|
Message: "获取成功",
|
|
|
|
|
Data: gin.H{
|
|
|
|
|
"list": data,
|
|
|
|
|
"total": total,
|
2025-07-11 00:49:41 +08:00
|
|
|
},
|
2025-07-11 17:45:16 +08:00
|
|
|
Code: 200,
|
2025-07-11 00:49:41 +08:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-11 17:45:16 +08:00
|
|
|
// PageResponse 分页响应
|
|
|
|
|
func PageResponse(c *gin.Context, data interface{}, total int64, page, limit int) {
|
|
|
|
|
c.JSON(http.StatusOK, Response{
|
2025-07-11 00:49:41 +08:00
|
|
|
Success: true,
|
2025-07-11 17:45:16 +08:00
|
|
|
Message: "获取成功",
|
|
|
|
|
Data: gin.H{
|
|
|
|
|
"list": data,
|
|
|
|
|
"total": total,
|
|
|
|
|
"page": page,
|
|
|
|
|
"limit": limit,
|
|
|
|
|
},
|
|
|
|
|
Code: 200,
|
2025-07-11 00:49:41 +08:00
|
|
|
})
|
|
|
|
|
}
|