tree.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package service
  2. import (
  3. "context"
  4. "go-common/app/admin/ep/melloi/model"
  5. "go-common/library/ecode"
  6. )
  7. // QueryServiceTreeToken get tree token by user sessionID
  8. func (s *Service) QueryServiceTreeToken(c context.Context, sessionID string) (token string, err error) {
  9. return s.dao.QueryServiceTreeToken(c, sessionID)
  10. }
  11. // QueryUserRoleNode QueryUserRoleNode
  12. func (s *Service) QueryUserRoleNode(c context.Context, sessionID string) (apps []string, err error) {
  13. var (
  14. roleApp []*model.RoleApp
  15. token string
  16. )
  17. if token, err = s.QueryServiceTreeToken(c, sessionID); err != nil {
  18. return
  19. }
  20. if roleApp, err = s.dao.QueryUserRoleApp(c, token); err != nil {
  21. err = ecode.MerlinGetUserTreeFailed
  22. return
  23. }
  24. for _, app := range roleApp {
  25. // Role 1:管理员 2:研发 3:测试 4:运维 5:访客
  26. // Type 1.公司 2.部门 3.项目 4. 应用 5.环境 6.挂载点
  27. if app.Role <= 3 && app.Type == 4 {
  28. apps = append(apps, app.Name)
  29. }
  30. }
  31. return
  32. }
  33. // QueryUserTree get user tree
  34. func (s *Service) QueryUserTree(c context.Context, sessionID string) (firstRetMap []map[string]interface{}, err error) {
  35. var (
  36. treeMap *model.UserTree
  37. token string
  38. )
  39. if token, err = s.QueryServiceTreeToken(c, sessionID); err != nil {
  40. err = ecode.MerlinGetUserTreeFailed
  41. return
  42. }
  43. if treeMap, err = s.dao.QueryUserTree(c, token); err != nil {
  44. err = ecode.MerlinGetUserTreeFailed
  45. return
  46. }
  47. if treeMap.Bilibili == nil {
  48. return
  49. }
  50. firstLevelMapTmp := treeMap.Bilibili
  51. firstLevelMap := firstLevelMapTmp["children"].(map[string]interface{})
  52. if firstLevelMap == nil {
  53. return
  54. }
  55. for firstLevelKey, firstLevelChildren := range firstLevelMap {
  56. var secondRetMap []map[string]interface{}
  57. secondLevelMapTmp := firstLevelChildren.(map[string]interface{})
  58. if secondLevelMapTmp["children"] == nil {
  59. continue
  60. }
  61. secondLevelMap := secondLevelMapTmp["children"].(map[string]interface{})
  62. for secondLevelKey, secondLevelChildren := range secondLevelMap {
  63. var thirdRetMap []map[string]interface{}
  64. thirdLevelMapTmp := secondLevelChildren.(map[string]interface{})
  65. if thirdLevelMapTmp["children"] == nil {
  66. continue
  67. }
  68. thirdLevelMap := thirdLevelMapTmp["children"].(map[string]interface{})
  69. for thirdLevelKey, thirdLevelChildren := range thirdLevelMap {
  70. if thirdLevelKey != "" && thirdLevelChildren != nil {
  71. thirdTmp := make(map[string]interface{})
  72. thirdTmp["value"] = thirdLevelKey
  73. thirdTmp["label"] = thirdLevelKey
  74. thirdRetMap = append(thirdRetMap, thirdTmp)
  75. }
  76. }
  77. if secondLevelKey != "" {
  78. secondTmp := make(map[string]interface{})
  79. secondTmp["value"] = secondLevelKey
  80. secondTmp["label"] = secondLevelKey
  81. secondTmp["children"] = thirdRetMap
  82. secondRetMap = append(secondRetMap, secondTmp)
  83. }
  84. }
  85. if firstLevelKey != "" {
  86. firstTmp := make(map[string]interface{})
  87. firstTmp["value"] = firstLevelKey
  88. firstTmp["label"] = firstLevelKey
  89. firstTmp["children"] = secondRetMap
  90. firstRetMap = append(firstRetMap, firstTmp)
  91. }
  92. }
  93. return
  94. }
  95. // QueryTreeAdmin get tree admin
  96. func (s *Service) QueryTreeAdmin(c context.Context, path string, sessionID string) (roles []*model.TreeRole, err error) {
  97. var (
  98. tar *model.TreeAdminResponse
  99. token string
  100. )
  101. if token, err = s.QueryServiceTreeToken(c, sessionID); err != nil {
  102. return
  103. }
  104. if tar, err = s.dao.QueryTreeAdmin(c, path, token); err != nil {
  105. err = ecode.MerlinGetUserTreeFailed
  106. return
  107. }
  108. //todo: 如果没有服务树权限需要申请
  109. for _, v := range tar.Data {
  110. if v.Role == 1 {
  111. roles = append(roles, v)
  112. }
  113. }
  114. return
  115. }