context.go 1.1 KB
package domain

import "gitlab.fjmaimaimai.com/allied-creation/character-library-metadata-bastion/pkg/constant"

type Context struct {
	// 公司
	CompanyId int `json:"companyId"`
	// 操作人
	OperatorId int `json:"operatorId"`
	// 操作人名称
	OperatorName string `json:"operatorName"`
	// 租户 (个人、企业)
	TenantId int `json:"tenantId"`
	// 应用键值
	AppKey string `json:"appKey"`
	// Token
	AccessToken string `json:"-"`
	// 附加数据
	data map[string]interface{}
}

func (c *Context) WithValue(key string, value interface{}) *Context {
	if c.data == nil {
		c.data = make(map[string]interface{})
	}
	if _, ok := c.data[key]; ok {
		return c
	}
	c.data[key] = value
	return c
}

func (c *Context) GetValue(key string) (interface{}, bool) {
	if c.data == nil {
		return nil, false
	}
	if v, ok := c.data[key]; ok {
		return v, true
	}
	return nil, false
}

const (
	ContextWithLogLevel = "WithLogLevel"
	ContextWithLogMsg   = "WithLogMsg"
)

func (c *Context) Access() bool {
	for _, userId := range constant.WHITE_LIST_USERS {
		if userId == c.OperatorId {
			return true
		}
	}
	return false
}