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.

40 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. func (m *Menu) GetMenuTree(authorityId float64) (err error, menus []Menu) {
  23. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", authorityId, 0).Find(&menus).Error
  24. for i := 0; i < len(menus); i++ {
  25. err = getChildrenList(&menus[i])
  26. }
  27. return err, menus
  28. }
  29. func getChildrenList(menu *Menu) (err error) {
  30. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", menu.AuthorityId, menu.ID).Find(&menu.Children).Error
  31. for i := 0; i < len(menu.Children); i++ {
  32. err = getChildrenList(&menu.Children[i])
  33. }
  34. return err
  35. }