From b53bad0bfb7e4b03e263fb319f15372b6ebf3fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Tue, 3 Sep 2024 16:55:18 +0800 Subject: [PATCH] =?UTF-8?q?[feat]=E5=AD=98=E5=9C=A8=E4=B8=89=E7=BA=A7?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=EF=BC=88=E5=8C=85=E6=8B=AC=E4=B8=89=E7=BA=A7?= =?UTF-8?q?=E4=BB=A5=E4=B8=8A=EF=BC=89=E6=97=B6=EF=BC=8C=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=8A=8A=E9=A1=B6=E7=BA=A7=E8=8F=9C=E5=8D=95=E4=BD=9C=E4=B8=BA?= =?UTF-8?q?=E5=AD=90=E6=A8=A1=E5=9D=97=EF=BC=8C=E5=9C=A8=E5=A4=B4=E9=83=A8?= =?UTF-8?q?=E5=AF=BC=E8=88=AA=E6=9D=A1=E5=91=88=E7=8E=B0=E5=AD=90=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E5=A4=A7=E8=8F=9C=E5=8D=95=EF=BC=8C=E5=B7=A6=E8=BE=B9?= =?UTF-8?q?=E8=8F=9C=E5=8D=95=E6=A0=91=E4=BB=85=E6=98=BE=E7=A4=BA=E8=AF=A5?= =?UTF-8?q?=E5=AD=90=E6=A8=A1=E5=9D=97=E4=B8=8B=E7=BA=A7=E8=8F=9C=E5=8D=95?= =?UTF-8?q?=EF=BC=8C=E4=BB=A5=E8=A7=A3=E5=86=B3=E8=8F=9C=E5=8D=95=E6=95=B0?= =?UTF-8?q?=E9=87=8F=E8=BF=87=E5=A4=9A=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Cube/Common/ViewHelper.cs | 27 +++++++++++++++ .../Areas/Admin/Index/IndexController.cs | 33 +++++++++++++++++-- NewLife.CubeNC/Views/ACE/_Navbar.cshtml | 21 ++++++++++-- NewLife.CubeNC/Views/layui/_Navbar.cshtml | 17 ++++++++++ 4 files changed, 93 insertions(+), 5 deletions(-) diff --git a/NewLife.Cube/Common/ViewHelper.cs b/NewLife.Cube/Common/ViewHelper.cs index 92a9e012..363e64cc 100644 --- a/NewLife.Cube/Common/ViewHelper.cs +++ b/NewLife.Cube/Common/ViewHelper.cs @@ -889,6 +889,33 @@ public static IList GetMenus(this IUser user) return menuTree; } + /// 获取用户所拥有的模块。三级菜单 + /// + /// + public static IDictionary GetModules(this IUser user) + { + var ms = new Dictionary(); + if (user == null) return ms; + + var menus = Menu.Root.Childs; + menus = menus.Where(e => e.Visible).ToList(); + //if (user != null) menus = menus.Where(e => user.Has(e)).ToList(); + + foreach (var item in menus) + { + // 授权判断 + if (!user.Has(item)) continue; + + // 三级菜单作为模块,否则统一归类到base + if (item.Childs.Any(e => e.Childs.Count > 0)) + ms[item.Name] = item.DisplayName ?? item.Name; + else + ms["base"] = "基础"; + } + + return ms; + } + /// 获取附件Url /// /// diff --git a/NewLife.CubeNC/Areas/Admin/Index/IndexController.cs b/NewLife.CubeNC/Areas/Admin/Index/IndexController.cs index 1833af82..6028e627 100644 --- a/NewLife.CubeNC/Areas/Admin/Index/IndexController.cs +++ b/NewLife.CubeNC/Areas/Admin/Index/IndexController.cs @@ -71,7 +71,10 @@ public ActionResult Index() if (startPage.IsNullOrEmpty()) startPage = CubeSetting.Current.StartPage; ViewBag.Main = startPage; - ViewBag.Menus = GetMenu(); + + var module = Request.GetRequestValue("module"); + //var modules = (user as User)?.GetModules(); + ViewBag.Menus = GetMenu(module); var uAgent = Request.Headers["User-Agent"] + ""; var isMobile = uAgent.Contains("Android") || uAgent.Contains("iPhone") || uAgent.Contains("iPad"); @@ -216,9 +219,9 @@ public ActionResult MemoryFree() /// /// [EntityAuthorize] - public ActionResult GetMenuTree() => Ok(data: GetMenu()); + public ActionResult GetMenuTree(String module) => Ok(data: GetMenu(module)); - private IList GetMenu() + private IList GetMenu(String module) { var user = _provider.Current as IUser; @@ -229,6 +232,30 @@ private IList GetMenu() menus = fact.GetMySubMenus(fact.Root.ID, user, true); } + // 根据模块过滤菜单 + if (module.EqualIgnoreCase("base")) + { + // 直接取base下级,以及所有仅有二级的菜单 + var ms = menus.FirstOrDefault(e => e.Name.EqualIgnoreCase("base"))?.Childs ?? []; + foreach (var item in menus) + { + if (!item.Name.EqualIgnoreCase("base") && item.Childs.All(e => e.Childs.Count == 0)) + { + ms.Add(item); + } + } + menus = ms; + } + else if (!module.IsNullOrEmpty()) + { + menus = menus.FirstOrDefault(e => e.Name.EqualIgnoreCase(module))?.Childs ?? []; + } + else + { + // 去掉三级菜单 + menus = menus.Where(e => e.Childs.All(x => x.Childs.Count == 0)).ToList(); + } + // 如果顶级只有一层,并且至少有三级目录,则提升一级 if (menus.Count == 1 && menus[0].Childs.All(m => m.Childs.Count > 0)) { menus = menus[0].Childs; } diff --git a/NewLife.CubeNC/Views/ACE/_Navbar.cshtml b/NewLife.CubeNC/Views/ACE/_Navbar.cshtml index b35bb2f5..ed5619ed 100644 --- a/NewLife.CubeNC/Views/ACE/_Navbar.cshtml +++ b/NewLife.CubeNC/Views/ACE/_Navbar.cshtml @@ -12,6 +12,10 @@ var cfg = ViewBag.Config as SysConfig; var name = cfg == null ? "" : cfg.DisplayName; + // 模块菜单 + var module = Context.Request.Query["module"].ToString(); + var ms = user.GetModules(); + var set = CubeSetting.Current; } @@ -30,14 +34,27 @@ -
+ +