diff --git a/asg_form/Controllers/menuAssignController.cs b/asg_form/Controllers/menuAssignController.cs index 2a0a6dc..5127761 100644 --- a/asg_form/Controllers/menuAssignController.cs +++ b/asg_form/Controllers/menuAssignController.cs @@ -4,6 +4,7 @@ using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using System.Security.Claims; using static asg_form.Controllers.InviteReferee; +using static asg_form.Controllers.menuAssignController; namespace asg_form.Controllers { @@ -118,5 +119,66 @@ namespace asg_form.Controllers } } } + public class menuFront + { + public string id { get; set; } + public string path { get; set; } + public string iconClass { get; set; } + public string title { get; set; } + public string parentId { get; set; } + public string auth { get; set; } + public string component { get; set; } + public string allowOperate { get; set; } + public string show { get; set; } + public List children { get; set; } = new List(); + } + + private List BuildTree(IEnumerable nodes, string? parentId) + { + return nodes.Where(x => x.parent_id == parentId) + .Select(x => new menuFront + { + id = x.id, + path = x.path, + iconClass = x.icon_class, + title = x.title, + parentId = x.parent_id, + auth = x.auth, + component = x.component, + allowOperate = x.allow_operate, + show = x.show, + children = BuildTree(nodes, x.id) + }).ToList(); + } + + /// + /// 查询菜单 + /// + [Route("api/v1/admin/menuFind")] + [HttpGet] + [Authorize] + public async Task> menuFind() + { + if (!this.User.FindAll(ClaimTypes.Role).Any(a => a.Value == "nbadmin")) + { + return BadRequest(new error_mb { code = 400, message = "无权访问" }); + } + using (var db = new TestDbContext()) + { + try + { + var query = db.mainMenu.AsQueryable(); + int total = query.Count(); + var allData = query.ToList(); + var rootNodes = allData.Where(x => x.parent_id == "-1").ToList(); + var treeData = BuildTree(allData, "-1").ToList(); + return Ok(new { code = 200, message = "" ,data=treeData,total}); + } + catch (Exception ex) + { + return Ok(new { code = 500, message = "服务器错误", ex }); + } + } + } } }