sys_apis.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. package system
  2. import (
  3. "go_server/model/common"
  4. "gorm.io/gorm"
  5. "strings"
  6. )
  7. type Apis struct {
  8. common.GormFullModel
  9. ParentId int64 `json:"parentId" gorm:"column:parent_id;comment:上级接口Id"`
  10. Group string `json:"group" gorm:"column:group;type:varchar(50);comment:分组名称"`
  11. Name string `json:"name" gorm:"column:name;type:varchar(50);comment:接口名称"`
  12. Method string `json:"method" gorm:"column:method;type:varchar(20);comment:请求方法"`
  13. Path string `json:"path" gorm:"column:path;type:varchar(80);comment:接口全路径;unique"`
  14. }
  15. func (*Apis) TableName() string {
  16. return common.ModelPrefix + "apis"
  17. }
  18. func NewApis() *Apis {
  19. return &Apis{}
  20. }
  21. func (*Apis) Comment() string {
  22. return "api权限表"
  23. }
  24. func (s *Apis) FindOrCreateAuth(db *gorm.DB, path, method string) (*Apis, error) {
  25. if err := db.Where("path", path).First(&s).Error; err != nil {
  26. if !strings.Contains(path, "*") {
  27. pathList := strings.Split(path, "/")
  28. if len(pathList) > 2 {
  29. s.Group = pathList[2]
  30. }
  31. s.ParentId = 1
  32. s.Path = path
  33. s.Method = method
  34. s.Name = "待设置"
  35. if err := db.Create(&s).Error; err != nil {
  36. return s, err
  37. }
  38. }
  39. return s, err
  40. }
  41. return s, nil
  42. }
  43. type ApiNode struct {
  44. Id int64 `json:"id"`
  45. ParentId int64 `json:"parentId"`
  46. Group string `json:"group"`
  47. Name string `json:"name"`
  48. Method string `json:"method"`
  49. Path string `json:"path"`
  50. Children []*ApiNode `json:"children"`
  51. }
  52. func (s *Apis) GetUserTree(db *gorm.DB, ids []string) ([]ApiNode, error) {
  53. menusNodeTree := make([]ApiNode, 0) // 根节点
  54. // 所有节点
  55. allMenus := make([]Apis, 0)
  56. if err := db.Model(&Apis{}).
  57. Where("id in (?)", ids).
  58. Find(&allMenus).Error; err != nil {
  59. return menusNodeTree, err
  60. }
  61. for _, m := range allMenus {
  62. childMenus := make([]*ApiNode, 0)
  63. if m.ParentId == 0 {
  64. rootNode := ApiNode{
  65. Id: m.ID,
  66. ParentId: m.ParentId,
  67. Group: m.Group,
  68. Name: m.Name,
  69. Method: m.Method,
  70. Path: m.Path,
  71. Children: childMenus,
  72. }
  73. menusNodeTree = append(menusNodeTree, rootNode)
  74. }
  75. }
  76. for i, _ := range menusNodeTree {
  77. s.walk(allMenus, &menusNodeTree[i])
  78. }
  79. return menusNodeTree, nil
  80. }
  81. // MenuTree 系统递归树[满足无限层级递归]
  82. func (s *Apis) SysTree(db *gorm.DB) ([]ApiNode, error) {
  83. menusNodeTree := make([]ApiNode, 0) // 根节点
  84. // 所有节点
  85. allMenus := make([]Apis, 0)
  86. if err := db.Model(&Apis{}).Where("1=1").Find(&allMenus).Error; err != nil {
  87. return menusNodeTree, err
  88. }
  89. for _, m := range allMenus {
  90. childMenus := make([]*ApiNode, 0)
  91. if m.ParentId == 0 {
  92. rootNode := ApiNode{
  93. Id: m.ID,
  94. ParentId: m.ParentId,
  95. Name: m.Name,
  96. Group: m.Group,
  97. Method: m.Method,
  98. Path: m.Path,
  99. Children: childMenus,
  100. }
  101. menusNodeTree = append(menusNodeTree, rootNode)
  102. }
  103. }
  104. for i, _ := range menusNodeTree {
  105. s.walk(allMenus, &menusNodeTree[i])
  106. }
  107. return menusNodeTree, nil
  108. }
  109. // 递归组装菜单树(根节点)
  110. func (s *Apis) walk(allMenus []Apis, rootNode *ApiNode) {
  111. // 列出所有下级子目录
  112. nodes := s.childrenList(allMenus, rootNode.Id)
  113. if len(nodes) == 0 {
  114. return
  115. }
  116. // 遍历这些文件
  117. for _, m := range nodes {
  118. newNode := ApiNode{
  119. Id: m.Id,
  120. ParentId: m.ParentId,
  121. Group: m.Group,
  122. Name: m.Name,
  123. Method: m.Method,
  124. Path: m.Path,
  125. Children: nil,
  126. }
  127. s.walk(allMenus, &newNode)
  128. rootNode.Children = append(rootNode.Children, &newNode)
  129. }
  130. return
  131. }
  132. // 获得子节点列表
  133. func (s *Apis) childrenList(allMenus []Apis, pId int64) (menusNodeTree []ApiNode) {
  134. for _, m := range allMenus {
  135. if m.ParentId == pId {
  136. rootNode := ApiNode{
  137. Id: m.ID,
  138. ParentId: m.ParentId,
  139. Name: m.Name,
  140. Group: m.Group,
  141. Method: m.Method,
  142. Path: m.Path,
  143. Children: nil,
  144. }
  145. menusNodeTree = append(menusNodeTree, rootNode)
  146. }
  147. }
  148. return
  149. }