From ad1318b8f988e143a5e383f5b12580c1dc5e86e0 Mon Sep 17 00:00:00 2001 From: Ryan Su Date: Fri, 30 Sep 2022 12:25:14 +0800 Subject: [PATCH] fix: authority tree generation and init database code --- rpc/internal/logic/getmenuauthoritylogic.go | 4 +- rpc/internal/logic/getmenulistbyrolelogic.go | 80 ++++++++++---------- rpc/internal/logic/initdatabaselogic.go | 62 ++++++++++----- 3 files changed, 86 insertions(+), 60 deletions(-) diff --git a/rpc/internal/logic/getmenuauthoritylogic.go b/rpc/internal/logic/getmenuauthoritylogic.go index 5e566d8f..4636ec48 100644 --- a/rpc/internal/logic/getmenuauthoritylogic.go +++ b/rpc/internal/logic/getmenuauthoritylogic.go @@ -40,7 +40,9 @@ func (l *GetMenuAuthorityLogic) GetMenuAuthority(in *core.IDReq) (*core.RoleMenu } var menuIds []uint64 for _, v := range r.Menus { - menuIds = append(menuIds, uint64(v.ID)) + if v.ID != 1 { + menuIds = append(menuIds, uint64(v.ID)) + } } return &core.RoleMenuAuthorityResp{MenuId: menuIds}, nil } diff --git a/rpc/internal/logic/getmenulistbyrolelogic.go b/rpc/internal/logic/getmenulistbyrolelogic.go index c8aeb70f..fa55c94f 100644 --- a/rpc/internal/logic/getmenulistbyrolelogic.go +++ b/rpc/internal/logic/getmenulistbyrolelogic.go @@ -48,55 +48,57 @@ func (l *GetMenuListByRoleLogic) GetMenuListByRole(in *core.IDReq) (*core.MenuIn for _, v := range r.Menus { validId[v.ID] = struct{}{} } - res.Data = findRoleMenuChildren(r.Menus, validId) + res.Data = findRoleMenuChildren(r.Menus, validId, 1) // delete menus whose menu levels are not 1 - var tmp []*core.MenuInfo - for _, v := range res.Data { - if v.Level == 1 { - tmp = append(tmp, v) - } - } - res.Data = tmp + //var tmp []*core.MenuInfo + //for _, v := range res.Data { + // if v.Level == 1 { + // tmp = append(tmp, v) + // } + //} + //res.Data = tmp return res, nil } -func findRoleMenuChildren(data []model.Menu, validId map[uint]struct{}) []*core.MenuInfo { +func findRoleMenuChildren(data []model.Menu, validId map[uint]struct{}, parentId uint) []*core.MenuInfo { if data == nil { return nil } var result []*core.MenuInfo for _, v := range data { - if _, ok := validId[v.ID]; ok { - tmp := &core.MenuInfo{ - Id: uint64(v.ID), - CreateAt: v.CreatedAt.Unix(), - UpdateAt: v.UpdatedAt.Unix(), - MenuType: v.MenuType, - Level: v.MenuLevel, - ParentId: uint32(v.ParentId), - Path: v.Path, - Name: v.Name, - Redirect: v.Redirect, - Component: v.Component, - OrderNo: v.OrderNo, - Meta: &core.Meta{ - Title: v.Meta.Title, - Icon: v.Meta.Icon, - HideMenu: v.Meta.HideMenu, - HideBreadcrumb: v.Meta.HideBreadcrumb, - CurrentActiveMenu: v.Meta.CurrentActiveMenu, - IgnoreKeepAlive: v.Meta.IgnoreKeepAlive, - HideTab: v.Meta.HideTab, - FrameSrc: v.Meta.FrameSrc, - CarryParam: v.Meta.CarryParam, - HideChildrenInMenu: v.Meta.HideChildrenInMenu, - Affix: v.Meta.Affix, - DynamicLevel: v.Meta.DynamicLevel, - RealPath: v.Meta.RealPath, - }, - Children: findRoleMenuChildren(v.Children, validId), + if v.ParentId == parentId && v.ID != v.ParentId { + if _, ok := validId[v.ID]; ok { + tmp := &core.MenuInfo{ + Id: uint64(v.ID), + CreateAt: v.CreatedAt.UnixMilli(), + UpdateAt: v.UpdatedAt.UnixMilli(), + MenuType: v.MenuType, + Level: v.MenuLevel, + ParentId: uint32(v.ParentId), + Path: v.Path, + Name: v.Name, + Redirect: v.Redirect, + Component: v.Component, + OrderNo: v.OrderNo, + Meta: &core.Meta{ + Title: v.Meta.Title, + Icon: v.Meta.Icon, + HideMenu: v.Meta.HideMenu, + HideBreadcrumb: v.Meta.HideBreadcrumb, + CurrentActiveMenu: v.Meta.CurrentActiveMenu, + IgnoreKeepAlive: v.Meta.IgnoreKeepAlive, + HideTab: v.Meta.HideTab, + FrameSrc: v.Meta.FrameSrc, + CarryParam: v.Meta.CarryParam, + HideChildrenInMenu: v.Meta.HideChildrenInMenu, + Affix: v.Meta.Affix, + DynamicLevel: v.Meta.DynamicLevel, + RealPath: v.Meta.RealPath, + }, + Children: findRoleMenuChildren(data, validId, v.ID), + } + result = append(result, tmp) } - result = append(result, tmp) } } return result diff --git a/rpc/internal/logic/initdatabaselogic.go b/rpc/internal/logic/initdatabaselogic.go index f72e163b..7e5b693c 100644 --- a/rpc/internal/logic/initdatabaselogic.go +++ b/rpc/internal/logic/initdatabaselogic.go @@ -584,7 +584,7 @@ func (l *InitDatabaseLogic) insertMenuData() error { Meta: model.Meta{ Title: "routes.system.fileManagementTitle", Icon: "ant-design:folder-open-outlined", - HideMenu: false, + HideMenu: true, HideBreadcrumb: true, IgnoreKeepAlive: false, HideTab: false, @@ -595,18 +595,18 @@ func (l *InitDatabaseLogic) insertMenuData() error { }, }, { - MenuLevel: 1, + MenuLevel: 2, MenuType: 1, - ParentId: 7, - Path: "/profile", - Name: "Profile", - Component: "/sys/profile/index", - OrderNo: 3, + ParentId: 3, + Path: "/dictionary", + Name: "Dictionary Management", + Component: "/sys/dictionary/index", + OrderNo: 5, Disabled: false, Meta: model.Meta{ - Title: "routes.system.userProfileTitle", - Icon: "ant-design:profile-outlined", - HideMenu: true, + Title: "routes.system.dictionaryManagementTitle", + Icon: "ant-design:book-outlined", + HideMenu: false, HideBreadcrumb: true, IgnoreKeepAlive: false, HideTab: false, @@ -617,18 +617,18 @@ func (l *InitDatabaseLogic) insertMenuData() error { }, }, { - MenuLevel: 2, - MenuType: 1, - ParentId: 3, - Path: "/dictionary", - Name: "Dictionary Management", - Component: "/sys/dictionary/index", - OrderNo: 5, + MenuLevel: 1, + MenuType: 0, + ParentId: 1, + Path: "", + Name: "Other Pages", + Component: "LAYOUT", + OrderNo: 4, Disabled: false, Meta: model.Meta{ - Title: "routes.system.dictionaryManagementTitle", - Icon: "ant-design:book-outlined", - HideMenu: false, + Title: "routes.system.otherPages", + Icon: "ant-design:question-circle-outlined", + HideMenu: true, HideBreadcrumb: true, IgnoreKeepAlive: false, HideTab: false, @@ -660,6 +660,28 @@ func (l *InitDatabaseLogic) insertMenuData() error { DynamicLevel: 20, }, }, + { + MenuLevel: 1, + MenuType: 1, + ParentId: 10, + Path: "/profile", + Name: "Profile", + Component: "/sys/profile/index", + OrderNo: 3, + Disabled: false, + Meta: model.Meta{ + Title: "routes.system.userProfileTitle", + Icon: "ant-design:profile-outlined", + HideMenu: true, + HideBreadcrumb: true, + IgnoreKeepAlive: false, + HideTab: false, + CarryParam: false, + HideChildrenInMenu: false, + Affix: false, + DynamicLevel: 20, + }, + }, } result := l.svcCtx.DB.CreateInBatches(menus, 100) if result.Error != nil {