| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172 |
- /*
- * @desc:context-service
- * @company:云南奇讯科技有限公司
- * @Author: yixiaohu
- * @Date: 2022/3/16 14:46
- */
- package service
- import (
- "context"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/tiger1103/gfast/v3/internal/app/system/consts"
- "github.com/tiger1103/gfast/v3/internal/app/system/model"
- )
- type IContext interface {
- Init(r *ghttp.Request, customCtx *model.Context)
- Get(ctx context.Context) *model.Context
- SetUser(ctx context.Context, ctxUser *model.ContextUser)
- GetLoginUser(ctx context.Context) *model.ContextUser
- GetUserId(ctx context.Context) uint64
- }
- // Context 上下文管理服务
- var contextImpl = contextServiceImpl{}
- type contextServiceImpl struct{}
- func Context() IContext {
- return IContext(&contextImpl)
- }
- // Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
- func (s *contextServiceImpl) Init(r *ghttp.Request, customCtx *model.Context) {
- r.SetCtxVar(consts.CtxKey, customCtx)
- }
- // Get 获得上下文变量,如果没有设置,那么返回nil
- func (s *contextServiceImpl) Get(ctx context.Context) *model.Context {
- value := ctx.Value(consts.CtxKey)
- if value == nil {
- return nil
- }
- if localCtx, ok := value.(*model.Context); ok {
- return localCtx
- }
- return nil
- }
- // SetUser 将上下文信息设置到上下文请求中,注意是完整覆盖
- func (s *contextServiceImpl) SetUser(ctx context.Context, ctxUser *model.ContextUser) {
- s.Get(ctx).User = ctxUser
- }
- // GetLoginUser 获取当前登陆用户信息
- func (s *contextServiceImpl) GetLoginUser(ctx context.Context) *model.ContextUser {
- context := s.Get(ctx)
- if context == nil {
- return nil
- }
- return context.User
- }
- // GetUserId 获取当前登录用户id
- func (s *contextServiceImpl) GetUserId(ctx context.Context) uint64 {
- user := s.GetLoginUser(ctx)
- if user != nil {
- return user.Id
- }
- return 0
- }
|