init: 毛孩子计划 小程序 + Go 后端 + 内嵌后台
- pets-fe: 微信原生小程序(首页/计划/记录/报告/社区/引导), 服务端驱动、无假数据;弹层改用 scroll-view,打开时隐藏自定义 tabBar - pets-be: Gin + GORM(MySQL, sundynix_ 前缀) + MinIO,统一响应/分页, 微信 code2session 登录,provider-neutral AI(DeepSeek),go:embed React 后台 - 修复:分段选择类型不匹配(字符串 vs 数字)导致选不中 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package response
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/sundynix/pets-be/pkg/errcode"
|
||||
)
|
||||
|
||||
// Body 统一响应体:{ code, message, data }
|
||||
type Body struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data"`
|
||||
}
|
||||
|
||||
// OK 成功响应(data 可为任意结构,含分页 PageResult)
|
||||
func OK(c *gin.Context, data any) {
|
||||
c.JSON(http.StatusOK, Body{
|
||||
Code: errcode.Success,
|
||||
Message: errcode.Message(errcode.Success),
|
||||
Data: data,
|
||||
})
|
||||
}
|
||||
|
||||
// OKMsg 带自定义文案的成功响应
|
||||
func OKMsg(c *gin.Context, message string, data any) {
|
||||
c.JSON(http.StatusOK, Body{Code: errcode.Success, Message: message, Data: data})
|
||||
}
|
||||
|
||||
// Fail 业务失败:HTTP 恒为 200,用 code 区分(前端统一按 code 判断)
|
||||
func Fail(c *gin.Context, code int, message string) {
|
||||
if message == "" {
|
||||
message = errcode.Message(code)
|
||||
}
|
||||
c.JSON(http.StatusOK, Body{Code: code, Message: message, Data: nil})
|
||||
}
|
||||
|
||||
// FailParams 参数错误快捷方法
|
||||
func FailParams(c *gin.Context, message string) {
|
||||
Fail(c, errcode.ErrParams, message)
|
||||
}
|
||||
|
||||
// FailErr 内部错误快捷方法(携带 error 文案)
|
||||
func FailErr(c *gin.Context, err error) {
|
||||
msg := errcode.Message(errcode.ErrInternal)
|
||||
if err != nil {
|
||||
msg = err.Error()
|
||||
}
|
||||
Fail(c, errcode.ErrInternal, msg)
|
||||
}
|
||||
|
||||
// Abort 在中间件中失败并中断(如鉴权失败)
|
||||
func Abort(c *gin.Context, code int, message string) {
|
||||
if message == "" {
|
||||
message = errcode.Message(code)
|
||||
}
|
||||
c.AbortWithStatusJSON(http.StatusOK, Body{Code: code, Message: message, Data: nil})
|
||||
}
|
||||
Reference in New Issue
Block a user