Files
sundynix-site/server/internal/pkg/resp/resp.go
T
Blizzard 5abc705eae feat: 官网 + 管理端 + Gin/GORM 后端首个完整版本
- web/: 产品官网(Hero 事件流终端、功能矩阵、架构、快速开始、下载、博客 + Markdown 详情页),青瓷绿双主题
- admin/: 内容管理(JWT 登录、文章分页/搜索/CRUD、草稿与发布),embed 挂 /admin
- server/: Gin + GORM,MySQL(DSN 走 .env,SQLite 兜底);规范落地:sundynix_ 表前缀、字符串雪花主键、snake_case 列名、统一响应信封、公共分页参数
- 双 SPA embed 单二进制部署,make build 一键出包

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-07-17 11:03:31 +08:00

56 lines
1.4 KiB
Go

// Package resp 统一结果响应:{code, message, data}。
// code = 0 表示成功,非 0 为业务错误码。
package resp
import (
"net/http"
"github.com/gin-gonic/gin"
)
const (
CodeOK = 0
CodeBadRequest = 40000
CodeUnauthorized = 40100
CodeNotFound = 40400
CodeServerError = 50000
)
type Body struct {
Code int `json:"code"`
Message string `json:"message"`
Data any `json:"data,omitempty"`
}
// PageResult 分页数据统一结构。
type PageResult struct {
List any `json:"list"`
Total int64 `json:"total"`
Page int `json:"page"`
PageSize int `json:"page_size"`
}
func OK(c *gin.Context, data any) {
c.JSON(http.StatusOK, Body{Code: CodeOK, Message: "ok", Data: data})
}
func Page(c *gin.Context, list any, total int64, page, pageSize int) {
OK(c, PageResult{List: list, Total: total, Page: page, PageSize: pageSize})
}
func BadRequest(c *gin.Context, message string) {
c.JSON(http.StatusBadRequest, Body{Code: CodeBadRequest, Message: message})
}
func Unauthorized(c *gin.Context, message string) {
c.AbortWithStatusJSON(http.StatusUnauthorized, Body{Code: CodeUnauthorized, Message: message})
}
func NotFound(c *gin.Context, message string) {
c.JSON(http.StatusNotFound, Body{Code: CodeNotFound, Message: message})
}
func ServerError(c *gin.Context, message string) {
c.JSON(http.StatusInternalServerError, Body{Code: CodeServerError, Message: message})
}