27 lines
665 B
Go
27 lines
665 B
Go
package utils
|
||
|
||
import (
|
||
"github.com/gin-gonic/gin"
|
||
"net/http"
|
||
"time"
|
||
)
|
||
|
||
func JSON(c *gin.Context, httpStatus int, code int, message string, data interface{}) {
|
||
c.JSON(httpStatus, ApiResponse{
|
||
Code: code,
|
||
Message: message,
|
||
Data: data,
|
||
Timestamp: time.Now(), // 自动填充当前时间
|
||
})
|
||
}
|
||
|
||
// Success 成功响应(简化版,Code=200,Message="success")
|
||
func Success(c *gin.Context, data any) {
|
||
JSON(c, http.StatusOK, CodeSuccess, "success", data)
|
||
}
|
||
|
||
// Error 失败响应(简化版,自动匹配 HTTP 状态码)
|
||
func Error(c *gin.Context, httpStatus int, code int, message string) {
|
||
JSON(c, httpStatus, code, message, nil)
|
||
}
|