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