44 lines
745 B
Go
44 lines
745 B
Go
|
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) {
|
||
|
code, stack, msg := errcode.ParseError(err)
|
||
|
ctx.rawCtx.JSON(200, map[string]any{
|
||
|
"code": code,
|
||
|
"stack": stack,
|
||
|
"msg": msg,
|
||
|
"data": "",
|
||
|
})
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) HandleError(path string, err error) {
|
||
|
ctx.Fail(err)
|
||
|
}
|
||
|
|
||
|
func (ctx *WebContext) HandleSuccess(rspData any) {
|
||
|
ctx.Ok(rspData)
|
||
|
}
|