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.

59 lines
1.7 KiB

  1. package sysModel
  2. import (
  3. "fmt"
  4. "gin-vue-admin/init/qmsql"
  5. )
  6. // menu需要构建的点有点多 这里关联关系表直接把所有数据拿过来 用代码实现关联 后期实现主外键模式
  7. type SysMenu struct {
  8. SysBaseMenu
  9. MenuId string `json:"menuId"`
  10. AuthorityId string `json:"-"`
  11. Children []SysMenu `json:"children"`
  12. }
  13. type Meta struct {
  14. Title string `json:"title"`
  15. Icon string `json:"icon"`
  16. }
  17. // 为角色增加menu树
  18. func (m *SysMenu) AddMenuAuthority(menus []SysBaseMenu, authorityId string) (err error) {
  19. var menu SysMenu
  20. qmsql.DEFAULTDB.Where("authority_id = ? ", authorityId).Unscoped().Delete(&SysMenu{})
  21. for _, v := range menus {
  22. menu.SysBaseMenu = v
  23. menu.AuthorityId = authorityId
  24. menu.MenuId = fmt.Sprintf("%v", v.ID)
  25. menu.ID = 0
  26. err = qmsql.DEFAULTDB.Create(&menu).Error
  27. if err != nil {
  28. return err
  29. }
  30. }
  31. return nil
  32. }
  33. // 查看当前角色树
  34. func (m *SysMenu) GetMenuAuthority(authorityId string) (err error, menus []SysMenu) {
  35. err = qmsql.DEFAULTDB.Where("authority_id = ?", authorityId).Find(&menus).Error
  36. return err, menus
  37. }
  38. //获取动态路由树
  39. func (m *SysMenu) GetMenuTree(authorityId string) (err error, menus []SysMenu) {
  40. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", authorityId, 0).Order("sort", true).Find(&menus).Error
  41. for i := 0; i < len(menus); i++ {
  42. err = getChildrenList(&menus[i])
  43. }
  44. return err, menus
  45. }
  46. func getChildrenList(menu *SysMenu) (err error) {
  47. err = qmsql.DEFAULTDB.Where("authority_id = ? AND parent_id = ?", menu.AuthorityId, menu.MenuId).Order("sort", true).Find(&menu.Children).Error
  48. for i := 0; i < len(menu.Children); i++ {
  49. err = getChildrenList(&menu.Children[i])
  50. }
  51. return err
  52. }