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.

68 lines
2.9 KiB

  1. package model
  2. import (
  3. "gin-vue-admin/global"
  4. )
  5. type SysMenu struct {
  6. SysBaseMenu
  7. MenuId string `json:"menuId"`
  8. AuthorityId string `json:"-"`
  9. Children []SysMenu `json:"children"`
  10. }
  11. // @title AddMenuAuthority
  12. // @description 为角色增加menu树
  13. // @auth (2020/04/05 20:22 )
  14. // @param menus []SysBaseMenu
  15. // @param authorityId string
  16. // @return error
  17. func (m *SysMenu) AddMenuAuthority(menus []SysBaseMenu, authorityId string) (err error) {
  18. var auth SysAuthority
  19. auth.AuthorityId = authorityId
  20. auth.SysBaseMenus = menus
  21. err = auth.SetMuneAuthority()
  22. return err
  23. }
  24. // @title GetMenuAuthority
  25. // @description 查看当前角色树
  26. // @auth (2020/04/05 20:22 )
  27. // @param authorityId string
  28. // @return err error
  29. // @return menus []SysBaseMenu
  30. func (m *SysMenu) GetMenuAuthority(authorityId string) (err error, menus []SysMenu) {
  31. 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 = ?"
  32. err = global.GVA_DB.Raw(SQLstatement, authorityId).Scan(&menus).Error
  33. return err, menus
  34. }
  35. // @title GetMenuTree
  36. // @description 获取动态菜单树
  37. // @auth (2020/04/05 20:22 )
  38. // @param authorityId string
  39. // @return err error
  40. // @return menus []SysMenu
  41. func (m *SysMenu) GetMenuTree(authorityId string) (err error, menus []SysMenu) {
  42. 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 = ?"
  43. err = global.GVA_DB.Raw(SQLstatement, authorityId, 0).Scan(&menus).Error
  44. for i := 0; i < len(menus); i++ {
  45. err = getChildrenList(&menus[i], SQLstatement)
  46. }
  47. return err, menus
  48. }
  49. // @title getChildrenList
  50. // @description 获取子菜单
  51. // @auth (2020/04/05 20:22 )
  52. // @param menu *SysMenu
  53. // @param SQLstatement string
  54. // @return err error
  55. func getChildrenList(menu *SysMenu, SQLstatement string) (err error) {
  56. err = global.GVA_DB.Raw(SQLstatement, menu.AuthorityId, menu.MenuId).Scan(&menu.Children).Error
  57. for i := 0; i < len(menu.Children); i++ {
  58. err = getChildrenList(&menu.Children[i], SQLstatement)
  59. }
  60. return err
  61. }