Files
nl-game-api/pkg/resp/resp.go
2026-08-14 13:17:03 +08:00

30 lines
924 B
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Package resp 提供统一的 JSON 响应格式:{code, msg, data}
// code=0 表示成功,非 0 表示业务失败
package resp
import (
"net/http"
"github.com/gin-gonic/gin"
)
// OK 成功响应data 为业务数据
func OK(c *gin.Context, data any) {
c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "ok", "data": data})
}
// Fail 业务失败响应HTTP 仍为 200靠 code 区分)
func Fail(c *gin.Context, msg string) {
c.JSON(http.StatusOK, gin.H{"code": 1, "msg": msg, "data": nil})
}
// Unauthorized 未登录/凭证失效响应HTTP 401前端据此跳转登录页
func Unauthorized(c *gin.Context, msg string) {
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"code": 401, "msg": msg, "data": nil})
}
// Forbidden 无权限响应HTTP 403
func Forbidden(c *gin.Context, msg string) {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"code": 403, "msg": msg, "data": nil})
}