123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612 |
- using Attribute;
- using Common;
- using Dto;
- using Enums;
- using Extensions;
- using Model;
- using Service;
- using Services;
- using Vo;
- namespace Services
- {
- /// <summary>
- /// 菜单
- /// </summary>
- [AppService(ServiceType = typeof(ISysMenuService), ServiceLifetime = LifeTime.Transient)]
- public class SysMenuService : BaseService<SysMenu>, ISysMenuService
- {
- public ISysRoleService SysRoleService;
- public SysMenuService(ISysRoleService sysRoleService)
- {
- SysRoleService = sysRoleService;
- }
- /// <summary>
- /// 获取所有菜单数(菜单管理)
- /// </summary>
- /// <returns></returns>
- public List<TreeSelectVo> SelectTreeMenuList(MenuQueryDto menu, long userId)
- {
- // if (menu.parentId != null)
- // {
- // return GetMenusByMenuId(menu.parentId.ParseToInt(), userId);
- // }
- List<TreeSelectVo> menuList = BuildMenuTreeSelect(SelectMenuList(menu, userId));
- return menuList;
- }
- /// <summary>
- /// 获取所有菜单列表
- /// </summary>
- /// <returns></returns>
- public List<SysMenu> SelectMenuList(MenuQueryDto menu, long userId)
- {
- List<SysMenu> menuList;
- if (SysRoleService.IsAdmin(userId))
- {
- menuList = SelectMenuList(menu);
- }
- else
- {
- var userRoles = SysRoleService.SelectUserRoles(userId);
- menuList = SelectMenuListByRoles(menu, userRoles);
- }
- return menuList;
- }
- /// <summary>
- /// 获取菜单详情
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- public SysMenu GetMenuByMenuId(int menuId)
- {
- return GetFirst(it => it.menuId == menuId);
- }
- /// <summary>
- /// 根据菜单id获取菜单列表
- /// </summary>
- /// <param name="menuId"></param>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<SysMenu> GetMenusByMenuId(int menuId, long userId)
- {
- var menuExpression = Expressionable.Create<SysMenu>();
- menuExpression.And(c => c.parentId == menuId);
- if (!SysRoleService.IsAdmin(userId))
- {
- var userRoles = SysRoleService.SelectUserRoles(userId);
- var roleMenus = Context.Queryable<SysRoleMenu>()
- .Where(r => userRoles.Contains(r.RoleId)).Select(s => s.MenuId).ToList();
- menuExpression.And(c => roleMenus.Contains(c.menuId));
- }
- var list = GetList(menuExpression.ToExpression()).OrderBy(f => f.sortOrder).ToList();
- Context.ThenMapper(list, item =>
- {
- item.subNum = Context.Queryable<SysMenu>().SetContext(x => x.parentId, () => item.menuId, item).Count;
- });
- return list;
- }
- /// <summary>
- /// 添加菜单
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- public long AddMenu(SysMenu menu)
- {
- menu.CreateTime = DateTime.Now;
- menu.UpdateTime = DateTime.Now;
- return InsertReturnBigIdentity(menu);
- }
- /// <summary>
- /// 编辑菜单
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- public long EditMenu(SysMenu menu)
- {
- menu.UpdateTime = DateTime.Now;
- menu.icon = string.IsNullOrEmpty(menu.icon) ? "" : menu.icon;
- return Update(menu, true);
- }
- /// <summary>
- /// 删除菜单管理信息
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- public int DeleteMenuById(int menuId)
- {
- return Delete(menuId);
- }
- /// <summary>
- /// 校验菜单名称是否唯一
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- public string CheckMenuNameUnique(SysMenu menu)
- {
- long menuId = menu.menuId == 0 ? -1 : menu.menuId;
- SysMenu info = GetFirst(it => it.name == menu.name && it.parentId == menu.parentId);
- //if (info != null && menuId != info.menuId && menu.menuName.Equals(info.menuName))
- //{
- // return UserConstants.NOT_UNIQUE;
- //}
- if (info != null && info.menuId != menu.menuId)
- {
- return UserConstants.NOT_UNIQUE;
- }
- return UserConstants.UNIQUE;
- }
- /// <summary>
- /// 菜单排序
- /// </summary>
- /// <param name="menuDto"></param>
- /// <returns></returns>
- public int ChangeSortMenu(MenuDto menuDto)
- {
- return Update(new SysMenu() { menuId = menuDto.menuId, sortOrder = menuDto.sortOrder }, it => new { it.sortOrder });
- }
- /// <summary>
- /// 是否存在菜单子节点
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- public bool HasChildByMenuId(long menuId)
- {
- return Count(it => it.parentId == menuId) > 0;
- }
- /// <summary>
- /// 获取左边导航栏菜单树
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<SysMenu> SelectMenuTreeByUserId(long userId)
- {
- MenuQueryDto dto = new() { delFlag = "0", menuTypeIds = "M,C" };
- if (SysRoleService.IsAdmin(userId))
- {
- return SelectTreeMenuList(dto);
- }
- else
- {
- List<long> roleIds = SysRoleService.SelectUserRoles(userId);
- return SelectTreeMenuListByRoles(dto, roleIds);
- }
- }
- /// <summary>
- /// 查询精确到按钮的操作权限
- /// </summary>
- /// <param name="userId"></param>
- /// <returns></returns>
- public List<string> SelectMenuPermsByUserId(long userId)
- {
- var menus = Context.Queryable<SysMenu, SysRoleMenu, SysUserRole, SysRole>((m, rm, ur, r) => new JoinQueryInfos(
- JoinType.Left, m.menuId == rm.MenuId,
- JoinType.Left, rm.RoleId == ur.RoleId,
- JoinType.Left, ur.RoleId == r.roleId
- ))
- .WithCache(60 * 10)
- .Where((m, rm, ur, r) => m.DelFlag == "0" && ur.UserId == userId)
- .Select((m, rm, ur, r) => m).ToList();
- var menuList = menus.Where(f => !string.IsNullOrEmpty(f.permission));
- return menuList.Select(x => x.permission).Distinct().ToList();
- }
- /// <summary>
- /// 根据用户查询系统菜单列表
- /// </summary>
- /// <param name="menu"></param>
- /// <param name="roles">用户角色集合</param>
- /// <returns></returns>
- public List<SysMenu> SelectTreeMenuListByRoles(MenuQueryDto menu, List<long> roles)
- {
- var roleMenus = Context.Queryable<SysRoleMenu>()
- .Where(r => roles.Contains(r.RoleId))
- .Select(f => f.MenuId).Distinct().ToList();
- return Queryable()
- .Where(c => roleMenus.Contains(c.menuId))
- .WhereIF(!string.IsNullOrEmpty(menu.menuName), (c) => c.name.Contains(menu.menuName))
- .WhereIF(!string.IsNullOrEmpty(menu.visible), (c) => c.visible == menu.visible)
- .WhereIF(!string.IsNullOrEmpty(menu.delFlag), (c) => c.DelFlag == menu.delFlag)
- // .WhereIF(!string.IsNullOrEmpty(menu.menuTypeIds), c => menu.menuTypeIdArr.Contains(c.menuType))
- .OrderBy((c) => new { c.parentId, c.sortOrder })
- .Select(c => c)
- .ToTree(it => it.children, it => it.parentId, 0);
- }
- /// <summary>
- /// 根据用户查询系统菜单列表
- /// </summary>
- /// <param name="menu"></param>
- /// <param name="roleId">用户角色</param>
- /// <returns></returns>
- public List<RoleMenuExportDto> SelectRoleMenuListByRole(MenuQueryDto menu, int roleId)
- {
- var menuIds = Context.Queryable<SysRoleMenu>()
- .Where(r => r.RoleId == roleId)
- .Select(f => f.MenuId).Distinct().ToList();
- return Context.Queryable<SysMenu>()
- .InnerJoin<SysMenu>((t1, t2) => t1.menuId == t2.parentId)
- .InnerJoin<SysMenu>((t1, t2, t3) => t2.menuId == t3.parentId)
- .Where((t1, t2, t3) => menuIds.Contains(t1.menuId))
- .Select((t1, t2, t3) => new RoleMenuExportDto()
- {
- MenuName = $"{t1.name}->{t2.name}->{t3.name}",
- Path = t2.path,
- Component = t2.path,
- Perms = t3.permission,
- MenuType = (MenuType)(object)t3.menuType,
- Status = (MenuStatus)(object)t3.DelFlag
- }).ToList();
- }
- /// <summary>
- /// 获取所有菜单
- /// </summary>
- /// <returns></returns>
- private List<SysMenu> SelectMenuList(MenuQueryDto menu)
- {
- var menuExp = Expressionable.Create<SysMenu>();
- menuExp.AndIF(!string.IsNullOrEmpty(menu.menuName), it => it.name.Contains(menu.menuName));
- menuExp.AndIF(!string.IsNullOrEmpty(menu.visible), it => it.visible == menu.visible);
- menuExp.AndIF(!string.IsNullOrEmpty(menu.delFlag), it => it.DelFlag == menu.delFlag);
- // menuExp.AndIF(!string.IsNullOrEmpty(menu.menuTypeIds), it => menu.menuTypeIdArr.Contains(it.menuType));
- menuExp.AndIF(menu.parentId != null, it => it.parentId == menu.parentId);
- return Queryable()
- .Where(menuExp.ToExpression())
- .OrderBy(it => new { it.parentId, it.sortOrder })
- .ToList();
- }
- /// <summary>
- /// 根据用户查询系统菜单列表
- /// </summary>
- /// <param name="sysMenu"></param>
- /// <param name="roles">用户角色集合</param>
- /// <returns></returns>
- private List<SysMenu> SelectMenuListByRoles(MenuQueryDto sysMenu, List<long> roles)
- {
- var roleMenus = Context.Queryable<SysRoleMenu>()
- .Where(r => roles.Contains(r.RoleId));
- return Queryable()
- .InnerJoin(roleMenus, (c, j) => c.menuId == j.MenuId)
- .Where((c, j) => c.DelFlag == "0")
- .WhereIF(!string.IsNullOrEmpty(sysMenu.menuName), (c, j) => c.name.Contains(sysMenu.menuName))
- .WhereIF(!string.IsNullOrEmpty(sysMenu.visible), (c, j) => c.visible == sysMenu.visible)
- .OrderBy((c, j) => new { c.parentId, c.sortOrder })
- .Select(c => c)
- .ToList();
- }
- /// <summary>
- /// 获取所有菜单(菜单管理)
- /// </summary>
- /// <returns></returns>
- public List<SysMenu> SelectTreeMenuList(MenuQueryDto menu)
- {
- int parentId = menu.parentId != null ? (int)menu.parentId : 0;
- var list = Queryable()
- .WithCache(60 * 10)
- .WhereIF(!string.IsNullOrEmpty(menu.menuName), it => it.name.Contains(menu.menuName))
- .WhereIF(!string.IsNullOrEmpty(menu.visible), it => it.visible == menu.visible)
- .WhereIF(!string.IsNullOrEmpty(menu.delFlag), it => it.DelFlag == menu.delFlag)
- // .WhereIF(!string.IsNullOrEmpty(menu.menuTypeIds), it => menu.menuTypeIdArr.Contains(it.menuType))
- .WhereIF(menu.parentId != null, it => it.parentId == menu.parentId)
- .OrderBy(it => new { it.parentId, it.sortOrder })
- .ToTree(it => it.children, it => it.parentId, parentId);
- return list;
- }
- #region 方法
- ///// <summary>
- ///// 根据父节点的ID获取所有子节点
- ///// </summary>
- ///// <param name="list">分类表</param>
- ///// <param name="parentId">传入的父节点ID</param>
- ///// <returns></returns>
- //public List<SysMenu> GetChildPerms(List<SysMenu> list, int parentId)
- //{
- // List<SysMenu> returnList = new List<SysMenu>();
- // var data = list.FindAll(f => f.parentId == parentId);
- // foreach (var item in data)
- // {
- // RecursionFn(list, item);
- // returnList.Add(item);
- // }
- // return returnList;
- //}
- /// <summary>
- /// 递归列表
- /// </summary>
- /// <param name="list"></param>
- /// <param name="t"></param>
- private void RecursionFn(List<SysMenu> list, SysMenu t)
- {
- //得到子节点列表
- List<SysMenu> childList = GetChildList(list, t);
- t.children = childList;
- t.meta = new TreeSelectMeta();
- t.meta.title = t.name;
- t.meta.icon = t.icon;
- t.id = t.menuId;
- foreach (var item in childList)
- {
- if (GetChildList(list, item).Count() > 0)
- {
- RecursionFn(list, item);
- }
- }
- }
- /// <summary>
- /// 递归获取子菜单
- /// </summary>
- /// <param name="list">所有菜单</param>
- /// <param name="sysMenu"></param>
- /// <returns></returns>
- private List<SysMenu> GetChildList(List<SysMenu> list, SysMenu sysMenu)
- {
- return list.Where(p => p.parentId == sysMenu.menuId).ToList();
- }
- /// <summary>
- /// 获取路由侧边栏,动态生成
- /// </summary>
- /// <param name="menus"></param>
- /// <returns></returns>
- public List<RouterVo> BuildMenus(List<SysMenu> menus)
- {
- List<RouterVo> routers = new();
- if (menus == null) return routers;
- foreach (var menu in menus)
- {
- RouterVo router = new()
- {
- Hidden = "1".Equals(menu.visible),
- Name = GetRouteName(menu),
- Path = GetRoutePath(menu),
- Component = GetComponent(menu),
- Meta = new Meta(menu.name, menu.icon, "1".Equals(menu.keepAlive), menu.enName, menu.path, menu.CreateTime.Value)
- };
- List<SysMenu> cMenus = menu.children;
- //是目录并且有子菜单
- if (cMenus != null && cMenus.Count > 0 && UserConstants.TYPE_DIR.Equals(menu.menuType))
- {
- router.AlwaysShow = true;
- router.Redirect = "noRedirect";
- router.Children = BuildMenus(cMenus);
- }
- else if (IsMeunFrame(menu))
- {
- router.Meta = null;
- List<RouterVo> childrenList = new();
- RouterVo children = new()
- {
- Path = menu.path,
- Name = string.IsNullOrEmpty(menu.path) ? "" : menu.path.ToLower(),
- Meta = new Meta(menu.name, menu.icon, "1".Equals(menu.keepAlive), menu.enName, menu.path, menu.CreateTime.Value)
- };
- childrenList.Add(children);
- router.Children = childrenList;
- }
- else if (menu.parentId == 0 && IsInnerLink(menu))
- {
- router.Meta = new Meta(menu.name, menu.icon);
- router.Path = "/";
- List<RouterVo> childrenList = new();
- RouterVo children = new();
- string routerPath = InnerLinkReplaceEach(menu.path);
- children.Path = routerPath;
- children.Component = UserConstants.INNER_LINK;
- children.Name = routerPath.ToLower();
- children.Meta = new Meta(menu.name, menu.icon, menu.path);
- childrenList.Add(children);
- router.Children = childrenList;
- }
- routers.Add(router);
- }
- return routers;
- }
- /// <summary>
- /// 构建前端所需要下拉树结构
- /// </summary>
- /// <param name="menus">菜单列表</param>
- /// <returns>下拉树结构列表</returns>
- public List<SysMenu> BuildMenuTree(List<SysMenu> menus)
- {
- List<SysMenu> returnList = new();
- List<long> tempList = menus.Select(f => f.menuId).ToList();
- foreach (var menu in menus)
- {
- // 如果是顶级节点, 遍历该父节点的所有子节点
- if (!tempList.Contains(menu.parentId))
- {
- var menuInfo = menus.Find(f => f.menuId == menu.menuId);
- //移除按钮没有上级
- if (!tempList.Contains(menuInfo.parentId) && menu.menuType != "F")
- {
- RecursionFn(menus, menu);
- returnList.Add(menu);
- }
- }
- }
- if (!returnList.Any())
- {
- returnList = menus;
- }
- return returnList;
- }
- /// <summary>
- /// 构建前端所需要下拉树结构
- /// </summary>
- /// <param name="menus"></param>
- /// <returns></returns>
- public List<TreeSelectVo> BuildMenuTreeSelect(List<SysMenu> menus)
- {
- List<SysMenu> menuTrees = BuildMenuTree(menus);
- List<TreeSelectVo> treeMenuVos = new List<TreeSelectVo>();
- foreach (var item in menuTrees)
- {
- treeMenuVos.Add(new TreeSelectVo(item));
- }
- return treeMenuVos;
- }
- public List<LeftMenuVo> BuildLeftMenuTreeSelect(List<SysMenu> menus)
- {
- List<SysMenu> menuTrees = BuildMenuTree(menus);
- List<LeftMenuVo> treeMenuVos = new List<LeftMenuVo>();
- foreach (var item in menuTrees)
- {
- treeMenuVos.Add(new LeftMenuVo(item));
- }
- return treeMenuVos;
- }
- /// <summary>
- /// 获取路由名称
- /// </summary>
- /// <param name="menu">菜单信息</param>
- /// <returns>路由名称</returns>
- public string GetRouteName(SysMenu menu)
- {
- string routerName = menu.path.ToLower();
- // 非外链并且是一级目录(类型为目录)
- if (IsMeunFrame(menu))
- {
- routerName = string.Empty;
- }
- return routerName;
- }
- /// <summary>
- /// 获取路由路径
- /// </summary>
- /// <param name="menu">菜单信息</param>
- /// <returns>路由地址</returns>
- public string GetRoutePath(SysMenu menu)
- {
- string routerPath = menu.path;
- // 内链打开外网方式
- if (menu.parentId != 0 && IsInnerLink(menu))
- {
- routerPath = InnerLinkReplaceEach(routerPath);
- }
- // 非外链并且是一级目录(类型为目录)
- if (0 == menu.parentId && UserConstants.TYPE_DIR.Equals(menu.menuType)
- && UserConstants.NO_FRAME.Equals(menu.embedded))
- {
- routerPath = "/" + menu.path;
- }
- else if (IsMeunFrame(menu))// 非外链并且是一级目录(类型为菜单)
- {
- routerPath = "/";
- }
- return routerPath;
- }
- /// <summary>
- /// 获取组件名称
- /// </summary>
- /// <param name="menu"></param>
- /// <returns></returns>
- public string GetComponent(SysMenu menu)
- {
- string component = UserConstants.LAYOUT;
- // if (!string.IsNullOrEmpty(menu.Component) && !IsMeunFrame(menu))
- // {
- // component = menu.Component;
- // }
- // else if (menu.Component.IsEmpty() && menu.ParentId != 0 && IsInnerLink(menu))
- // {
- // component = UserConstants.INNER_LINK;
- // }
- // else if (string.IsNullOrEmpty(menu.Component) && IsParentView(menu))
- // {
- // component = UserConstants.PARENT_VIEW;
- // }
- return component;
- }
- /// <summary>
- /// 是否为菜单内部跳转
- /// </summary>
- /// <param name="menu">菜单信息</param>
- /// <returns></returns>
- public bool IsMeunFrame(SysMenu menu)
- {
- return menu.parentId == 0 && UserConstants.TYPE_MENU.Equals(menu.menuType)
- && menu.embedded.Equals(UserConstants.NO_FRAME);
- }
- /// <summary>
- /// 是否为内链组件
- /// </summary>
- /// <param name="menu">菜单信息</param>
- /// <returns>结果</returns>
- public bool IsInnerLink(SysMenu menu)
- {
- return menu.embedded.Equals(UserConstants.NO_FRAME) && Tools.IsUrl(menu.path);
- }
- /// <summary>
- /// 是否为parent_view组件
- /// </summary>
- /// <param name="menu">菜单信息</param>
- /// <returns></returns>
- public bool IsParentView(SysMenu menu)
- {
- return menu.parentId != 0 && UserConstants.TYPE_DIR.Equals(menu.menuType);
- }
- /// <summary>
- /// 内链域名特殊字符替换
- /// </summary>
- /// <param name = "path" ></ param >
- /// < returns ></ returns >
- public string InnerLinkReplaceEach(string path)
- {
- return path.IsNotEmpty() ? path
- .Replace(UserConstants.HTTP, "")
- .Replace(UserConstants.HTTPS, "")
- .Replace(UserConstants.WWW, "")
- .Replace(".", "/") : path;
- }
- #endregion
- public void AddSysMenu()
- {
-
- }
- }
- }
|