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.

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