44 lines
735 B
Go
44 lines
735 B
Go
|
package context
|
||
|
|
||
|
import (
|
||
|
"admin/internal/errcode"
|
||
|
"admin/lib/web"
|
||
|
"context"
|
||
|
)
|
||
|
|
||
|
type WebContext struct {
|
||
|
context.Context
|
||
|
rawCtx web.RawContext
|
||
|
}
|
||
|
|
||
|
func NewWebContext() web.Context {
|
||
|
return &WebContext{}
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) Ok(data any) {
|
||
|
ctx.rawCtx.Json(200, map[string]any{
|
||
|
"code": 200,
|
||
|
"msg": "",
|
||
|
"data": data,
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) Fail(err error) {
|
||
|
code, stack, msg := errcode.ParseError(err)
|
||
|
ctx.rawCtx.Json(200, map[string]any{
|
||
|
"code": code,
|
||
|
"stack": stack,
|
||
|
"msg": msg,
|
||
|
"data": "",
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) SetRawContext(rawCtx web.RawContext) {
|
||
|
ctx.Context = context.Background()
|
||
|
ctx.rawCtx = rawCtx
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) GetRawContext() web.RawContext {
|
||
|
return ctx.rawCtx
|
||
|
}
|