package context import ( "admin/internal/errcode" "admin/lib/web" "admin/lib/xlog" "context" "github.com/gin-gonic/gin" ) type WebContext struct { context.Context rawCtx *gin.Context alreadySetRsp bool } func NewWebContext(rawCtx *gin.Context) web.IContext { return &WebContext{rawCtx: rawCtx} } func (ctx *WebContext) GinCtx() *gin.Context { return ctx.rawCtx } func (ctx *WebContext) Ok(data any) { if ctx.alreadySetRsp { return } ctx.rawCtx.JSON(200, map[string]any{ "code": 200, "msg": "", "data": data, }) ctx.alreadySetRsp = true } func (ctx *WebContext) Fail(err error) { if ctx.alreadySetRsp { return } code, stack, msg := errcode.ParseError(err) ctx.rawCtx.JSON(200, map[string]any{ "code": code, "stack": stack, "msg": msg, "data": "", }) ctx.alreadySetRsp = true } func (ctx *WebContext) HandleError(path string, err error) { xlog.Errorf("path:%v handle error:%v ", path, err) ctx.Fail(err) } func (ctx *WebContext) HandleSuccess(rspData any) { ctx.Ok(rspData) }