添加菜单视图模型及方法,菜单数据从控制器获取
This commit is contained in:
parent
66c3b72806
commit
6b537476b3
|
@ -45,3 +45,4 @@ bld/
|
|||
/NewLife.Cube4/Model.xml
|
||||
/NewLife.Cube4/Build.tt
|
||||
/NewLife.Cube4/Build.cs
|
||||
/.vscode
|
||||
|
|
|
@ -1,4 +1,16 @@
|
|||
using System;
|
||||
//using System;
|
||||
//using System.Collections.Generic;
|
||||
//using System.ComponentModel;
|
||||
//using System.Reflection;
|
||||
//using Microsoft.AspNetCore.Mvc;
|
||||
//using NewLife.Common;
|
||||
//using NewLife.CubeNC.Com;
|
||||
//using NewLife.Web;
|
||||
//using XCode;
|
||||
//using XCode.Membership;
|
||||
//using XCode.Statistics;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
|
@ -16,6 +28,8 @@ using Microsoft.AspNetCore.Mvc;
|
|||
using NewLife.CubeNC.Com;
|
||||
using NewLife.CubeNC.Extensions;
|
||||
using ManagerProviderHelper = NewLife.CubeNC.Membership.ManagerProviderHelper;
|
||||
using NewLife.Model;
|
||||
using NewLife.CubeNC.ViewModels;
|
||||
|
||||
namespace NewLife.Cube.Admin.Controllers
|
||||
{
|
||||
|
@ -47,8 +61,10 @@ namespace NewLife.Cube.Admin.Controllers
|
|||
[AllowAnonymous]
|
||||
public ActionResult Index()
|
||||
{
|
||||
var user = ManagerProviderHelper.TryLogin(ManageProvider,HttpContext.RequestServices);
|
||||
if (user == null) return RedirectToAction("Login", "User", new { r = Request.GetEncodedPathAndQuery()
|
||||
var user = ManagerProviderHelper.TryLogin(ManageProvider, HttpContext.RequestServices);
|
||||
if (user == null) return RedirectToAction("Login", "User", new
|
||||
{
|
||||
r = Request.GetEncodedPathAndQuery()
|
||||
//.Url.PathAndQuery
|
||||
});
|
||||
|
||||
|
@ -60,6 +76,7 @@ namespace NewLife.Cube.Admin.Controllers
|
|||
if (startPage.IsNullOrEmpty()) startPage = Setting.Current.StartPage;
|
||||
|
||||
ViewBag.Main = startPage;
|
||||
ViewBag.Menus = GetMenu();
|
||||
|
||||
return View();
|
||||
}
|
||||
|
@ -151,5 +168,51 @@ namespace NewLife.Cube.Admin.Controllers
|
|||
|
||||
return base.ScanActionMenu(menu);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 获取菜单树
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
[EntityAuthorize(PermissionFlags.Detail)]
|
||||
public List<MenuTree> GetMenu()
|
||||
{
|
||||
var user = ManageProvider.User;
|
||||
|
||||
var fact = ObjectContainer.Current.Resolve<IMenuFactory>();
|
||||
|
||||
var menus = fact.Root.Childs;
|
||||
if (user?.Role != null)
|
||||
{
|
||||
menus = fact.GetMySubMenus(fact.Root.ID, user);
|
||||
}
|
||||
|
||||
// 如果顶级只有一层,并且至少有三级目录,则提升一级
|
||||
if (menus.Count == 1 && menus[0].Childs.All(m => m.Childs.Count > 0)) { menus = menus[0].Childs; }
|
||||
|
||||
menus = menus.Where(m => m.Visible).ToList();
|
||||
|
||||
var menuTree = MenuTree.GetMenuTree(pMenuTree =>
|
||||
{
|
||||
return fact.GetMySubMenus(pMenuTree.ID, user).Where(m => m.Visible).ToList();
|
||||
}, list =>
|
||||
{
|
||||
|
||||
var menuList = (from menu in list
|
||||
// where m.Visible
|
||||
select new MenuTree
|
||||
{
|
||||
ID = menu.ID,
|
||||
DisplayName = menu.DisplayName,
|
||||
Url = Url.Content(menu.Url),
|
||||
Icon = menu.Icon,
|
||||
Class = ""
|
||||
}).ToList();
|
||||
return menuList.Count > 0 ? menuList : null;
|
||||
}, menus.ToList());
|
||||
|
||||
var childs = menuTree[0].Children;
|
||||
return menuTree;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
|
@ -1,25 +1,14 @@
|
|||
@using NewLife.Model;
|
||||
@using NewLife.CubeNC.ViewModels;
|
||||
@{
|
||||
var user = ViewBag.User as IUser ?? User.Identity as IUser;
|
||||
|
||||
var fact = ObjectContainer.Current.Resolve<IMenuFactory>();
|
||||
|
||||
var Menus = fact.Root.Childs;
|
||||
if (user != null && user.Role != null)
|
||||
{
|
||||
Menus = fact.GetMySubMenus(fact.Root.ID, user);
|
||||
}
|
||||
|
||||
// 如果顶级只有一层,并且至少有三级目录,则提升一级
|
||||
if (Menus.Count == 1 && Menus[0].Childs.All(m => m.Childs.Count > 0)) { Menus = Menus[0].Childs; }
|
||||
|
||||
String[] icos = new String[] { "fa-tachometer", "fa-desktop", "fa-list", "fa-pencil-square-o", "fa-list-alt", "fa-calendar", "fa-picture-o", "fa-tag", "fa-file-o" };
|
||||
Int32 _idx = 0;
|
||||
var Menus = ViewBag.Menus as List<MenuTree>;
|
||||
}
|
||||
<ul class="nav nav-list">
|
||||
@foreach (IMenu menu in Menus.Where(m => m.Visible))
|
||||
@foreach (var menu in Menus)
|
||||
{
|
||||
var childs = fact.GetMySubMenus(menu.ID, user).Where(m => m.Visible);
|
||||
var childs = menu.Children ?? new List<MenuTree>();
|
||||
if (_idx >= icos.Length) { _idx = 0; }
|
||||
<li @Html.Raw(menu == Menus[0] ? "class=\"active open\"" : "")>
|
||||
<a href="#" class="dropdown-toggle">
|
||||
|
@ -33,7 +22,7 @@
|
|||
<b class="arrow"></b>
|
||||
|
||||
<ul @Html.Raw(menu == Menus[0] ? "class=\"submenu nav-show\" style=\"display:block;\"" : "class=\"submenu nav-hide\" style=\"display:none;\"")>
|
||||
@foreach (IMenu menu2 in childs)
|
||||
@foreach (var menu2 in childs)
|
||||
{
|
||||
@Html.PartialAsync("_Left_Item", menu2).Result;
|
||||
}
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
@using NewLife.Model;
|
||||
@using XCode.Membership;
|
||||
@using NewLife.CubeNC.ViewModels;
|
||||
@{
|
||||
var user = ViewBag.User as IUser ?? User.Identity as IUser;
|
||||
|
||||
var fact = ObjectContainer.Current.Resolve<IMenuFactory>();
|
||||
|
||||
var item = Model as IMenu;
|
||||
var childs = fact.GetMySubMenus(item.ID, user).Where(m => m.Visible);
|
||||
var item = Model as MenuTree;
|
||||
var childs = item.Children ?? new List<MenuTree>();
|
||||
var url = item.Url.IsNullOrEmpty() ? "" : Url.Content(item.Url);
|
||||
}
|
||||
<li>
|
||||
|
|
|
@ -0,0 +1,63 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace NewLife.CubeNC.ViewModels
|
||||
{
|
||||
/// <summary>
|
||||
/// 菜单树
|
||||
/// </summary>
|
||||
public class MenuTree
|
||||
{
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public int ID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 名称
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string DisplayName { get; set; }
|
||||
/// <summary>
|
||||
/// 链接
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Url { get; set; }
|
||||
/// <summary>
|
||||
/// 图标
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Icon { get; set; }
|
||||
/// <summary>
|
||||
/// 自定义样式类
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public string Class { get; set; }
|
||||
/// <summary>
|
||||
/// 子菜单
|
||||
/// </summary>
|
||||
/// <value></value>
|
||||
public List<MenuTree> Children { get => GetChildren?.Invoke(this) ?? null; set { } }
|
||||
|
||||
/// <summary>
|
||||
/// 获取子菜单的方法
|
||||
/// </summary>
|
||||
private static Func<MenuTree, List<MenuTree>> GetChildren;
|
||||
|
||||
/// <summary>
|
||||
/// 获取菜单树
|
||||
/// </summary>
|
||||
/// <param name="getChildrenSrc">自定义的获取子菜单需要数据的方法</param>
|
||||
/// <param name="getMenuList">获取菜单列表的方法</param>
|
||||
/// <param name="src">获取菜单列表的初始数据来源</param>
|
||||
/// <returns></returns>
|
||||
public static List<MenuTree> GetMenuTree<T>(Func<MenuTree, T> getChildrenSrc,
|
||||
Func<T, List<MenuTree>> getMenuList, T src) where T : class, new()
|
||||
{
|
||||
GetChildren = m =>getMenuList?.Invoke(getChildrenSrc(m));
|
||||
|
||||
return getMenuList?.Invoke(src);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue