context.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. package service
  2. import (
  3. "context"
  4. "dashoo.cn/opms_libary/myerrors"
  5. "github.com/gogf/gf/frame/g"
  6. "reflect"
  7. "dashoo.cn/opms_libary/micro_srv"
  8. "dashoo.cn/opms_libary/request"
  9. )
  10. // Context 上下文管理服务
  11. type contextService struct {
  12. Tenant string `json:"tenant"`
  13. Ctx context.Context `json:"ctx"`
  14. CxtUser *request.UserInfo `json:"cxtUser"`
  15. DataScope g.Map `json:"dataScope"`
  16. }
  17. // Init 初始化上下文对象指针到上下文对象中,以便后续的请求流程中可以修改。
  18. func (c *contextService) Init(ctx context.Context) (*contextService, error) {
  19. cs := ctx.Value("contextService")
  20. if cs != nil {
  21. return cs.(*contextService), nil
  22. }
  23. c = new(contextService)
  24. // 获取租户码
  25. tenant, err := micro_srv.GetTenant(ctx)
  26. if err != nil {
  27. return nil, myerrors.MicroCallError(err.Error())
  28. }
  29. c.Tenant = tenant
  30. c.CxtUser = nil
  31. if !micro_srv.IsAuthExclude(ctx) {
  32. userInfo, err := micro_srv.GetUserInfo(ctx)
  33. if err != nil {
  34. return nil, myerrors.MicroCallError(err.Error())
  35. }
  36. c.CxtUser = &userInfo
  37. c.DataScope = userInfo.DataScope
  38. }
  39. c.Ctx = context.WithValue(ctx, "contextService", c)
  40. return c, nil
  41. }
  42. // checkDataScopeWhere 检查结构体是否存在创建人字段
  43. func (c *contextService) checkDataScopeWhere(entity interface{}) error {
  44. t := reflect.TypeOf(entity)
  45. if t.Kind() == reflect.Ptr {
  46. t = t.Elem()
  47. }
  48. if _, ok := t.FieldByName("CreatedBy"); !ok {
  49. return myerrors.TipsError("结构体不存在创建人字段")
  50. }
  51. return nil
  52. }
  53. func (c *contextService) GetCxtUserId() int {
  54. if c.CxtUser == nil {
  55. return -1
  56. }
  57. return c.CxtUser.Id
  58. }
  59. func (c *contextService) GetCxtUserUuid() string {
  60. if c.CxtUser == nil {
  61. return "-1"
  62. }
  63. return c.CxtUser.Uuid
  64. }
  65. func (c *contextService) GetCxtUserName() string {
  66. if c.CxtUser == nil {
  67. return "-1"
  68. }
  69. return c.CxtUser.NickName
  70. }
  71. func (c *contextService) GetCxtUserDingtalkId() string {
  72. if c.CxtUser == nil {
  73. return "-1"
  74. }
  75. return c.CxtUser.DingtalkId
  76. }
  77. func (c *contextService) GetCxtUserDingtalkUid() string {
  78. if c.CxtUser == nil {
  79. return "-1"
  80. }
  81. return c.CxtUser.DingtalkUid
  82. }
  83. func (c *contextService) GetCxtUserDeptId() int {
  84. if c.CxtUser == nil {
  85. return -1
  86. }
  87. return c.CxtUser.DeptId
  88. }
  89. func (c *contextService) GetCxtUserRoles() []string {
  90. if c.CxtUser == nil {
  91. return []string{}
  92. }
  93. return c.CxtUser.Roles
  94. }
  95. func (c *contextService) GetCxtUserPosts() []string {
  96. if c.CxtUser == nil {
  97. return []string{}
  98. }
  99. return c.CxtUser.Posts
  100. }
  101. func (c *contextService) GetCxtUserGroups() []string {
  102. if c.CxtUser == nil {
  103. return []string{}
  104. }
  105. return c.CxtUser.Groups
  106. }