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.

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