sys_menu.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. )
  8. // @title GetMenuTree
  9. // @description 获取动态菜单树
  10. // @auth (2020/04/05 20:22 )
  11. // @param authorityId string
  12. // @return err error
  13. // @return menus []SysMenu
  14. func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) {
  15. sql := "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 = ?"
  16. err = global.GVA_DB.Raw(sql, authorityId, 0).Scan(&menus).Error
  17. for i := 0; i < len(menus); i++ {
  18. err = getChildrenList(&menus[i], sql)
  19. }
  20. return err, menus
  21. }
  22. // @title getChildrenList
  23. // @description 获取子菜单
  24. // @auth (2020/04/05 20:22 )
  25. // @param menu *SysMenu
  26. // @param SQLstatement string
  27. // @return err error
  28. func getChildrenList(menu *model.SysMenu, sql string) (err error) {
  29. err = global.GVA_DB.Raw(sql, menu.AuthorityId, menu.MenuId).Scan(&menu.Children).Error
  30. for i := 0; i < len(menu.Children); i++ {
  31. err = getChildrenList(&menu.Children[i], sql)
  32. }
  33. return err
  34. }
  35. // @title GetInfoList
  36. // @description 获取路由分页
  37. // @auth (2020/04/05 20:22 )
  38. // @param newPassword string
  39. // @return err error
  40. func GetInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  41. limit := info.PageSize
  42. offset := info.PageSize * (info.Page - 1)
  43. db := global.GVA_DB
  44. var menuList []model.SysBaseMenu
  45. err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  46. for i := 0; i < len(menuList); i++ {
  47. err = getBaseChildrenList(&menuList[i])
  48. }
  49. return err, menuList, total
  50. }
  51. // @title getBaseChildrenList
  52. // @description get children of menu, 获取菜单的子菜单
  53. // @auth (2020/04/05 20:22 )
  54. // @param menu *SysBaseMenu
  55. // @return err error
  56. func getBaseChildrenList(menu *model.SysBaseMenu) (err error) {
  57. err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  58. for i := 0; i < len(menu.Children); i++ {
  59. err = getBaseChildrenList(&menu.Children[i])
  60. }
  61. return err
  62. }
  63. // @title AddBaseMenu
  64. // @description 函数的详细描述
  65. // @auth (2020/04/05 20:22 )
  66. // @param newPassword string
  67. // @return err error
  68. //增加基础路由
  69. func AddBaseMenu(menu model.SysBaseMenu) (err error) {
  70. findOne := global.GVA_DB.Where("name = ?", menu.Name).Find(&model.SysBaseMenu{}).Error
  71. if findOne != nil {
  72. err = global.GVA_DB.Create(&menu).Error
  73. } else {
  74. err = errors.New("存在重复name,请修改name")
  75. }
  76. return err
  77. }
  78. // @title GetBaseMenuTree
  79. // @description 获取基础路由树
  80. // @auth (2020/04/05 20:22 )
  81. // @return err error
  82. // @return menus []SysBaseMenu
  83. func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) {
  84. err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  85. for i := 0; i < len(menus); i++ {
  86. err = getBaseChildrenList(&menus[i])
  87. }
  88. return err, menus
  89. }
  90. // @title AddMenuAuthority
  91. // @description 为角色增加menu树
  92. // @auth (2020/04/05 20:22 )
  93. // @param menus []SysBaseMenu
  94. // @param authorityId string
  95. // @return error
  96. func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) {
  97. var auth model.SysAuthority
  98. auth.AuthorityId = authorityId
  99. auth.SysBaseMenus = menus
  100. err = SetMenuAuthority(&auth)
  101. return err
  102. }
  103. // @title GetMenuAuthority
  104. // @description 查看当前角色树
  105. // @auth (2020/04/05 20:22 )
  106. // @param authorityId string
  107. // @return err error
  108. // @return menus []SysBaseMenu
  109. func GetMenuAuthority(authorityId string) (err error, menus []model.SysMenu) {
  110. sql := "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 = ?"
  111. err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
  112. return err, menus
  113. }