Files
novel/internal/logic/bizctx/bizctx.go
2024-01-12 18:08:51 +08:00

50 lines
1.1 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package bizctx
import (
"context"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"novel/internal/consts"
"novel/internal/model"
"novel/internal/service"
)
type sBizCtx struct{}
func init() {
service.RegisterBizCtx(New())
}
func New() *sBizCtx {
return &sBizCtx{}
}
// Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
func (s *sBizCtx) Init(r *ghttp.Request, customCtx *model.Context) {
r.SetCtxVar(consts.ContextKey, customCtx)
}
// Get 获得上下文变量如果没有设置那么返回nil
func (s *sBizCtx) Get(ctx context.Context) *model.Context {
value := ctx.Value(consts.ContextKey)
if value == nil {
return nil
}
if localCtx, ok := value.(*model.Context); ok {
return localCtx
}
return nil
}
// SetUser 将上下文信息设置到上下文请求中,注意是完整覆盖
func (s *sBizCtx) SetUser(ctx context.Context, ctxUser *model.ContextUser) {
s.Get(ctx).User = ctxUser
}
// SetData 将上下文信息设置到上下文请求中,注意是完整覆盖
func (s *sBizCtx) SetData(ctx context.Context, data g.Map) {
s.Get(ctx).Data = data
}