Esempio n. 1
0
        /// <summary>
        /// 尋找所有節點(Menu編輯側邊樹、Menu parentId下拉選單、後台選單、Role checkbox)
        /// </summary>
        /// <param name="parentMenu">The parent menu.</param>
        /// <param name="type">Enum:後台、前台</param>
        /// <param name="chooseMenu">Enum:全部、menu、包含角色</param>
        /// <param name="menuCheck">某角色所包含的選單</param>
        /// <returns></returns>
        public List<TreeViewModel> GetTrees(Menu parentMenu, MenuType type, MenuChoose chooseMenu = 0, IEnumerable<Menu> menuCheck = null)
        {
            IEnumerable<Menu> menuData;

            // 第一層:pareintId null
            if (parentMenu == null)
            {
                menuData =
                    Db.Menus.Where(x => x.ParentId == null && x.Type == (int)type && x.IsDelete == false)
                        .OrderBy(x => x.Sort)
                        .ToList();

                #region "取得使用者包含Menu"

                //if (chooseMenu == MenuChoose.Menu && roleMenu == null)
                //{
                //    roleMenu = Db.Admins.Find(UserHelper.Id).Roles.Where(x => x.IsDelete == false)
                //                                            .SelectMany(x => x.Menus).Where(x => x.IsDelete == false)
                //                                            .Distinct().ToList();
                //}

                #endregion
            }
            else
            {
                menuData = parentMenu.ChildMenus.Where(x => x.IsDelete == false).OrderBy(x => x.Sort).ToList();
            }

            // 篩選Menu(userMenu包含使用者擁有權限的menu)
            if (chooseMenu == MenuChoose.Menu)
            {
                menuData = menuData.Where(x => x.IsMenu);

                List<Guid> userMenu = UserHelper.UserMenu;

                if (UserHelper.IsSuperManager == false) //&& userMenu != null
                {
                    menuData = menuData.Where(x => userMenu.Contains(x.Id));
                }// super管理員的Menu、設定角色全限時 不用篩選menu
            }

            var tree = new List<TreeViewModel>();
            foreach (var menu in menuData)
            {
                var node = new TreeViewModel();
                node.Id = menu.Id;
                node.Name = menu.NativeName;
                node.IsMenu = menu.IsMenu ? "true" : "false";

                UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
                node.Url = url.Action(menu.Action, menu.Controller) + menu.Url.FieldToString(); //Keys.AdminRoot +

                // 角色是否包含選單
                node.IsChecked = (menuCheck != null) && menuCheck.Any(x => x.Id == menu.Id);

                // 若有子層繼續遞迴
                #region "遞迴"
                var child = Db.Menus.Where(x => x.ParentId == menu.Id && x.IsDelete == false);
                if (chooseMenu == MenuChoose.Menu) { child = child.Where(x => x.IsMenu); }
                int childCount = child.Count();
                if (childCount > 0)
                {
                    node.Nodes = GetTrees(menu, type, chooseMenu, menuCheck);
                }
                #endregion

                tree.Add(node);
            }

            return tree;
        }
Esempio n. 2
0
        /// <summary>
        /// 取得選單(前台)
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="lang">The language.</param>
        /// <param name="routeUrl">{museum}/{language}</param>
        /// <returns></returns>
        public List<TreeViewModel> GetFrontMenu(int type, int lang, string routeUrl = "")
        {
            IEnumerable<Menu> menuData =
                Db.Menus.Where(x => x.ParentId == null && x.Type == type && x.IsDelete == false && x.IsMenu)
                    .OrderBy(x => x.Sort)
                    .ToList();

            if (!string.IsNullOrEmpty(routeUrl))
            {
                routeUrl = "/" + routeUrl;
            }

            var tree = new List<TreeViewModel>();
            foreach (var menu in menuData)
            {
                var node = new TreeViewModel();
                node.Id = menu.Id;
                node.Url = !string.IsNullOrEmpty(menu.Url) ? menu.Url : (routeUrl + "/" + menu.Controller + "/" + menu.Action);
                node.Name = menu.NativeName;
                tree.Add(node);
            }

            return tree;
        }