You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

441 lines
16 KiB

using System;
using System.Collections.Generic;
using System.Linq;
using Znyc.Cloudcar.Admin.Commons.Core.App;
using Znyc.Cloudcar.Admin.Commons.Json;
using Znyc.Cloudcar.Admin.Commons.Log;
using Znyc.Cloudcar.Admin.Commons.Mapping;
using Znyc.Cloudcar.Admin.Commons.Tree;
using Znyc.Cloudcar.Admin.Security.Dtos;
using Znyc.Cloudcar.Admin.Security.Entitys;
using Znyc.Cloudcar.Admin.Security.IServices;
namespace Znyc.Cloudcar.Admin.Security.Application
{
/// <summary>
/// </summary>
public class MenuApp
{
private readonly IMenuService service = App.GetService<IMenuService>();
private readonly IAdminUserService serviceUser = App.GetService<IAdminUserService>();
private readonly ISystemTypeService systemservice = App.GetService<ISystemTypeService>();
/// <summary>
/// 获取菜单树JsTree模式
/// </summary>
/// <returns></returns>
public List<JsTreeModel> MenuFuntionJsTree()
{
List<JsTreeModel> list = new List<JsTreeModel>();
List<MenuEntity> listMenu = service.GetAllByIsNotDeleteAndEnabledMark().OrderBy(t => t.SortCode).ToList();
foreach (MenuEntity item in listMenu)
{
JsTreeModel jsTreeModel = new JsTreeModel
{
id = item.Id,
text = item.FullName,
icon = item.Icon,
parent = item.ParentId
};
JsTreeStateModel jsTreeStateModel = new JsTreeStateModel
{
disabled = false,
selected = true,
opened = true
};
jsTreeModel.state = jsTreeStateModel;
list.Add(jsTreeModel);
}
return list.JsTreeJson();
}
/// <summary>
/// </summary>
/// <param name="ParentId"></param>
/// <returns></returns>
public List<MenuInputDto> GetMenu(long ParentId)
{
List<MenuInputDto> list = new List<MenuInputDto>();
return list;
}
/// <summary>
/// 获取菜单树TreeView模式
/// </summary>
/// <returns></returns>
public List<TreeViewModel> MenuFuntionTreeViewJson()
{
List<TreeViewModel> list = new List<TreeViewModel>();
List<MenuEntity> listMenu = service.GetAll().OrderBy(t => t.SortCode).ToList();
list = JsTreeJson(listMenu, 0, "").ToList<TreeViewModel>();
return list;
}
/// <summary>
/// </summary>
/// <param name="data"></param>
/// <param name="ParentId"></param>
/// <param name="blank"></param>
/// <returns></returns>
private static string JsTreeJson(List<MenuEntity> data, long ParentId, string blank)
{
List<TreeViewModel> list = new List<TreeViewModel>();
TreeViewModel jsTreeModel = new TreeViewModel();
List<MenuEntity> ChildNodeList = data.FindAll(t => t.ParentId == ParentId);
string tabline = "";
if (ChildNodeList.Count > 0)
{
tabline = tabline + blank;
}
foreach (MenuEntity entity in ChildNodeList)
{
jsTreeModel.text = entity.FullName;
jsTreeModel.icon = entity.Icon;
//list.Add(jsTreeModel);
jsTreeModel.nodes = JsTreeJson(data, entity.Id, tabline).ToList<TreeViewModel>();
list.Add(jsTreeModel);
}
return list.ToJson();
}
/// <summary>
/// 根据用户角色获取菜单树VuexMenusTree模式
/// </summary>
/// <param name="roleIds">角色ID</param>
/// <param name="systemCode">系统类型代码子系统代码</param>
/// <returns></returns>
public List<MenuOutputDto> GetMenuFuntionJson(long roleIds, string systemCode)
{
List<MenuOutputDto> list = new List<MenuOutputDto>();
try
{
SystemTypeEntity systemType = systemservice.GetByCode(systemCode);
list = GetMenusByRole(roleIds, systemType.Id.ToString()).OrderBy(t => t.SortCode)
.MapTo<MenuOutputDto>();
}
catch (Exception ex)
{
Log4NetHelper.Error("根据用户角色和子系统代码获取菜单异常", ex);
}
return list;
}
/// <summary>
/// 根据用户角色获取菜单树VuexMenusTree模式
/// </summary>
/// <param name="systemCode">系统类型代码子系统代码</param>
/// <returns></returns>
public List<MenuOutputDto> GetMenuFuntionJson(string systemCode)
{
List<MenuOutputDto> list = new List<MenuOutputDto>();
try
{
SystemTypeEntity systemType = systemservice.GetByCode(systemCode);
List<MenuEntity> listMenu = GetMenusByRole(systemType.Id).OrderBy(t => t.SortCode).ToList();
list = listMenu.MapTo<MenuOutputDto>();
}
catch (Exception ex)
{
Log4NetHelper.Error("根据用户角色和子系统代码获取菜单异常", ex);
}
return list;
}
/// <summary>
/// 构建菜单树
/// </summary>
/// <param name="menus">菜单列表</param>
/// <param name="ParentId">父级Id</param>
/// <returns></returns>
public List<MenuOutputDto> BuildTreeMenus(List<MenuEntity> menus, long ParentId = 0)
{
List<MenuOutputDto> resultList = new List<MenuOutputDto>();
List<MenuEntity> childNodeList = menus.FindAll(t => t.ParentId == ParentId);
foreach (MenuEntity menu in childNodeList)
{
MenuOutputDto menuOutputDto = new MenuOutputDto();
menuOutputDto = menu.MapTo<MenuOutputDto>();
List<MenuEntity> subChildNodeList = menus.FindAll(t => t.ParentId == menu.Id);
if (subChildNodeList.Count > 0)
{
menuOutputDto.SubMenu = BuildTreeMenus(menus, menu.Id);
}
resultList.Add(menuOutputDto);
}
return resultList;
}
/// <summary>
/// 根据用户获取功能菜单
/// </summary>
/// <param name="roleIds">用户角色ID</param>
/// <param name="systemId">系统类型ID/子系统ID</param>
/// <returns></returns>
public List<MenuEntity> GetMenusByRole(long roleIds, string systemId)
{
List<MenuEntity> menuListResult = new List<MenuEntity>();
if (roleIds == 0)
{
menuListResult = service.GetFunctions("", systemId, true);
}
else
{
// string roleIDsStr = string.Format("'{0}'", roleIds.Replace(",", "','"));
string roleIDsStr = "";
menuListResult = service.GetFunctions(roleIDsStr, systemId, true);
}
return menuListResult;
}
/// <summary>
/// 根据用户获取功能菜单
/// </summary>
/// <param name="systemId">系统类型ID/子系统ID</param>
/// <returns></returns>
public List<MenuEntity> GetMenusByRole(long systemId)
{
List<MenuEntity> menuListResult = service
.GetAllByIsNotDeleteAndEnabledMark("MenuType in ('M','C') and SystemTypeId='" + systemId + "'")
.ToList();
return menuListResult;
}
/// <summary>
/// 根据用户ID,获取对应的功能列表
/// </summary>
/// <param name="userID">用户ID</param>
/// <param name="typeID">系统类别ID</param>
/// <returns></returns>
public List<MenuOutputDto> GetFunctionsByUser(long userId, string typeID)
{
string roleId = serviceUser.Get(userId).RoleId.ToString();
List<MenuOutputDto> functions = new List<MenuOutputDto>();
string roleIDsStr = string.Format("'{0}'", roleId.Replace(",", "','"));
if (roleIDsStr != "")
{
functions = service.GetFunctions(roleIDsStr, typeID).ToList().MapTo<MenuOutputDto>();
}
return functions;
}
/// <summary>
/// 根据用户角色IDs,获取对应的功能列表
/// </summary>
/// <param name="roleIds">用户角色ID</param>
/// <param name="systemId">系统类型ID/子系统ID</param>
/// <returns></returns>
public List<MenuOutputDto> GetFunctionsByRole(string roleIds, string systemId)
{
List<MenuOutputDto> functions = new List<MenuOutputDto>();
string roleIDsStr = string.Format("'{0}'", roleIds.Replace(",", "','"));
if (roleIDsStr != "")
{
functions = service.GetFunctions(roleIDsStr, systemId).ToList().MapTo<MenuOutputDto>();
}
return functions;
}
/// <summary>
/// 根据用户角色IDs,获取对应的功能列表
/// </summary>
/// <param name="systemId">系统类型ID/子系统ID</param>
/// <returns></returns>
public List<MenuOutputDto> GetFunctionsBySystem(string systemId)
{
List<MenuOutputDto> functions = new List<MenuOutputDto>();
functions = service.GetFunctions(systemId).ToList().MapTo<MenuOutputDto>();
return functions;
}
#region 获取 Vue Router
/// <summary>
/// 根据用户角色获取菜单树VueRouter模式
/// </summary>
/// <param name="roleIds">角色ID</param>
/// <param name="systemCode">系统类型代码子系统代码</param>
/// <returns></returns>
public List<VueRouterModel> GetVueRouter(long roleIds, string systemCode)
{
List<VueRouterModel> list = new List<VueRouterModel>();
try
{
SystemTypeEntity systemType = systemservice.GetByCode(systemCode);
List<MenuEntity> listMenu = GetMenusByRole(roleIds, systemType.Id.ToString()).OrderBy(t => t.SortCode).ToList();
List<MenuOutputDto> listTree = BuildTreeMenus(listMenu);
list = BuildMenus(listTree);
}
catch (Exception ex)
{
Log4NetHelper.Error("根据用户角色和子系统代码获取菜单异常", ex);
}
return list;
}
/// <summary>
/// 构建前端路由所需要的菜单
/// </summary>
/// <param name="menus">菜单列表</param>
/// <returns></returns>
public List<VueRouterModel> BuildMenus(List<MenuOutputDto> menus)
{
List<VueRouterModel> routers = new List<VueRouterModel>();
foreach (MenuOutputDto menu in menus)
{
VueRouterModel router = new VueRouterModel
{
hidden = menu.IsShow ? false : true,
name = GetRouteName(menu),
path = GetRouterPath(menu),
component = GetComponent(menu)
};
Meta meta = new Meta(menu.FullName, menu.Icon == null ? "" : menu.Icon, menu.IsCache);
if (!menu.IsShow && menu.MenuType.Contains("M"))
{
meta.activeMenu = menu.ActiveMenu;
}
router.meta = meta;
List<MenuOutputDto> cMenus = menu.SubMenu;
if (cMenus != null && menu.MenuType == "C")
{
router.alwaysShow = true;
router.redirect = "noRedirect";
router.children = BuildMenus(cMenus);
}
else if (IsMeunDoc(menu))
{
List<VueRouterModel> childrenList = new List<VueRouterModel>();
VueRouterModel childrenRouter = new VueRouterModel
{
path = menu.UrlAddress,
component = menu.Component,
name = menu.EnCode,
meta = new Meta(menu.FullName, menu.Icon == null ? "" : menu.Icon, menu.IsCache)
};
childrenRouter.meta = meta;
childrenList.Add(childrenRouter);
router.children = childrenList;
}
else if (IsMeunFrame(menu) && cMenus != null)
{
if (!menu.IsShow)
{
router.hidden = true;
}
router.children = BuildMenus(cMenus);
}
routers.Add(router);
}
return routers;
}
/// <summary>
/// 获取路由名称
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public string GetRouteName(MenuOutputDto menu)
{
string routerName = menu.EnCode;
// 非外链并且是一级目录(类型为目录)
if (IsMeunDoc(menu))
{
routerName = "";
}
return routerName;
}
/// <summary>
/// 获取路由地址
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public string GetRouterPath(MenuOutputDto menu)
{
string routerPath = menu.UrlAddress;
// 非外链并且是一级目录(类型为目录)
if (0 == menu.ParentId && menu.MenuType == "C" && !menu.IsFrame)
{
routerPath = menu.UrlAddress; // "/" + menu.EnCode;
}
// 非外链并且是一级目录(类型为菜单)
else if (IsMeunDoc(menu))
{
routerPath = "/";
}
return routerPath;
}
/// <summary>
/// 获取组件信息
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public string GetComponent(MenuOutputDto menu)
{
string component = "Layout";
if (!string.IsNullOrEmpty(menu.Component) && IsMeunFrame(menu) && !IsMeunDoc(menu))
{
component = menu.Component;
}
else if (string.IsNullOrEmpty(menu.Component) && IsParentView(menu))
{
component = "ParentView";
}
return component;
}
/// <summary>
/// 是否为菜单内部跳转,并且是一级目录
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public bool IsMeunDoc(MenuOutputDto menu)
{
return menu.ParentId == 0 && menu.MenuType == "M" && !menu.IsFrame;
}
/// <summary>
/// 是否为菜单内部跳转
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public bool IsMeunFrame(MenuOutputDto menu)
{
return menu.MenuType == "M" && !menu.IsFrame;
}
/// <summary>
/// 是否为parent_view组件
/// </summary>
/// <param name="menu">菜单信息</param>
/// <returns></returns>
public bool IsParentView(MenuOutputDto menu)
{
return menu.ParentId != 0 && menu.MenuType == "C";
}
#endregion 获取 Vue Router
}
}