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.

159 lines
5.8 KiB

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