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.

48 lines
1.8 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. )
  5. // menu需要构建的点有点多 这里关联关系表直接把所有数据拿过来 用代码实现关联 后期实现主外键模式
  6. type SysMenu struct {
  7. SysBaseMenu
  8. MenuID string `json:"menuId"`
  9. AuthorityId string `json:"-"`
  10. Children []SysMenu `json:"children"`
  11. }
  12. // 为角色增加menu树
  13. func (m *SysMenu) AddMenuAuthority(menus []SysBaseMenu, authorityId string) (err error) {
  14. var auth SysAuthority
  15. auth.AuthorityId = authorityId
  16. auth.SysBaseMenus = menus
  17. auth.SetMuneAuthority()
  18. return nil
  19. }
  20. // 查看当前角色树
  21. func (m *SysMenu) GetMenuAuthority(authorityId string) (err error, menus []SysBaseMenu) {
  22. var a SysAuthority
  23. err = global.GVA_DB.Preload("SysBaseMenus").Where("authority_id = ?", authorityId).First(&a).Error
  24. return err, a.SysBaseMenus
  25. }
  26. //获取动态路由树
  27. func (m *SysMenu) GetMenuTree(authorityId string) (err error, menus []SysMenu) {
  28. SQLstatement := "SELECT authority_menu.created_at,authority_menu.updated_at,authority_menu.deleted_at,authority_menu.menu_level,authority_menu.parent_id,authority_menu.path,authority_menu.`name`,authority_menu.hidden,authority_menu.component,authority_menu.title,authority_menu.icon,authority_menu.sort,authority_menu.menu_id,authority_menu.authority_id FROM authority_menu WHERE authority_menu.authority_id = ? AND authority_menu.parent_id = ?"
  29. err = global.GVA_DB.Raw(SQLstatement, authorityId, 0).Scan(&menus).Error
  30. for i := 0; i < len(menus); i++ {
  31. err = getChildrenList(&menus[i], SQLstatement)
  32. }
  33. return err, menus
  34. }
  35. func getChildrenList(menu *SysMenu, SQLstatement string) (err error) {
  36. err = global.GVA_DB.Raw(SQLstatement, menu.AuthorityId, menu.MenuID).Scan(&menu.Children).Error
  37. for i := 0; i < len(menu.Children); i++ {
  38. err = getChildrenList(&menu.Children[i], SQLstatement)
  39. }
  40. return err
  41. }