sso

purview_service.go 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. package service
  2. import (
  3. "SSO/models"
  4. "fmt"
  5. "github.com/jinzhu/gorm"
  6. )
  7. func getAllOriginPurviews(appType int) ([]*models.Purview, error) {
  8. var purviews []*models.Purview
  9. getPurviewErr := readUserDb.Model(models.Purview{}).Where("module = ? AND status = 1", appType).Order("listorder asc").Order("id asc").Find(&purviews).Error
  10. if getPurviewErr != nil {
  11. if getPurviewErr == gorm.ErrRecordNotFound {
  12. return nil, nil
  13. } else {
  14. return nil, getPurviewErr
  15. }
  16. }
  17. return purviews, nil
  18. }
  19. func GetAllPurviewsProcessed(appType int) ([]*models.Purview, error) {
  20. originPurviews, getPurviewErr := getAllOriginPurviews(appType)
  21. if getPurviewErr != nil {
  22. return nil, getPurviewErr
  23. }
  24. return processPurviews2Tree(originPurviews), nil
  25. }
  26. // 加工这些规则:树形化
  27. // 正确结果的前提是 originPurviews 以 parentid asc 排好序了的
  28. func processPurviews2Tree(originPurviews []*models.Purview) []*models.Purview {
  29. processedPurviews := make([]*models.Purview, 0)
  30. pid_childs := make(map[int][]*models.Purview)
  31. for _, purview := range originPurviews {
  32. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  33. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  34. if purview.Parentid == 0 {
  35. processedPurviews = append(processedPurviews, purview)
  36. } else {
  37. childs := pid_childs[purview.Parentid]
  38. if pid_childs[purview.Parentid] == nil {
  39. childs = make([]*models.Purview, 0)
  40. }
  41. childs = append(childs, purview)
  42. pid_childs[purview.Parentid] = childs
  43. }
  44. }
  45. for _, proPurview := range processedPurviews {
  46. proPurview.Childs = pid_childs[proPurview.Id]
  47. }
  48. return processedPurviews
  49. }
  50. func GetSuperAdminUsersPurviewTreeAndUrlfors(appType int) ([]string, []*models.Purview, error) {
  51. originPurviews, getPurviewErr := getAllOriginPurviews(appType)
  52. if getPurviewErr != nil {
  53. return nil, nil, getPurviewErr
  54. }
  55. urlfors, processedPurviews := getUrlforsAndProcessPurviews2Tree(originPurviews)
  56. return urlfors, processedPurviews, nil
  57. }
  58. func GetGeneralAdminUsersPurviewTreeAndUrlfors(adminUserID int, appId int) ([]string, []*models.Purview, error) {
  59. rows, err := readUserDb.Raw("SELECT p.purview_ids AS pids FROM sgj_user_admin_role AS a_r, sgj_user_role_purview AS p WHERE a_r.admin_user_id = ? AND a_r.app_id = ? AND a_r.role_id = p.role_id AND a_r.status = 1 AND p.`status` = 1;", adminUserID, appId).Rows()
  60. defer rows.Close()
  61. if err != nil {
  62. if err == gorm.ErrRecordNotFound {
  63. return make([]string, 0), make([]*models.Purview, 0), nil
  64. } else {
  65. return nil, nil, err
  66. }
  67. }
  68. pids := ""
  69. if rows.Next() {
  70. rows.Scan(&pids)
  71. }
  72. if len(pids) == 0 {
  73. return make([]string, 0), make([]*models.Purview, 0), nil
  74. } else {
  75. var originPurviews []*models.Purview
  76. getPurviewErr := readUserDb.Model(&models.Purview{}).Where(fmt.Sprintf("id in (%v) and status = 1", pids)).Order("listorder asc").Order("id asc").Find(&originPurviews).Error
  77. if getPurviewErr != nil {
  78. if err == gorm.ErrRecordNotFound {
  79. return make([]string, 0), make([]*models.Purview, 0), nil
  80. } else {
  81. return nil, nil, err
  82. }
  83. } else {
  84. urlfors, processedPurviews := getUrlforsAndProcessPurviews2Tree(originPurviews)
  85. return urlfors, processedPurviews, nil
  86. }
  87. }
  88. }
  89. // 加工这些规则:树形化;以及从中取出不为空的 urlfor
  90. // 正确结果的前提是 originPurviews 以 parentid asc 排好序了的
  91. func getUrlforsAndProcessPurviews2Tree(originPurviews []*models.Purview) ([]string, []*models.Purview) {
  92. processedPurviews := make([]*models.Purview, 0)
  93. pid_childs := make(map[int][]*models.Purview)
  94. urlfors := make([]string, 0, len(originPurviews))
  95. for _, purview := range originPurviews {
  96. if len(purview.Urlfor) != 0 {
  97. urlfors = append(urlfors, purview.Urlfor)
  98. }
  99. // warning:下面这个算法只适用最多两层树形结构的菜单,对于两层以上的会丢失掉第三层及其以下的节点
  100. // 因为取出 originPurviews 的时候已经排过序了,所以顶级节点肯定最先处理,不需要担心子节点比父节点先处理
  101. if purview.Parentid == 0 {
  102. processedPurviews = append(processedPurviews, purview)
  103. } else {
  104. childs := pid_childs[purview.Parentid]
  105. if pid_childs[purview.Parentid] == nil {
  106. childs = make([]*models.Purview, 0)
  107. }
  108. childs = append(childs, purview)
  109. pid_childs[purview.Parentid] = childs
  110. }
  111. }
  112. for _, proPurview := range processedPurviews {
  113. proPurview.Childs = pid_childs[proPurview.Id]
  114. }
  115. return urlfors, processedPurviews
  116. }