45 lines
807 B
Go
Raw Normal View History

2025-04-22 15:46:48 +08:00
package server
import (
"admin/internal/errcode"
"admin/lib/web"
"context"
"github.com/gin-gonic/gin"
)
type WebContext struct {
context.Context
rawCtx *gin.Context
}
func NewWebContext(rawCtx *gin.Context) web.IContext {
return &WebContext{rawCtx: rawCtx}
}
func (ctx *WebContext) Ok(data any) {
ctx.rawCtx.JSON(200, map[string]any{
"code": 200,
"msg": "",
"data": data,
})
}
func (ctx *WebContext) Fail(err error) {
2025-05-08 17:33:46 +08:00
code, codeContent, stack, msg := errcode.ParseError(err)
2025-04-22 15:46:48 +08:00
ctx.rawCtx.JSON(200, map[string]any{
2025-05-08 17:33:46 +08:00
"code": code,
"stack": stack,
"msg": codeContent,
"detail_msg": msg,
"data": "",
2025-04-22 15:46:48 +08:00
})
}
func (ctx *WebContext) HandleError(path string, err error) {
ctx.Fail(err)
}
func (ctx *WebContext) HandleSuccess(rspData any) {
ctx.Ok(rspData)
}