116 lines
2.4 KiB
Go
Raw Normal View History

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-05-07 18:25:31 +08:00
"fmt"
2025-04-22 15:46:48 +08:00
"github.com/gin-gonic/gin"
2025-05-07 18:25:31 +08:00
"github.com/gin-gonic/gin/render"
"net/http"
"net/url"
"strings"
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-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-18 17:17:23 +08:00
})
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-05-07 18:25:31 +08:00
func (ctx *WebContext) OkFile(fileName string, content string) {
if ctx.alreadySetRsp {
return
}
reader := strings.NewReader(string(content))
contentLength := len(content)
contentType := ""
fileName = url.QueryEscape(fileName)
valueName := fmt.Sprintf(`"attachment; filename*=utf-8''%v"`, fileName)
extraHeaders := map[string]string{
"Content-Disposition": valueName,
"Content-Transfer-Encoding": "binary",
}
ctx.GinCtx().Render(http.StatusOK, render.Reader{
Headers: extraHeaders,
ContentType: contentType,
ContentLength: int64(contentLength),
Reader: reader,
})
ctx.alreadySetRsp = true
}
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
}