package service import ( "context" "dashoo.cn/micro/app/model" "github.com/gogf/gf/net/ghttp" ) // Context 上下文管理服务 var Context = new(contextService) type contextService struct{} // Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。 func (s *contextService) Init(r *ghttp.Request, customCtx *model.Context) { r.SetCtxVar(model.CtxKey, customCtx) } // Get 获得上下文变量,如果没有设置,那么返回nil func (s *contextService) Get(ctx context.Context) *model.Context { value := ctx.Value(model.CtxKey) if value == nil { return nil } if localCtx, ok := value.(*model.Context); ok { return localCtx } return nil } // SetUser 将上下文信息设置到上下文请求中,注意是完整覆盖 func (s *contextService) SetUser(ctx context.Context, ctxUser *model.CtxUser) { s.Get(ctx).User = ctxUser }