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.

128 lines
4.8 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. )
  8. // @title GetMenuTree
  9. // @description 获取动态菜单树
  10. // @auth (2020/04/05 20:22 )
  11. // @param authorityId string
  12. // @return err error
  13. // @return menus []SysMenu
  14. func GetMenuTree(authorityId string) (err error, menus []model.SysMenu) {
  15. sql := "SELECT 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 = ? AND authority_menu.parent_id = ?"
  16. err = global.GVA_DB.Raw(sql, authorityId, 0).Scan(&menus).Error
  17. for i := 0; i < len(menus); i++ {
  18. err = getChildrenList(&menus[i], sql)
  19. }
  20. return err, menus
  21. }
  22. // @title getChildrenList
  23. // @description 获取子菜单
  24. // @auth (2020/04/05 20:22 )
  25. // @param menu *SysMenu
  26. // @param SQLstatement string
  27. // @return err error
  28. func getChildrenList(menu *model.SysMenu, sql string) (err error) {
  29. err = global.GVA_DB.Raw(sql, menu.AuthorityId, menu.MenuId).Scan(&menu.Children).Error
  30. for i := 0; i < len(menu.Children); i++ {
  31. err = getChildrenList(&menu.Children[i], sql)
  32. }
  33. return err
  34. }
  35. // @title GetInfoList
  36. // @description 获取路由分页
  37. // @auth (2020/04/05 20:22 )
  38. // @param newPassword string
  39. // @return err error
  40. func GetInfoList(info request.PageInfo) (err error, list interface{}, total int) {
  41. limit := info.PageSize
  42. offset := info.PageSize * (info.Page - 1)
  43. db := global.GVA_DB
  44. if err != nil {
  45. return
  46. } else {
  47. var menuList []model.SysBaseMenu
  48. err = db.Limit(limit).Offset(offset).Where("parent_id = 0").Order("sort", true).Find(&menuList).Error
  49. for i := 0; i < len(menuList); i++ {
  50. err = getBaseChildrenList(&menuList[i])
  51. }
  52. return err, menuList, total
  53. }
  54. }
  55. // @title getBaseChildrenList
  56. // @description get children of menu, 获取菜单的子菜单
  57. // @auth (2020/04/05 20:22 )
  58. // @param menu *SysBaseMenu
  59. // @return err error
  60. func getBaseChildrenList(menu *model.SysBaseMenu) (err error) {
  61. err = global.GVA_DB.Where("parent_id = ?", menu.ID).Order("sort", true).Find(&menu.Children).Error
  62. for i := 0; i < len(menu.Children); i++ {
  63. err = getBaseChildrenList(&menu.Children[i])
  64. }
  65. return err
  66. }
  67. // @title AddBaseMenu
  68. // @description 函数的详细描述
  69. // @auth (2020/04/05 20:22 )
  70. // @param newPassword string
  71. // @return err error
  72. //增加基础路由
  73. func AddBaseMenu(menu model.SysBaseMenu) (err error) {
  74. findOne := global.GVA_DB.Where("name = ?", menu.Name).Find(&model.SysBaseMenu{}).Error
  75. if findOne != nil {
  76. err = global.GVA_DB.Create(menu).Error
  77. } else {
  78. err = errors.New("存在重复name,请修改name")
  79. }
  80. return err
  81. }
  82. // @title GetBaseMenuTree
  83. // @description 获取基础路由树
  84. // @auth (2020/04/05 20:22 )
  85. // @return err error
  86. // @return menus []SysBaseMenu
  87. func GetBaseMenuTree() (err error, menus []model.SysBaseMenu) {
  88. err = global.GVA_DB.Where(" parent_id = ?", 0).Order("sort", true).Find(&menus).Error
  89. for i := 0; i < len(menus); i++ {
  90. err = getBaseChildrenList(&menus[i])
  91. }
  92. return err, menus
  93. }
  94. // @title AddMenuAuthority
  95. // @description 为角色增加menu树
  96. // @auth (2020/04/05 20:22 )
  97. // @param menus []SysBaseMenu
  98. // @param authorityId string
  99. // @return error
  100. func AddMenuAuthority(menus []model.SysBaseMenu, authorityId string) (err error) {
  101. var auth model.SysAuthority
  102. auth.AuthorityId = authorityId
  103. auth.SysBaseMenus = menus
  104. err = SetMenuAuthority(&auth)
  105. return err
  106. }
  107. // @title GetMenuAuthority
  108. // @description 查看当前角色树
  109. // @auth (2020/04/05 20:22 )
  110. // @param authorityId string
  111. // @return err error
  112. // @return menus []SysBaseMenu
  113. func GetMenuAuthority(authorityId string) (err error, menus []model.SysMenu) {
  114. sql := "SELECT 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 = ?"
  115. err = global.GVA_DB.Raw(sql, authorityId).Scan(&menus).Error
  116. return err, menus
  117. }