sys_menu.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. package system
  2. import (
  3. "errors"
  4. "hongze/fms_api/models/base"
  5. "hongze/fms_api/models/system"
  6. "hongze/fms_api/utils"
  7. )
  8. //MenuList 只返回当前登录管理员有权限的菜单列表,不返回按钮
  9. func MenuList(adminInfo *system.SysAdmin) (list []*system.MenuListItemResp, err error, errMsg string) {
  10. //查询有权限的菜单
  11. roleMenu := new(system.SysRoleMenu)
  12. cond := " rm.role_id = ?"
  13. pars := make([]interface{}, 0)
  14. pars = append(pars, adminInfo.RoleId)
  15. roleHasMenuList, err := roleMenu.GetMenuListByCondition(cond, pars)
  16. if err != nil {
  17. errMsg = "查询角色绑定的菜单出错"
  18. err = errors.New(errMsg + "Err:" + err.Error())
  19. return
  20. }
  21. var menuIds []int64
  22. for _, v := range roleHasMenuList {
  23. menuIds = append(menuIds, v.MenuId)
  24. }
  25. //查询其他级别的菜单列表
  26. menu := new(system.SysMenu)
  27. var condition string
  28. par := make([]interface{}, 0)
  29. condition = " menu_type = 0 and menu_id in ?"
  30. par = append(par, menuIds)
  31. orderStr := "sort asc, create_time, menu_id desc"
  32. //遍历排序好的菜单列表,,返回每个菜单结点,不包含按钮类型
  33. childList, err := menu.GetMenuListByCondition(condition, par, orderStr)
  34. if err != nil {
  35. return
  36. }
  37. list = make([]*system.MenuListItemResp, 0)
  38. for _, v := range childList {
  39. tmp := &system.MenuListItemResp{
  40. MenuId: v.MenuId,
  41. ParentId: v.ParentId,
  42. Name: v.Name,
  43. Sort: v.Sort,
  44. Path: v.Path,
  45. PathName: v.PathName,
  46. IconPath: v.IconPath,
  47. Component: v.Component,
  48. Hidden: v.Hidden,
  49. HiddenLayout: v.HiddenLayout,
  50. Level: v.Level,
  51. MenuType: v.MenuType,
  52. ButtonCode: v.ButtonCode,
  53. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  54. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  55. HasBind: true,
  56. Children: nil,
  57. }
  58. list = append(list, tmp)
  59. }
  60. //组装返回值
  61. list = getMenuTreeRecursive(list, 0)
  62. return
  63. }
  64. func AllMenuList(page base.IPage, req *system.MenuListReq) (count int64, list []*system.MenuListItemResp, err error, errMsg string) {
  65. //查询一级菜单的数量和列表
  66. //查询其他级别的菜单列表
  67. menu := new(system.SysMenu)
  68. var condition string
  69. pars := make([]interface{}, 0)
  70. var finalRootIds []int64
  71. if req.Name != "" {
  72. var rootIds []int64
  73. name := "%" + req.Name + "%"
  74. relationList, tErr := menu.GetMenuByName(name)
  75. if tErr != nil {
  76. errMsg = "查询菜单出错"
  77. err = errors.New(errMsg + "Err:" + tErr.Error())
  78. return
  79. }
  80. if len(relationList) == 0 { //查不到相关记录
  81. return
  82. }
  83. for _, v := range relationList {
  84. if v.RootId == 0 {
  85. rootIds = append(rootIds, v.MenuId)
  86. } else {
  87. rootIds = append(rootIds, v.RootId)
  88. }
  89. }
  90. //分页处理 查询最顶层结点,得到最终的顶层结点
  91. condition = "parent_id=0 and menu_id in ?"
  92. pars = append(pars, rootIds)
  93. } else {
  94. condition = " parent_id = 0"
  95. }
  96. count, rootList, err := menu.SelectPage(page, condition, pars)
  97. if err != nil {
  98. errMsg = "查询菜单列表出错"
  99. err = errors.New(errMsg + "Err:" + err.Error())
  100. return
  101. }
  102. if len(rootList) == 0 { //查不到相关记录
  103. return
  104. }
  105. for _, v := range rootList {
  106. finalRootIds = append(finalRootIds, v.MenuId)
  107. }
  108. fCondition := " 1=1"
  109. fPars := make([]interface{}, 0)
  110. if req.HideLevel3 == 1 {
  111. fCondition += " and level <=1 "
  112. }
  113. if req.HideButton == 1 {
  114. fCondition += " and menu_type = 0"
  115. }
  116. //查询所有该顶层节点下的子节点
  117. fCondition += " and (root_id in ? or menu_id in ?)"
  118. fPars = append(fPars, finalRootIds, finalRootIds)
  119. orderStr := "sort asc, create_time, menu_id desc"
  120. childList, err := menu.GetMenuListByCondition(fCondition, fPars, orderStr)
  121. if err != nil {
  122. errMsg = "查询菜单列表出错"
  123. err = errors.New(errMsg + "Err:" + err.Error())
  124. return
  125. }
  126. list = make([]*system.MenuListItemResp, 0)
  127. for _, v := range childList {
  128. tmp := &system.MenuListItemResp{
  129. MenuId: v.MenuId,
  130. ParentId: v.ParentId,
  131. Name: v.Name,
  132. Sort: v.Sort,
  133. Path: v.Path,
  134. PathName: v.PathName,
  135. IconPath: v.IconPath,
  136. Component: v.Component,
  137. Hidden: v.Hidden,
  138. HiddenLayout: v.HiddenLayout,
  139. Level: v.Level,
  140. MenuType: v.MenuType,
  141. ButtonCode: v.ButtonCode,
  142. CreateTime: utils.TimeTransferString(utils.FormatDateTime, v.CreateTime),
  143. ModifyTime: utils.TimeTransferString(utils.FormatDateTime, v.ModifyTime),
  144. Children: nil,
  145. }
  146. list = append(list, tmp)
  147. }
  148. //组装返回值
  149. list = getMenuTreeRecursive(list, 0)
  150. return
  151. }
  152. func getMenuTreeRecursive(list []*system.MenuListItemResp, parentId int64) []*system.MenuListItemResp {
  153. res := make([]*system.MenuListItemResp, 0)
  154. for _, v := range list {
  155. if v.ParentId == parentId {
  156. v.Children = getMenuTreeRecursive(list, v.MenuId)
  157. res = append(res, v)
  158. }
  159. }
  160. return res
  161. }
  162. //ButtonList 只返回当前登录管理员有权限的按钮列表,不返回按钮
  163. func ButtonList(adminInfo *system.SysAdmin, req *system.MenuShortListItem) (list []*system.SysMenu, err error, errMsg string) {
  164. //查询有权限的菜单
  165. roleMenu := new(system.SysRoleMenu)
  166. cond := " rm.role_id = ?"
  167. pars := make([]interface{}, 0)
  168. pars = append(pars, adminInfo.RoleId)
  169. roleHasMenuList, err := roleMenu.GetMenuListByCondition(cond, pars)
  170. if err != nil {
  171. errMsg = "查询角色绑定的菜单出错"
  172. err = errors.New(errMsg + "Err:" + err.Error())
  173. return
  174. }
  175. var menuIds []int64
  176. for _, v := range roleHasMenuList {
  177. menuIds = append(menuIds, v.MenuId)
  178. }
  179. //查询其他级别的菜单列表
  180. menu := new(system.SysMenu)
  181. var condition string
  182. par := make([]interface{}, 0)
  183. condition = " menu_type = 1 and menu_id in ?"
  184. par = append(par, menuIds)
  185. if req.MenuId > 0 {
  186. condition += " and parent_id = ?"
  187. par = append(par, req.MenuId)
  188. }
  189. //遍历排序好的菜单列表,,返回每个菜单结点,不包含按钮类型
  190. list, err = menu.GetMenuListByCondition(condition, par, "")
  191. if err != nil {
  192. errMsg = "查询菜单按钮列表出错"
  193. err = errors.New(errMsg + "Err:" + err.Error())
  194. return
  195. }
  196. return
  197. }
  198. // DeleteMenu 删除菜单,连同删除子菜单
  199. func DeleteMenu(req *system.MenuShortListItem) (err error, errMsg string) {
  200. menu := new(system.SysMenu)
  201. //找出当前层级,如果当前是最顶层,则批量删除和顶层相关的所有部门
  202. menuInfo, err := menu.GetMenuByMenuId(req.MenuId)
  203. if err != nil {
  204. if err == utils.ErrNoRow {
  205. errMsg = "菜单不存在或者已删除"
  206. err = errors.New(errMsg + "Err:" + err.Error())
  207. return
  208. }
  209. errMsg = "查询菜单出错"
  210. err = errors.New(errMsg + "Err:" + err.Error())
  211. return
  212. }
  213. if menuInfo.ParentId > 0 {
  214. //如果不是最顶层,则构建树形图,得到需要删除的menu_id
  215. //查询所有该顶层节点下的子节点
  216. fCondition := "root_id = ? or menu_id = ?"
  217. fPars := make([]interface{}, 0)
  218. fPars = append(fPars, menuInfo.RootId, menuInfo.RootId)
  219. orderStr := "level desc"
  220. childList, tErr := menu.GetMenuListByCondition(fCondition, fPars, orderStr)
  221. if tErr != nil {
  222. errMsg = "查询菜单列表出错"
  223. err = errors.New(errMsg + "Err:" + tErr.Error())
  224. return
  225. }
  226. menuIdMap := make(map[int64][]int64)
  227. for _, v := range childList {
  228. menuIdMap[v.MenuId] = append(menuIdMap[v.MenuId], v.MenuId)
  229. if v.ParentId > 0 {
  230. menuIdMap[v.ParentId] = append(menuIdMap[v.ParentId], menuIdMap[v.MenuId]...)
  231. }
  232. }
  233. cond := "menu_id in ?"
  234. pars := make([]interface{}, 0)
  235. pars = append(pars, menuIdMap[menuInfo.MenuId])
  236. err = menu.DeleteByCondition(cond, pars)
  237. } else {
  238. //如果是最顶层,则删除所有相关的menu_id
  239. cond := "menu_id = ? or root_id = ?"
  240. pars := make([]interface{}, 0)
  241. pars = append(pars, menuInfo.MenuId, menuInfo.MenuId)
  242. err = menu.DeleteByCondition(cond, pars)
  243. }
  244. return
  245. }