| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- package service
- import (
- "context"
- "github.com/gogf/gf/os/glog"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/request"
- )
- // ContextService 上下文管理服务
- type ContextService struct {
- Tenant string `json:"tenant"`
- Table string `json:"table"`
- Ctx context.Context `json:"ctx"`
- CxtUser *request.UserInfo `json:"cxtUser"`
- }
- // Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
- func (c *ContextService) Init(ctx context.Context) (*ContextService, error) {
- cs := ctx.Value("contextService")
- if cs != nil {
- return cs.(*ContextService), nil
- }
- c = new(ContextService)
- c.Ctx = ctx
- // 获取租户码
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, err
- }
- reqMethod, _ := micro_srv.GetReqMethod(ctx)
- glog.Info("Received " + reqMethod + " request @ " + tenant)
- c.Tenant = tenant
- c.CxtUser = nil
- if !micro_srv.IsAuthExclude(ctx) {
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, err
- }
- c.CxtUser = &userInfo
- }
- c.Ctx = context.WithValue(ctx, "contextService", c)
- return c, nil
- }
- func (c *ContextService) GetCxtUserId() int {
- if c.CxtUser == nil {
- return -1
- }
- return c.CxtUser.Id
- }
- func (c *ContextService) GetCxtUserUuid() string {
- if c.CxtUser == nil {
- return "-1"
- }
- return c.CxtUser.Uuid
- }
- func (c *ContextService) GetCxtUserName() string {
- if c.CxtUser == nil {
- return "-1"
- }
- return c.CxtUser.NickName
- }
- func (c *ContextService) GetCxtUserDeptId() int {
- if c.CxtUser == nil {
- return -1
- }
- return c.CxtUser.DeptId
- }
- func (c *ContextService) GetCxtUserRoles() []string {
- if c.CxtUser == nil {
- return []string{}
- }
- return c.CxtUser.Roles
- }
- func (c *ContextService) GetCxtUserPosts() []string {
- if c.CxtUser == nil {
- return []string{}
- }
- return c.CxtUser.Posts
- }
- func (c *ContextService) GetCxtUserGroups() []string {
- if c.CxtUser == nil {
- return []string{}
- }
- return c.CxtUser.Groups
- }
|