| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package service
- import (
- "context"
- "dashoo.cn/opms_libary/myerrors"
- "github.com/gogf/gf/frame/g"
- "reflect"
- "dashoo.cn/opms_libary/micro_srv"
- "dashoo.cn/opms_libary/request"
- )
- // Context 上下文管理服务
- type contextService struct {
- Tenant string `json:"tenant"`
- Ctx context.Context `json:"ctx"`
- CxtUser *request.UserInfo `json:"cxtUser"`
- DataScope g.Map `json:"dataScope"`
- }
- // Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
- func (c *contextService) Init(ctx context.Context) (*contextService, error) {
- cs := ctx.Value("contextService")
- if cs != nil {
- return cs.(*contextService), nil
- }
- c = new(contextService)
- // 获取租户码
- tenant, err := micro_srv.GetTenant(ctx)
- if err != nil {
- return nil, myerrors.MicroCallError(err.Error())
- }
- c.Tenant = tenant
- c.CxtUser = nil
- if !micro_srv.IsAuthExclude(ctx) {
- userInfo, err := micro_srv.GetUserInfo(ctx)
- if err != nil {
- return nil, myerrors.MicroCallError(err.Error())
- }
- c.CxtUser = &userInfo
- c.DataScope = userInfo.DataScope
- }
- c.Ctx = context.WithValue(ctx, "contextService", c)
- return c, nil
- }
- // checkDataScopeWhere 检查结构体是否存在创建人字段
- func (c *contextService) checkDataScopeWhere(entity interface{}) error {
- t := reflect.TypeOf(entity)
- if t.Kind() == reflect.Ptr {
- t = t.Elem()
- }
- if _, ok := t.FieldByName("CreatedBy"); !ok {
- return myerrors.TipsError("结构体不存在创建人字段")
- }
- return 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) GetCxtUserDingtalkId() string {
- if c.CxtUser == nil {
- return "-1"
- }
- return c.CxtUser.DingtalkId
- }
- func (c *contextService) GetCxtUserDingtalkUid() string {
- if c.CxtUser == nil {
- return "-1"
- }
- return c.CxtUser.DingtalkUid
- }
- 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
- }
|