using Learn.Archives.API.Controllers.Dto; using Learn.Archives.API.Expand; using Learn.Archives.Core.Common; using Learn.Archives.Core.Model; using Learn.Archives.Core.Model.Dto; 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 roleService; readonly Repository menuRelationDB; readonly LiveUserInfo userInfo; public MenuController(Repository baseService, LiveUserInfo userInfo, Repository menuRelationDB, Repository roleService) : base(baseService) { this.baseService = baseService; this.userInfo = userInfo; this.menuRelationDB = menuRelationDB; this.roleService = roleService; } /// /// 管理员菜单 /// /// [HttpGet] public async Task> All() { var rId = userInfo.RoleId; if (rId != 1) Oh.Error("登录了无效的用户"); return await baseService.GetListAsync(); } /// /// 管理员菜单 /// /// [HttpGet] public async Task AdminMenu() { var rId = userInfo.RoleId; if (rId == 0) Oh.Error("登录了无效的用户"); Menu[] menuArr ; if (userInfo.IsSa) menuArr = await baseService.AsQueryable() .Where(m => !m.IsButton).ToArrayAsync(); else menuArr = await menuRelationDB.AsQueryable() .LeftJoin((mr, m) => mr.MenuId == m.Id) .Where((mr, m) => mr.RoleId == userInfo.RoleId && !m.IsButton) .Select((mr, m) => m) .ToArrayAsync(); return GetChildren(menuArr, 0); } /// /// 递归获取子菜单 /// [NonAction] private static MenuTree[]? GetChildren(IEnumerable menus, long parentId) { var res = menus .Where(m => m.ParentId == parentId) .OrderBy(m => m.Rank) .Select(m => new MenuTree { Id = m.Id, Name = m.Name, Path = m.Path, Meta = new MenuMeta() { Title = m.Title, Icon = m.Icon, Rank = m.Rank, ShowLink = m.ShowLink, //Auths = m.Auths, }, ParentId = m.ParentId, Children = GetChildren(menus, m.Id) }) .ToArray(); return res.Length==0?null: res; } } }