72 lines
2.3 KiB
C#
72 lines
2.3 KiB
C#
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
|
|
{
|
|
/// <summary>
|
|
/// 路由菜单
|
|
/// </summary>
|
|
public class MenuController : BackController<Menu>
|
|
{
|
|
readonly Repository<Menu> baseService;
|
|
readonly Repository<MenuRelation> menuRelationDB;
|
|
readonly LiveUserInfo userInfo;
|
|
public MenuController(Repository<Menu> baseService, LiveUserInfo userInfo, Repository<MenuRelation> menuRelationDB) : base(baseService)
|
|
{
|
|
this.baseService = baseService;
|
|
this.userInfo = userInfo;
|
|
this.menuRelationDB = menuRelationDB;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 管理员菜单
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpGet]
|
|
public async Task<MenuTree[]> AdminMenu()
|
|
{
|
|
var rId = userInfo.RoleId;
|
|
if (rId == 0) Oh.Error("登录了无效的用户");
|
|
var menuArr = await menuRelationDB.AsQueryable()
|
|
.LeftJoin<Menu>((mr, m) => mr.MenuId == m.Id)
|
|
.Where((mr,m)=> mr.RoleId == userInfo.RoleId)
|
|
.Select((mr, m) => m)
|
|
.ToArrayAsync();
|
|
return GetChildren(menuArr, menuArr.First().Id);
|
|
}
|
|
/// <summary>
|
|
/// 递归获取子菜单
|
|
/// </summary>
|
|
[NonAction]
|
|
private static MenuTree[] GetChildren(IEnumerable<Menu> 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();
|
|
}
|
|
|
|
}
|
|
}
|