Skip to content

Commit

Permalink
[feat]存在三级菜单(包括三级以上)时,自动把顶级菜单作为子模块,在头部导航条呈现子模块大菜单,左边菜单树仅显示该子模块下级菜单,以解…
Browse files Browse the repository at this point in the history
…决菜单数量过多的问题。
  • Loading branch information
nnhy committed Sep 3, 2024
1 parent 1b6ddfe commit b53bad0
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 5 deletions.
27 changes: 27 additions & 0 deletions NewLife.Cube/Common/ViewHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,33 @@ public static IList<MenuTree> GetMenus(this IUser user)
return menuTree;
}

/// <summary>获取用户所拥有的模块。三级菜单</summary>
/// <param name="user"></param>
/// <returns></returns>
public static IDictionary<String, String> GetModules(this IUser user)
{
var ms = new Dictionary<String, String>();
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;
}

/// <summary>获取附件Url</summary>
/// <param name="attachment"></param>
/// <returns></returns>
Expand Down
33 changes: 30 additions & 3 deletions NewLife.CubeNC/Areas/Admin/Index/IndexController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -216,9 +219,9 @@ public ActionResult MemoryFree()
/// </summary>
/// <returns></returns>
[EntityAuthorize]
public ActionResult GetMenuTree() => Ok(data: GetMenu());
public ActionResult GetMenuTree(String module) => Ok(data: GetMenu(module));

private IList<MenuTree> GetMenu()
private IList<MenuTree> GetMenu(String module)
{
var user = _provider.Current as IUser;

Expand All @@ -229,6 +232,30 @@ private IList<MenuTree> 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; }

Expand Down
21 changes: 19 additions & 2 deletions NewLife.CubeNC/Views/ACE/_Navbar.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
<!-- #section:basics/navbar.layout -->
Expand All @@ -30,14 +34,27 @@
</button>

<!-- /section:basics/sidebar.mobile.toggle -->
<div class="pull-left">
<div class="navbar-header">
<!-- #section:basics/navbar.layout.brand -->
<a href="@ViewBag.Main" class="navbar-brand" target="main">
<a href="/Admin" class="navbar-brand">
<small>
@name
</small>
</a>
</div>
<ul class="nav nav-pills navbar-nav">
@foreach (var item in ms)
{
if (item.Key.EqualIgnoreCase(module))
{
<li class="active"><a class="nav-link" href="[email protected]">@item.Value</a></li>
}
else
{
<li><a class="nav-link" href="[email protected]">@item.Value</a></li>
}
}
</ul>

<!-- #section:basics/navbar.dropdown -->
<div class="navbar-buttons navbar-header pull-right" role="navigation">
Expand Down
17 changes: 17 additions & 0 deletions NewLife.CubeNC/Views/layui/_Navbar.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -25,6 +29,19 @@
<i class="layui-icon layui-icon-spread-left"></i>
</li>
</ul>
<ul class="layui-nav layui-layout-left">
@foreach (var item in ms)
{
if (item.Key.EqualIgnoreCase(module))
{
<li class="layui-nav-item active" lay-header-event="menuLeft"><a href="[email protected]">@item.Value</a></li>
}
else
{
<li class="layui-nav-item" lay-header-event="menuLeft"><a href="[email protected]">@item.Value</a></li>
}
}
</ul>

<ul class="layui-nav layui-layout-right">
<li class="layui-nav-item layui-show-md-inline-block">
Expand Down

0 comments on commit b53bad0

Please sign in to comment.