sys_menu.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package controllers
  2. import (
  3. "eta/eta_forum_admin/models"
  4. "eta/eta_forum_admin/models/system"
  5. "strconv"
  6. )
  7. type SysMenuController struct {
  8. BaseAuthController
  9. }
  10. // @Title 获取用户权限菜单
  11. // @Description 获取用户权限菜单接口
  12. // @Success 200 {object} system.MenuListResp
  13. // @router /menu/list [get]
  14. func (this *SysRoleController) SysMenuList() {
  15. br := new(models.BaseResponse).Init()
  16. defer func() {
  17. this.Data["json"] = br
  18. this.ServeJSON()
  19. }()
  20. sysUser := this.SysUser
  21. if sysUser == nil {
  22. br.Msg = "请登录"
  23. br.ErrMsg = "请登录,SysUser Is Empty"
  24. return
  25. }
  26. roleId := sysUser.RoleId
  27. //roleId=1
  28. if roleId <= 0 {
  29. br.Msg = "参数错误"
  30. return
  31. }
  32. roleIds := strconv.Itoa(roleId)
  33. //查询账号绑定的其他角色
  34. otherRoles, err := system.GetRoleIdsByAdminId(sysUser.AdminId)
  35. if err != nil {
  36. br.Msg = "获取其他角色失败"
  37. br.ErrMsg = "获取其他角色失败,Err:" + err.Error()
  38. return
  39. }
  40. if len(otherRoles) > 0 {
  41. for _, v := range otherRoles {
  42. roleIds += "," + strconv.Itoa(v.RoleId)
  43. }
  44. }
  45. list, err := system.GetMenuListByRoleIds(roleIds)
  46. if err != nil {
  47. br.Msg = "获取失败"
  48. br.ErrMsg = "获取失败,Err:" + err.Error()
  49. return
  50. }
  51. lenList := len(list)
  52. for i := 0; i < lenList; i++ {
  53. item := list[i]
  54. child, err := system.GetMenuByParentIdRoleIds(roleIds, item.MenuId)
  55. if err != nil {
  56. br.Msg = "获取失败"
  57. br.ErrMsg = "获取子菜单数据失败,Err:" + err.Error()
  58. return
  59. }
  60. list[i].Children = child
  61. }
  62. resp := new(system.MenuListResp)
  63. resp.List = list
  64. br.Ret = 200
  65. br.Success = true
  66. br.Msg = "获取成功"
  67. br.Data = resp
  68. }