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.

153 lines
5.2 KiB

  1. package service
  2. import (
  3. "errors"
  4. "gin-vue-admin/global"
  5. "gin-vue-admin/model"
  6. "gin-vue-admin/model/request"
  7. "gorm.io/gorm"
  8. "strconv"
  9. )
  10. //@author: [piexlmax](https://github.com/piexlmax)
  11. //@function: getMenuTreeMap
  12. //@description: 获取路由总树map
  13. //@param: authorityId string
  14. //@return: err error, treeMap map[string][]model.SysMenu
  15. func getMenuTreeMap(authorityId string) (err error, treeMap map[string][]model.SysMenu) {
  16. var allMenus []model.SysMenu
  17. treeMap = make(map[string][]model.SysMenu)
  18. err = global.GVA_DB.Where("authority_id = ?", authorityId).Order("sort").Preload("Parameters").Find(&allMenus).Error
  19. for _, v := range allMenus {
  20. treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
  21. }
  22. return err, treeMap
  23. }
  24. //@author: [piexlmax](https://github.com/piexlmax)
  25. //@function: GetMenuTree
  26. //@description: 获取动态菜单树
  27. //@param: authorityId string
  28. //@return: err error, menus []model.SysMenu
  29. func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) {
  30. err, menuTree := getMenuTreeMap(authorityId)
  31. menus = menuTree["0"]
  32. for i := 0; i < len(menus); i++ {
  33. err = getChildrenList(&menus[i], menuTree)
  34. }
  35. return err, menus
  36. }
  37. //@author: [piexlmax](https://github.com/piexlmax)
  38. //@function: getChildrenList
  39. //@description: 获取子菜单
  40. //@param: menu *model.SysMenu, treeMap map[string][]model.SysMenu
  41. //@return: err error
  42. func getChildrenList(menu *model.SysMenu, treeMap map[string][]model.SysMenu) (err error) {
  43. menu.Children = treeMap[menu.MenuId]
  44. for i := 0; i < len(menu.Children); i++ {
  45. err = getChildrenList(&menu.Children[i], treeMap)
  46. }
  47. return err
  48. }
  49. //@author: [piexlmax](https://github.com/piexlmax)
  50. //@function: GetInfoList
  51. //@description: 获取路由分页
  52. //@return: err error, list interface{}, total int64
  53. func GetInfoList() (err error, list interface{}, total int64) {
  54. var menuList []model.SysBaseMenu
  55. err, treeMap := getBaseMenuTreeMap()
  56. menuList = treeMap["0"]
  57. for i := 0; i < len(menuList); i++ {
  58. err = getBaseChildrenList(&menuList[i], treeMap)
  59. }
  60. return err, menuList, total
  61. }
  62. //@author: [piexlmax](https://github.com/piexlmax)
  63. //@function: getBaseChildrenList
  64. //@description: 获取菜单的子菜单
  65. //@param: menu *model.SysBaseMenu, treeMap map[string][]model.SysBaseMenu
  66. //@return: err error
  67. func getBaseChildrenList(menu *model.SysBaseMenu, treeMap map[string][]model.SysBaseMenu) (err error) {
  68. menu.Children = treeMap[strconv.Itoa(int(menu.ID))]
  69. for i := 0; i < len(menu.Children); i++ {
  70. err = getBaseChildrenList(&menu.Children[i], treeMap)
  71. }
  72. return err
  73. }
  74. //@author: [piexlmax](https://github.com/piexlmax)
  75. //@function: AddBaseMenu
  76. //@description: 添加基础路由
  77. //@param: menu model.SysBaseMenu
  78. //@return: error
  79. func AddBaseMenu(menu model.SysBaseMenu) error {
  80. if !errors.Is(global.GVA_DB.Where("name = ?", menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
  81. return errors.New("存在重复name,请修改name")
  82. }
  83. return global.GVA_DB.Create(&menu).Error
  84. }
  85. //@author: [piexlmax](https://github.com/piexlmax)
  86. //@function: getBaseMenuTreeMap
  87. //@description: 获取路由总树map
  88. //@return: err error, treeMap map[string][]model.SysBaseMenu
  89. func getBaseMenuTreeMap() (err error, treeMap map[string][]model.SysBaseMenu) {
  90. var allMenus []model.SysBaseMenu
  91. treeMap = make(map[string][]model.SysBaseMenu)
  92. err = global.GVA_DB.Order("sort").Preload("Parameters").Find(&allMenus).Error
  93. for _, v := range allMenus {
  94. treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
  95. }
  96. return err, treeMap
  97. }
  98. //@author: [piexlmax](https://github.com/piexlmax)
  99. //@function: GetBaseMenuTree
  100. //@description: 获取基础路由树
  101. //@return: err error, menus []model.SysBaseMenu
  102. func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) {
  103. err, treeMap := getBaseMenuTreeMap()
  104. menus = treeMap["0"]
  105. for i := 0; i < len(menus); i++ {
  106. err = getBaseChildrenList(&menus[i], treeMap)
  107. }
  108. return err, menus
  109. }
  110. //@author: [piexlmax](https://github.com/piexlmax)
  111. //@function: AddMenuAuthority
  112. //@description: 为角色增加menu树
  113. //@param: menus []model.SysBaseMenu, authorityId string
  114. //@return: err error
  115. func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) {
  116. var auth model.SysAuthority
  117. auth.AuthorityId = authorityId
  118. auth.SysBaseMenus = menus
  119. err = SetMenuAuthority(&auth)
  120. return err
  121. }
  122. //@author: [piexlmax](https://github.com/piexlmax)
  123. //@function: GetMenuAuthority
  124. //@description: 查看当前角色树
  125. //@param: info *request.GetAuthorityId
  126. //@return: err error, menus []model.SysMenu
  127. func GetMenuAuthority(info *request.GetAuthorityId) (err error, menus []model.SysMenu) {
  128. err = global.GVA_DB.Where("authority_id = ? ", info.AuthorityId).Order("sort").Find(&menus).Error
  129. //sql := "SELECT authority_menu.keep_alive,authority_menu.default_menu,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 = ? ORDER BY authority_menu.sort ASC"
  130. //err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
  131. return err, menus
  132. }