/// 获取用户有权限访问的所有链接 /// </summary> /// <returns></returns> public List <CommonLink> GetAllLinks() { var links = new List <CommonLink>(); //判断用户是否存在,如果不在直接取AppType=0(全部)的数据 // 存在则判断勾选的用户|组织是否为当前用户(所属组织) if (currentUser != null) { var userOrg = currentUser.OrganizationID == null?Guid.Empty.ToString() : currentUser.OrganizationID; //条件1:应用范围为所有人|指定觉角色的 Expression <Func <CommonLink, bool> > predice = p => p.IsEnable && (p.AppalyType == 0 || p.AppalyType == 2); links = homelinkService.GenericService.GetAll(predice).ToList(); //用户所拥有的角色ID数组 var userRoles = (from p in new RoleUserService().GenericService.GetAll(p => p.UserID == currentUser.ID) select p.RoleID.ToString()).ToArray(); for (int i = links.Count - 1; i >= 0; i--) { var item = links[i]; if (item.AppalyType == 2) { //表示当前用户的角色无法访问该链接,移除链接项 if (!IsLinkInRole(userRoles, item.AppalyRoles)) { links.RemoveAt(i); continue; } } item.Url = CommonLinkService.PCFormatUrl(item.Url); item.PadUrl = CommonLinkService.PadFormatUrl(item.PadUrl); item.PhoneUrl = CommonLinkService.PhoneFormatUrl(item.PhoneUrl); } } else { links = homelinkService.GenericService.GetAll(p => p.IsEnable && p.AppalyType == 0).ToList(); links.ForEach(p => { p.Url = CommonLinkService.PCFormatUrl(p.Url); p.PadUrl = CommonLinkService.PadFormatUrl(p.PadUrl); p.PhoneUrl = CommonLinkService.PhoneFormatUrl(p.PhoneUrl); }); } var items = links.OrderBy(p => p.OrderNum).ToList(); return(items); }
public void SyncCommonLinks() { var keyName = SettingsKeyEnum.commonlink; if (!IsAllowUpdate(keyName, syncIntervalMillisecond)) { return; } var allLinksInDb = homelinkService.GenericService.GetAll().ToList(); var links = new List <CommonLink>(); //判断用户是否存在,如果不在提取空数据 // 存在则判断勾选的用户|组织是否为当前用户(所属组织) if (currentUser != null) { //默认默认显示为常用链接的数据 int count = 0; var pagelinksSettings = GetSettingsValue(SettingsKeyEnum.pagelink, out count); if (String.IsNullOrEmpty(pagelinksSettings)) { pagelinksSettings = "[]"; } var pageLinks = Newtonsoft.Json.JsonConvert.DeserializeObject <List <CommonLink> >(pagelinksSettings); var defaultCommonLinks = pageLinks.FindAll(p => p.IsCommonLink); var commonSettings = GetSettingsValue(keyName, out count); //从默认后台配置的显示在常用链接的数据项配置 if (String.IsNullOrEmpty(commonSettings)) { links.Clear(); //将获取的数据添加到配置 //先移除配置 // Type=99 表示是首页显示的混合类别图标 pageLinkUserService.GenericService.Delete(p => p.UserID == currentUser.ID && p.Type == 99); //将默认的数据项添加到用户配置中 var tempItems = defaultCommonLinks.OrderBy(p => p.OrderNum).ToList(); for (var i = 0; i < tempItems.Count(); i++) { var id = tempItems[i].ID; pageLinkUserService.GenericService.Add(new PageLinkUserConfig() { ID = Guid.NewGuid().ToString(), LinkID = id, UserID = currentUser.ID, OrderNum = i + 1, Type = 99, IsEnable = true, IsShowIndex = true }); } pageLinkUserService.GenericService.Save(); //设置返回的数据源为默认显示在公共链接的数据 links = defaultCommonLinks; } //通过配置来取 else { links.Clear(); //获取用户配置的列表 var configItems = pageLinkUserService.GenericService.GetAll(p => p.UserID == currentUser.ID && p.Type == 99).ToList(); foreach (var config in configItems) { var link = allLinksInDb.FirstOrDefault(p => p.ID == config.LinkID); if (link != null && link.IsEnable) { link.OrderNum = config.OrderNum; link.IsEnable = config.IsEnable; link.IsCommonLink = config.IsShowIndex; links.Add(link); } } var linkIds = links.Select(p => p.ID).ToList(); //将用户配置的数据与默认为常用链接的数据对比 //如果不存在于配置中,则添加到配置 foreach (var item in defaultCommonLinks) { if (!linkIds.Contains(item.ID)) { links.Add(item); } } } } foreach (var item in links) { item.Url = CommonLinkService.PCFormatUrl(item.Url); item.PadUrl = CommonLinkService.PadFormatUrl(item.PadUrl); item.PhoneUrl = CommonLinkService.PhoneFormatUrl(item.PhoneUrl); } var items = links.FindAll(p => p.IsCommonLink).OrderBy(p => p.OrderNum).ToList(); var linksJson = Newtonsoft.Json.JsonConvert.SerializeObject(items); AddOrUpdate(keyName, linksJson, items.Count()); }