permission.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package frame
  2. /*var permissions []*TreePermission*/
  3. const (
  4. DIR = 1 //目录
  5. MENU = 2 //菜单
  6. AUTH = 3 //权限
  7. )
  8. // 权限输出
  9. type TreePermission struct {
  10. Label string `json:"label"`
  11. Id string `json:"id"`
  12. Children []*TreePermission `json:"children"`
  13. Pid string `json:"pid"`
  14. Cat int `json:"cat"`
  15. }
  16. type Permission struct {
  17. Cat int
  18. Code string
  19. Label string
  20. }
  21. /*func GetPermissions() []*TreePermission {
  22. return permissions
  23. }*/
  24. func (this *Permission) add(permissions *[]*TreePermission, parent *Permission, cat int, code string, label string) *[]*TreePermission {
  25. //判断是否查找到父节点,未查找到就直接放入数组中
  26. if parent == nil {
  27. parentVo := &TreePermission{
  28. Label: label,
  29. Id: code,
  30. Children: nil,
  31. Pid: "0",
  32. Cat: cat,
  33. }
  34. *permissions = append(*permissions, parentVo)
  35. } else {
  36. //找到code为parent的节点并添加进去
  37. parentVo := this.FindParent(*permissions, parent)
  38. if parentVo != nil {
  39. child := &TreePermission{
  40. Label: label,
  41. Id: code,
  42. Children: nil,
  43. Pid: parentVo.Id,
  44. Cat: cat,
  45. }
  46. if parentVo.Children == nil {
  47. childs := make([]*TreePermission, 0)
  48. parentVo.Children = childs
  49. }
  50. parentVo.Children = append(parentVo.Children, child)
  51. /* }*/
  52. }
  53. }
  54. return permissions
  55. }
  56. func (this *Permission) FindParent(permissions []*TreePermission, parent *Permission) *TreePermission {
  57. var vo *TreePermission
  58. for _, item := range permissions {
  59. if item.Id == parent.Code && item.Label == parent.Label {
  60. vo = item
  61. }
  62. if vo == nil {
  63. if item.Children != nil && len(item.Children) > 0 {
  64. vo = this.FindSubParent(item.Children, parent)
  65. }
  66. }
  67. }
  68. return vo
  69. }
  70. func (this *Permission) FindSubParent(permissions []*TreePermission, parent *Permission) *TreePermission {
  71. for _, item := range permissions {
  72. if item.Id == parent.Code {
  73. return item
  74. }
  75. if item.Children != nil && len(item.Children) > 0 {
  76. for _, child := range item.Children {
  77. if child.Id == parent.Code && child.Label == parent.Label {
  78. return child
  79. }
  80. if child.Children != nil && len(child.Children) > 0 {
  81. return this.FindSubParent(child.Children, parent)
  82. }
  83. }
  84. }
  85. }
  86. return nil
  87. }