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.

154 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: err error
  79. func AddBaseMenu(menu model.SysBaseMenu) (err error) {
  80. if !errors.Is(global.GVA_DB.Where("name = ?", menu.Name).First(&model.SysBaseMenu{}).Error, gorm.ErrRecordNotFound) {
  81. err = errors.New("存在重复name,请修改name")
  82. }
  83. err = global.GVA_DB.Create(&menu).Error
  84. return err
  85. }
  86. //@author: [piexlmax](https://github.com/piexlmax)
  87. //@function: getBaseMenuTreeMap
  88. //@description: 获取路由总树map
  89. //@return: err error, treeMap map[string][]model.SysBaseMenu
  90. func getBaseMenuTreeMap() (err error, treeMap map[string][]model.SysBaseMenu) {
  91. var allMenus []model.SysBaseMenu
  92. treeMap = make(map[string][]model.SysBaseMenu)
  93. err = global.GVA_DB.Order("sort").Preload("Parameters").Find(&allMenus).Error
  94. for _, v := range allMenus {
  95. treeMap[v.ParentId] = append(treeMap[v.ParentId], v)
  96. }
  97. return err, treeMap
  98. }
  99. //@author: [piexlmax](https://github.com/piexlmax)
  100. //@function: GetBaseMenuTree
  101. //@description: 获取基础路由树
  102. //@return: err error, menus []model.SysBaseMenu
  103. func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) {
  104. err, treeMap := getBaseMenuTreeMap()
  105. menus = treeMap["0"]
  106. for i := 0; i < len(menus); i++ {
  107. err = getBaseChildrenList(&menus[i], treeMap)
  108. }
  109. return err, menus
  110. }
  111. //@author: [piexlmax](https://github.com/piexlmax)
  112. //@function: AddMenuAuthority
  113. //@description: 为角色增加menu树
  114. //@param: menus []model.SysBaseMenu, authorityId string
  115. //@return: err error
  116. func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) {
  117. var auth model.SysAuthority
  118. auth.AuthorityId = authorityId
  119. auth.SysBaseMenus = menus
  120. err = SetMenuAuthority(&auth)
  121. return err
  122. }
  123. //@author: [piexlmax](https://github.com/piexlmax)
  124. //@function: GetMenuAuthority
  125. //@description: 查看当前角色树
  126. //@param: info *request.GetAuthorityId
  127. //@return: err error, menus []model.SysMenu
  128. func GetMenuAuthority(info *request.GetAuthorityId) (err error, menus []model.SysMenu) {
  129. err = global.GVA_DB.Where("authority_id = ? ", info.AuthorityId).Order("sort").Find(&menus).Error
  130. //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"
  131. //err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
  132. return err, menus
  133. }