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.

163 lines
5.6 KiB

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