sys_menu.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package controllers
  2. import (
  3. "eta_gn/eta_api/models"
  4. "eta_gn/eta_api/models/system"
  5. "strconv"
  6. )
  7. type SysMenuController struct {
  8. BaseAuthController
  9. }
  10. // @router /menu/list [get]
  11. func (this *SysRoleController) SysMenuList() {
  12. br := new(models.BaseResponse).Init()
  13. defer func() {
  14. this.Data["json"] = br
  15. this.ServeJSON()
  16. }()
  17. sysUser := this.SysUser
  18. if sysUser == nil {
  19. br.Msg = "请登录"
  20. br.ErrMsg = "请登录,SysUser Is Empty"
  21. return
  22. }
  23. roleId := sysUser.RoleId
  24. if roleId <= 0 {
  25. br.Msg = "参数错误"
  26. return
  27. }
  28. roleIds := strconv.Itoa(roleId)
  29. otherRoles, err := system.GetRoleIdsByAdminId(sysUser.AdminId)
  30. if err != nil {
  31. br.Msg = "获取其他角色失败"
  32. br.ErrMsg = "获取其他角色失败,Err:" + err.Error()
  33. return
  34. }
  35. if len(otherRoles) > 0 {
  36. for _, v := range otherRoles {
  37. roleIds += "," + strconv.Itoa(v.RoleId)
  38. }
  39. }
  40. list, err := system.GetMenuListAllByRoleIds(roleIds)
  41. if err != nil {
  42. br.Msg = "获取失败"
  43. br.ErrMsg = "获取失败,Err:" + err.Error()
  44. return
  45. }
  46. rootList, err := system.GetMenuListByRoleIds(roleIds)
  47. if err != nil {
  48. br.Msg = "获取失败"
  49. br.ErrMsg = "获取失败,Err:" + err.Error()
  50. return
  51. }
  52. menuMap := make(map[int][]*system.MenuList)
  53. for _, v := range list {
  54. if menus, ok := menuMap[v.ParentId]; !ok {
  55. menus = make([]*system.MenuList, 0)
  56. menus = append(menus, v)
  57. menuMap[v.ParentId] = menus
  58. } else {
  59. menus = append(menus, v)
  60. menuMap[v.ParentId] = menus
  61. }
  62. }
  63. for _, firstMenus := range rootList {
  64. if secondMenus, ok := menuMap[firstMenus.MenuId]; ok {
  65. for _, sencodMenu := range secondMenus {
  66. if thirdMenus, ok := menuMap[sencodMenu.MenuId]; ok {
  67. sencodMenu.Children = thirdMenus
  68. } else {
  69. thirdMenuList := make([]*system.MenuList, 0)
  70. sencodMenu.Children = thirdMenuList
  71. }
  72. }
  73. firstMenus.Children = secondMenus
  74. }
  75. }
  76. resp := new(system.MenuListResp)
  77. resp.List = rootList
  78. br.Ret = 200
  79. br.Success = true
  80. br.Msg = "获取成功"
  81. br.Data = resp
  82. }