You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.0 KiB

  1. package dbModel
  2. import (
  3. "github.com/jinzhu/gorm"
  4. "main/init/qmsql"
  5. )
  6. type Menu struct {
  7. gorm.Model `json:"-"`
  8. MenuLevel uint `json:"-"`
  9. AuthorityId uint `json:"-"`
  10. ParentId uint `json:"parentId"`
  11. Path string `json:"path"`
  12. Name string `json:"name"`
  13. Hidden bool `json:"hidden"`
  14. Component string `json:"component"`
  15. Meta `json:"meta"`
  16. Children []Menu `json:"children"`
  17. }
  18. type Meta struct {
  19. Title string `json:"title"`
  20. Icon string `json:"icon"`
  21. }
  22. //获取动态路由树
  23. func (m *Menu) GetMenuTree(authorityId float64) (err error, menus []Menu) {
  24. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", authorityId, 0).Find(&menus).Error
  25. for i := 0; i < len(menus); i++ {
  26. err = getChildrenList(&menus[i])
  27. }
  28. return err, menus
  29. }
  30. func getChildrenList(menu *Menu) (err error) {
  31. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", menu.AuthorityId, menu.ID).Find(&menu.Children).Error
  32. for i := 0; i < len(menu.Children); i++ {
  33. err = getChildrenList(&menu.Children[i])
  34. }
  35. return err
  36. }