You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
1.8 KiB

/*
* @desc:context-service
* @company:云南奇讯科技有限公司
* @Author: yixiaohu
* @Date: 2022/3/16 14:46
*/
package service
import (
"context"
"github.com/gogf/gf/v2/net/ghttp"
"tyj_admin/internal/consts"
"tyj_admin/internal/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 contextService = contextServiceImpl{}
type contextServiceImpl struct{}
func Context() IContext {
return &contextService
}
// 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
}