123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- // <author></author>
- // <date></date>
- // <description></description>
- using ERP.Core.Constant;
- using ERP.Core.Dto;
- using ERP.Core.Entities;
- using ERP.Core.Interface;
- using ERP.Core.Repositorys;
- using ERP.Framework.Security;
- using ERP.Framework.Utils;
- namespace ERP.Core.Services
- {
- public class SysMenuService : ISysMenuService
- {
- private readonly SysMenuRepository _sysMenuRepository;
- /// <summary>
- ///
- /// </summary>
- /// <param name="sysMenuRepository"></param>
- public SysMenuService(SysMenuRepository sysMenuRepository)
- {
- _sysMenuRepository = sysMenuRepository;
- }
- /// <summary>
- /// 获取
- /// </summary>
- /// <returns></returns>
- public List<SysMenu> SelectMenuTreeByUserId(long userId)
- {
- List<SysMenu> result;
- if (LoginHelper.IsAdmin())
- {
- result = _sysMenuRepository.List();
- }
- else
- {
- result = _sysMenuRepository.SelectMenuListByUserId(userId);
- }
- return TreeUtil.Build(result);
- }
- public List<Router> BuildMenuTree(List<SysMenu> list, string parentPath)
- {
- var routers = new List<Router>();
- foreach (var menu in list)
- {
- var router = new Router()
- {
- Hidden = menu.Visible,
- Name = menu.Path,
- Path = GetRoutePath(menu, parentPath),
- Component = GetComponent(menu),
- Meta = new Meta()
- {
- Icon = menu.Icon,
- Title = menu.MenuName,
- //IsCache = menu.ca
- }
- };
- var cMenus = menu.Children;
- if (cMenus != null && menu.MenuType == Enums.MenuTypeEnum.Catalog)
- {
- router.Children = BuildMenuTree(cMenus, router.Path);
- }
- else if (IsMenuFrame(menu))
- {
- var childrenList = new List<Router>();
- var children = new Router()
- {
- Path = menu.Path,
- Component = menu.Component,
- Name = menu.Path,
- Meta = new Meta()
- {
- Icon = menu.Icon,
- Title = menu.MenuName
- }
- };
- childrenList.Add(children);
- router.Children = childrenList;
- }
- routers.Add(router);
- }
- return routers;
- }
- private string GetRoutePath(SysMenu menu, string parentPath)
- {
- string routerPath;
- if (menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog)
- {
- routerPath = "/" + menu.Path;
- }
- else if (IsMenuFrame(menu))
- {
- routerPath = "/";
- }
- else
- {
- routerPath = parentPath + "/" + menu.Path;
- }
- return routerPath;
- }
- /// <summary>
- /// 是否为一级菜单
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- private bool IsMenuFrame(SysMenu menu)
- {
- return menu.ParentId == 0 && menu.MenuType == Enums.MenuTypeEnum.Menu;
- }
- /// <summary>
- /// 是否为中间组件
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- private bool IsParentView(SysMenu menu)
- {
- return menu.ParentId != 0 && menu.MenuType == Enums.MenuTypeEnum.Catalog;
- }
- /// <summary>
- /// 获取组件值
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- private string GetComponent(SysMenu menu)
- {
- var component = CoreConstant.LAYOUT;
- if (!menu.Component.IsNullOrEmpty() && !IsMenuFrame(menu))
- {
- component = menu.Component;
- }
- else if (menu.Component.IsNullOrEmpty() && IsParentView(menu))
- {
- component = CoreConstant.PARENT_VIEW;
- }
- return component;
- }
- }
- }
|