2025-04-18 17:17:23 +08:00
|
|
|
|
package context
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"admin/internal/errcode"
|
|
|
|
|
"admin/lib/web"
|
2025-04-22 15:46:48 +08:00
|
|
|
|
"admin/lib/xlog"
|
2025-04-18 17:17:23 +08:00
|
|
|
|
"context"
|
2025-04-22 15:46:48 +08:00
|
|
|
|
"github.com/gin-gonic/gin"
|
2025-04-18 17:17:23 +08:00
|
|
|
|
)
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
type WebHeader struct {
|
2025-05-05 10:30:33 +08:00
|
|
|
|
UserId int `json:"UserId"` // 用户id,会与下面token解析出用户id做匹配校验
|
|
|
|
|
Token string `json:"Token"` // jwt token,内置一些信息
|
|
|
|
|
Ip string `json:"IP"`
|
|
|
|
|
UserName string `json:"-"`
|
2025-04-30 15:46:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 17:17:23 +08:00
|
|
|
|
type WebContext struct {
|
|
|
|
|
context.Context
|
2025-04-22 15:46:48 +08:00
|
|
|
|
rawCtx *gin.Context
|
2025-04-30 15:46:14 +08:00
|
|
|
|
Header *WebHeader
|
2025-04-22 15:46:48 +08:00
|
|
|
|
alreadySetRsp bool
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 15:46:48 +08:00
|
|
|
|
func NewWebContext(rawCtx *gin.Context) web.IContext {
|
2025-05-07 15:03:19 +08:00
|
|
|
|
return &WebContext{Context: context.Background(), rawCtx: rawCtx}
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-30 15:46:14 +08:00
|
|
|
|
func (ctx *WebContext) ExtractHeader() error {
|
|
|
|
|
header := &WebHeader{}
|
|
|
|
|
err := ctx.rawCtx.ShouldBindHeader(header)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
ctx.Header = header
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *WebContext) IsUserLogin() bool {
|
|
|
|
|
return ctx.Header != nil && ctx.Header.UserId > 0
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-24 20:39:31 +08:00
|
|
|
|
func (ctx *WebContext) GinCtx() *gin.Context {
|
|
|
|
|
return ctx.rawCtx
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 17:17:23 +08:00
|
|
|
|
func (ctx *WebContext) Ok(data any) {
|
2025-04-22 15:46:48 +08:00
|
|
|
|
if ctx.alreadySetRsp {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ctx.rawCtx.JSON(200, map[string]any{
|
2025-04-18 17:17:23 +08:00
|
|
|
|
"code": 200,
|
|
|
|
|
"msg": "",
|
|
|
|
|
"data": data,
|
|
|
|
|
})
|
2025-04-22 15:46:48 +08:00
|
|
|
|
|
|
|
|
|
ctx.alreadySetRsp = true
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (ctx *WebContext) Fail(err error) {
|
2025-04-22 15:46:48 +08:00
|
|
|
|
if ctx.alreadySetRsp {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-18 17:17:23 +08:00
|
|
|
|
code, stack, msg := errcode.ParseError(err)
|
2025-04-22 15:46:48 +08:00
|
|
|
|
ctx.rawCtx.JSON(200, map[string]any{
|
2025-04-18 17:17:23 +08:00
|
|
|
|
"code": code,
|
|
|
|
|
"stack": stack,
|
|
|
|
|
"msg": msg,
|
|
|
|
|
"data": "",
|
|
|
|
|
})
|
2025-04-24 20:39:31 +08:00
|
|
|
|
|
2025-04-22 15:46:48 +08:00
|
|
|
|
ctx.alreadySetRsp = true
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|
|
|
|
|
|
2025-04-22 15:46:48 +08:00
|
|
|
|
func (ctx *WebContext) HandleError(path string, err error) {
|
|
|
|
|
xlog.Errorf("path:%v handle error:%v ", path, err)
|
|
|
|
|
ctx.Fail(err)
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|
2025-04-22 15:46:48 +08:00
|
|
|
|
func (ctx *WebContext) HandleSuccess(rspData any) {
|
|
|
|
|
ctx.Ok(rspData)
|
2025-04-18 17:17:23 +08:00
|
|
|
|
}
|