using Learn.Archives.API.Controllers.Dto; using Learn.Archives.API.Expand; using Learn.Archives.Core.Common; using Learn.Archives.Core.Model; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; using System.Security.Claims; using UserCenter.Model; namespace Learn.Archives.API.Controllers { /// /// 路由菜单 /// public class MenuController : BackController { readonly Repository baseService; readonly Repository menuRelationDB; readonly LiveUserInfo userInfo; public MenuController(Repository baseService, LiveUserInfo userInfo, Repository menuRelationDB) : base(baseService) { this.baseService = baseService; this.userInfo = userInfo; this.menuRelationDB = menuRelationDB; } /// /// 管理员菜单 /// /// [HttpGet] public async Task AdminMenu() { var rId = userInfo.RoleId; if (rId == 0) Oh.Error("登录了无效的用户"); var menuArr = await menuRelationDB.AsQueryable() .LeftJoin((mr, m) => mr.MenuId == m.Id) .Where((mr,m)=> mr.RoleId == userInfo.RoleId) .Select((mr, m) => m) .ToArrayAsync(); return GetChildren(menuArr, menuArr.First().Id); } /// /// 递归获取子菜单 /// [NonAction] private static MenuTree[] GetChildren(IEnumerable menus, long parentId) { return menus .Where(m => m.ParentId == parentId) .OrderBy(m => m.Rank) .Select(m => new MenuTree { Id = m.Id, Name = m.Name, Title = m.Title, Path = m.Path, IsButton = m.IsButton, Icon = m.Icon, Auths = m.Auths, Rank = m.Rank, ShowLink = m.ShowLink, ParentId = m.ParentId, Children = GetChildren(menus, m.Id) }) .ToArray(); } } }