You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.1 KiB
Go
51 lines
1.1 KiB
Go
![]()
4 weeks ago
|
package response
|
||
|
|
||
|
import (
|
||
|
"github.com/gin-gonic/gin"
|
||
|
"net/http"
|
||
|
)
|
||
|
|
||
|
// Response 统一响应结构
|
||
|
type Response 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,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// Error 错误响应
|
||
|
func Error(c *gin.Context, code int, message string) {
|
||
|
c.JSON(code, Response{
|
||
|
Code: code,
|
||
|
Message: message,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
// ValidationError 参数验证错误响应
|
||
|
func ValidationError(c *gin.Context, message string) {
|
||
|
Error(c, http.StatusBadRequest, message)
|
||
|
}
|
||
|
|
||
|
// ServerError 服务器错误响应
|
||
|
func ServerError(c *gin.Context, message string) {
|
||
|
Error(c, http.StatusInternalServerError, message)
|
||
|
}
|
||
|
|
||
|
// UnauthorizedError 未授权错误响应
|
||
|
func UnauthorizedError(c *gin.Context) {
|
||
|
Error(c, http.StatusUnauthorized, "未授权访问")
|
||
|
}
|
||
|
|
||
|
// NotFoundError 资源不存在错误响应
|
||
|
func NotFoundError(c *gin.Context) {
|
||
|
Error(c, http.StatusNotFound, "资源不存在")
|
||
|
}
|