using AutoMapper; using SqlSugar; using System; using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEAWCS_DTO.System; using WIDESEAWCS_Core; using WIDESEAWCS_Core.BaseRepository; using WIDESEAWCS_Core.BaseServices; using WIDESEAWCS_Core.Helper; using WIDESEAWCS_ISystemServices; using WIDESEAWCS_Model; using WIDESEAWCS_Model.Models; namespace WIDESEAWCS_SystemServices { public class Sys_MenuService : ServiceBase<Sys_Menu, IRepository<Sys_Menu>>, ISys_MenuService { private readonly IUnitOfWorkManage _unitOfWorkManage; private readonly IMapper _mapper; public Sys_MenuService(IRepository<Sys_Menu> BaseDal, IUnitOfWorkManage unitOfWorkManage, IMapper mapper) : base(BaseDal) { _unitOfWorkManage = unitOfWorkManage; _mapper = mapper; } /// <summary> /// 获å–当å‰ç”¨æˆ·æ‰€æœ‰èœå•与æƒé™ /// </summary> /// <returns></returns> public object GetCurrentMenuActionList() { object obj = GetMenuActionList(App.User.RoleId); if (obj is IEnumerable<object> list) { if (list.Any()) { return obj; } else { return WebResponseContent.Instance.Error("未获å–到èœå•ä¿¡æ¯"); } } return obj; } public List<MenuDTO> GetAllMenu() { List<Sys_Menu> menus = BaseDal.QueryData(x => x.Enable == 1 || x.Enable == 2).OrderByDescending(a => a.OrderNo).ThenByDescending(q => q.ParentId).ToList(); List<MenuDTO> _menus = _mapper.Map<List<MenuDTO>>(menus); _menus.ForEach(x => { x.MenuType ??= 0; if (!string.IsNullOrEmpty(x.Auth) && x.Auth.Length > 10) { try { x.Actions = x.Auth.DeserializeObject<List<ActionDTO>>(); } catch { } } x.Actions ??= new List<ActionDTO>(); }); string test = _menus.Serialize(); return _menus; } public object GetSuperAdminMenu() { return GetAllMenu().Select(x => new { id = x.MenuId, name = x.MenuName, url = x.Url, parentId = x.ParentId, icon = x.Icon, x.Enable, x.TableName, // 2022.03.26å¢žç§»åŠ¨ç«¯åŠ èœå•类型 permission = x.Actions.Select(s => s.Value).ToArray() }).ToList(); } public object GetMenuByRoleId(int roleId) { var menu = from a in GetPermissions(roleId) join b in GetAllMenu() on a.MenuId equals b.MenuId orderby b.OrderNo descending select new { id = a.MenuId, name = b.MenuName, url = b.Url, parentId = b.ParentId, icon = b.Icon, b.Enable, b.TableName, permission = a.UserAuthArr }; return menu.ToList(); } /// <summary> /// 获å–角色æƒé™æ—¶é€šè¿‡å®‰å…¨å—å…¸é”定的角色id /// </summary> private static ConcurrentDictionary<string, object> objKeyValue = new ConcurrentDictionary<string, object>(); public List<Permissions> GetPermissions(int roleId) { if (App.User.IsRoleIdSuperAdmin(roleId)) { //2020.12.27å¢žåŠ èœå•界é¢ä¸Šä¸æ˜¾ç¤ºï¼Œä½†å¯ä»¥åˆ†é…æƒé™ var permissions = BaseDal.QueryData(x => x.Enable == 1 || x.Enable == 2) .Select(a => new Permissions { MenuId = a.MenuId, ParentId = a.ParentId, TableName = (a.TableName ?? "").ToLower(), UserAuth = a.Auth, MenuType = a.MenuType ?? 0 }).ToList(); return MenuActionToArray(permissions); } //é”定æ¯ä¸ªè§’色,通过安全å—å…¸å‡å°‘é”粒度,å¦åˆ™å¤šä¸ªåŒæ—¶è§’色获å–缓å˜ä¼šå¯¼è‡´é˜»å¡ž object objId = objKeyValue.GetOrAdd(roleId.ToString(), new object()); //é”定æ¯ä¸ªè§’色 lock (objId) { //没有redis/memory缓å˜è§’è‰²çš„ç‰ˆæœ¬å·æˆ–ä¸Žå½“å‰æœåŠ¡å™¨çš„è§’è‰²ç‰ˆæœ¬å·ä¸åŒæ—¶ï¼Œåˆ·æ–°ç¼“å˜ List<Permissions> _permissions = BaseDal.QueryTabs<Sys_Menu, Sys_RoleAuth, Permissions>((a, b) => new object[] { JoinType.Inner, a.MenuId == b.MenuId }, (a, b) => new Permissions { MenuId = a.MenuId, ParentId = a.ParentId, TableName = (a.TableName ?? "").ToLower(), MenuAuth = a.Auth, UserAuth = b.AuthValue ?? "", MenuType = a.MenuType ?? 0 }, (a, b) => b.RoleId == roleId, x => true); ActionToArray(_permissions); return _permissions; } } private List<Permissions> MenuActionToArray(List<Permissions> permissions) { permissions.ForEach(x => { try { x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth) ? new string[0] : x.UserAuth.DeserializeObject<List<Sys_Actions>>().Select(s => s.Value).ToArray(); } catch { } finally { if (x.UserAuthArr == null) { x.UserAuthArr = new string[0]; } } }); return permissions; } private List<Permissions> ActionToArray(List<Permissions> permissions) { permissions.ForEach(x => { try { var menuAuthArr = x.MenuAuth.DeserializeObject<List<Sys_Actions>>(); x.UserAuthArr = string.IsNullOrEmpty(x.UserAuth) ? new string[0] : x.UserAuth.Split(",").Where(c => menuAuthArr.Any(m => m.Value == c)).ToArray(); } catch { } finally { if (x.UserAuthArr == null) { x.UserAuthArr = new string[0]; } } }); return permissions; } public object GetMenu(List<int> menuIds) { return BaseDal.QueryData(x => menuIds.Contains(x.MenuId)).Select(a => new { id = a.MenuId, parentId = a.ParentId, name = a.MenuName, a.MenuType, a.OrderNo }).OrderByDescending(a => a.OrderNo) .ThenByDescending(q => q.parentId).ToList(); } public object GetTreeItem(int menuId) { var sysMenu = BaseDal.QueryData(x => x.MenuId == menuId) .Select( p => new { p.MenuId, p.ParentId, p.MenuName, p.Url, p.Auth, p.OrderNo, p.Icon, p.Enable, // 2022.03.26å¢žç§»åŠ¨ç«¯åŠ èœå•类型 MenuType = p.MenuType ?? 0, p.CreateDate, p.Creater, p.TableName, p.ModifyDate }).FirstOrDefault(); return sysMenu; } /// <summary> /// æ ¹æ®è§’色ID获å–èœå•与æƒé™ /// </summary> /// <param name="roleId"></param> /// <returns></returns> public object GetMenuActionList(int roleId) { if (App.User.IsRoleIdSuperAdmin(roleId)) { return GetSuperAdminMenu(); } return GetMenuByRoleId(roleId); } public List<MenuDTO> GetUserMenuList(int roleId) { if (App.User.IsRoleIdSuperAdmin(roleId)) { return GetAllMenu(); } List<int> menuIds = GetPermissions(roleId).Select(x => x.MenuId).ToList(); return GetAllMenu().Where(x => menuIds.Contains(x.MenuId)).ToList(); } public List<ActionDTO> GetActions(int menuId, List<ActionDTO> menuActions, List<Permissions> permissions, int roleId) { if (App.User.IsRoleIdSuperAdmin(roleId)) { return menuActions; } return menuActions.Where(p => permissions .Exists(w => menuId == w.MenuId && w.UserAuthArr.Contains(p.Value))) .ToList(); } /// <summary> /// 编辑修改èœå•æ—¶,èŽ·å–æ‰€æœ‰èœå• /// </summary> /// <returns></returns> public object GetMenu() { if (App.User.IsRoleIdSuperAdmin(App.User.RoleId)) { List<int> menuIds = BaseDal.QueryData().Select(x => x.MenuId).ToList(); return GetMenu(menuIds); } else { List<int> menuIds = GetPermissions(App.User.RoleId).Select(x => x.MenuId).ToList(); return GetMenu(menuIds); } } /// <summary> /// 新建或编辑èœå• /// </summary> /// <param name="menu"></param> /// <returns></returns> public WebResponseContent Save(Sys_Menu menu) { WebResponseContent webResponse = new WebResponseContent(); if (menu == null) return webResponse.Error("没有获å–到æäº¤çš„傿•°"); if (menu.MenuId > 0 && menu.MenuId == menu.ParentId) return webResponse.Error("父级IDä¸èƒ½æ˜¯å½“å‰èœå•çš„ID"); try { //webResponse = menu.ValidationEntity(x => new { x.MenuName, x.TableName }); //if (!webResponse.Status) return webResponse; if (menu.TableName != "/" && menu.TableName != ".") { // 2022.03.26å¢žç§»åŠ¨ç«¯åŠ èœå•ç±»åž‹åˆ¤æ– Sys_Menu sysMenu = BaseDal.QueryFirst(x => x.TableName == menu.TableName); if (sysMenu != null) { sysMenu.MenuType ??= 0; if (sysMenu.MenuType == menu.MenuType) { if ((menu.MenuId > 0 && sysMenu.MenuId != menu.MenuId) || menu.MenuId <= 0) { return webResponse.Error($"视图/表åã€{menu.TableName}】已被其他èœå•使用"); } } } } bool _changed = false; if (menu.MenuId <= 0) { int id = BaseDal.AddData(menu); menu.MenuId = id; } else { //2020.05.07æ–°å¢žç¦æ¢é€‰æ‹©ä¸Šçº§è§’色为自己 if (menu.MenuId == menu.ParentId) { return webResponse.Error($"父级idä¸èƒ½ä¸ºè‡ªå·±"); } if (BaseDal.QueryFirst(x => x.ParentId == menu.MenuId && menu.ParentId == x.MenuId) != null) { return webResponse.Error($"ä¸èƒ½é€‰æ‹©æ¤çˆ¶çº§id,选择的父级id与当å‰èœå•å½¢æˆä¾èµ–关系"); } _changed = BaseDal.QueryData(c => c.MenuId == menu.MenuId).Select(s => s.Auth).FirstOrDefault() != menu.Auth; BaseDal.UpdateData(menu); } webResponse.OK("ä¿å˜æˆåŠŸ", menu); } catch (Exception ex) { webResponse.Error(ex.Message); } return webResponse; } public WebResponseContent DelMenu(int menuId) { WebResponseContent webResponse = new WebResponseContent(); if(BaseDal.QueryFirst(x=>x.ParentId == menuId) != null) { return webResponse = WebResponseContent.Instance.Error("当å‰èœå•å˜åœ¨åèœå•,è¯·å…ˆåˆ é™¤åèœå•!"); } BaseDal.DeleteDataById(menuId); return webResponse = WebResponseContent.Instance.OK("åˆ é™¤æˆåŠŸ"); } } }