|
@@ -46,8 +46,33 @@ func (s *menuService) GetAll() []dao.Menu {
|
|
|
return menus
|
|
|
}
|
|
|
|
|
|
-func (s *menuService) Routes(roleId int64) (*model.RsqMenuRoutes, error) {
|
|
|
- allMenus := MenuService.GetAll()
|
|
|
+func (s *menuService) Tree() ([]model.MenuRouteDetail, error) {
|
|
|
+ allMenus := s.GetAll()
|
|
|
+
|
|
|
+ var rsp []model.MenuRouteDetail
|
|
|
+ //上级角色——多个下级角色数组
|
|
|
+ tmp := make(map[int64][]dao.Menu)
|
|
|
+ for _, menu := range allMenus {
|
|
|
+ childes, isExist := tmp[menu.ParentId]
|
|
|
+ if isExist {
|
|
|
+ childes = append(childes, menu)
|
|
|
+ tmp[menu.ParentId] = childes
|
|
|
+ } else {
|
|
|
+ tmp[menu.ParentId] = []dao.Menu{menu}
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ if len(tmp) == 0 {
|
|
|
+ return nil, nil
|
|
|
+ }
|
|
|
+ roots, _ := tmp[0]
|
|
|
+ rsp = menuDfs(rsp, roots, tmp)
|
|
|
+
|
|
|
+ return rsp, nil
|
|
|
+}
|
|
|
+
|
|
|
+func (s *menuService) Routes(roleId int64) ([]model.MenuRouteDetail, error) {
|
|
|
+ allMenus := s.GetAll()
|
|
|
menuMap := make(map[int64]dao.Menu)
|
|
|
for _, menu := range allMenus {
|
|
|
menuMap[menu.ID] = menu
|
|
@@ -55,7 +80,7 @@ func (s *menuService) Routes(roleId int64) (*model.RsqMenuRoutes, error) {
|
|
|
|
|
|
roleMenus := RoleMenuService.GetMenuByRole(roleId)
|
|
|
|
|
|
- rsp := &model.RsqMenuRoutes{}
|
|
|
+ var rsp []model.MenuRouteDetail
|
|
|
//上级角色——多个下级角色数组
|
|
|
tmp := make(map[int64][]dao.Menu)
|
|
|
for _, roleMenu := range roleMenus {
|
|
@@ -75,7 +100,7 @@ func (s *menuService) Routes(roleId int64) (*model.RsqMenuRoutes, error) {
|
|
|
return nil, nil
|
|
|
}
|
|
|
roots, _ := tmp[0]
|
|
|
- rsp.MenuRouteDetails = menuDfs(rsp.MenuRouteDetails, roots, tmp)
|
|
|
+ rsp = menuDfs(rsp, roots, tmp)
|
|
|
|
|
|
return rsp, nil
|
|
|
}
|
|
@@ -99,3 +124,33 @@ func menuDfs(result []model.MenuRouteDetail, menus []dao.Menu, tmp map[int64][]d
|
|
|
}
|
|
|
return result
|
|
|
}
|
|
|
+
|
|
|
+func (s *menuService) LazyList(parentId int64, name, code, alias string) ([]model.MenuLazyListDetail, error) {
|
|
|
+ menu := &dao.Menu{
|
|
|
+ ParentId: parentId,
|
|
|
+ Name: name,
|
|
|
+ Code: code,
|
|
|
+ Alias: alias,
|
|
|
+ }
|
|
|
+ menus, err := menu.GetByParentId()
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ var parentIds []int64
|
|
|
+ for _, m := range menus {
|
|
|
+ parentIds = append(parentIds, m.ID)
|
|
|
+ }
|
|
|
+ hasChildrenMap, err := menu.HasChildren(parentIds)
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+ var rsp []model.MenuLazyListDetail
|
|
|
+ for _, m := range menus {
|
|
|
+ hasChildren := hasChildrenMap[m.ID]
|
|
|
+ rsp = append(rsp, model.MenuLazyListDetail{
|
|
|
+ Menu: m,
|
|
|
+ HasChildren: hasChildren,
|
|
|
+ })
|
|
|
+ }
|
|
|
+ return rsp, nil
|
|
|
+}
|