207 lines
7.7 KiB
C#
207 lines
7.7 KiB
C#
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Identity;
|
|
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
|
|
{
|
|
|
|
|
|
[ApiController]
|
|
public class menuAssignController : ControllerBase
|
|
{
|
|
public class menuDB
|
|
{
|
|
public string id { get; set; }
|
|
public string path { get; set; }
|
|
public string icon_class { get; set; }
|
|
public string title { get; set; }
|
|
public string parent_id { get; set; }
|
|
public string auth { get; set; }
|
|
public string component { get; set; }
|
|
public string allow_operate { get; set; }
|
|
public string show { get; set; }
|
|
public int sort { get; set; }
|
|
public string adaptability { get; set; }
|
|
}
|
|
public class menuInput
|
|
{
|
|
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 int sort { get; set; }
|
|
public string adaptability { get; set; }
|
|
}
|
|
|
|
private readonly UserManager<User> userManager;
|
|
|
|
/// <summary>
|
|
/// 新增菜单
|
|
/// </summary>
|
|
[Route("api/v1/admin/menuAdd")]
|
|
[HttpPost]
|
|
[Authorize]
|
|
public async Task<ActionResult<object>> menuAdd([FromBody] menuInput msg)
|
|
{
|
|
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 mA = db.mainMenu.Find(msg.id);
|
|
if (mA == null)
|
|
{
|
|
var menu = new menuDB
|
|
{
|
|
id = msg.id,
|
|
path = msg.path,
|
|
icon_class = msg.iconClass,
|
|
title = msg.title,
|
|
parent_id = msg.parentId,
|
|
auth = msg.auth,
|
|
component = msg.component,
|
|
allow_operate = msg.allowOperate,
|
|
show = msg.show,
|
|
sort = msg.sort,
|
|
adaptability = msg.adaptability == null ? "1" :msg.adaptability,
|
|
};
|
|
db.mainMenu.Add(menu);
|
|
db.SaveChanges();
|
|
}
|
|
else
|
|
{
|
|
mA.id = msg.id;
|
|
mA.path = msg.path;
|
|
mA.icon_class = msg.iconClass;
|
|
mA.title = msg.title;
|
|
mA.parent_id = msg.parentId;
|
|
mA.auth = msg.auth;
|
|
mA.component = msg.component;
|
|
mA.allow_operate = msg.allowOperate;
|
|
mA.show = msg.show;
|
|
mA.sort = msg.sort;
|
|
mA.adaptability = msg.adaptability == null ? "1" : msg.adaptability;
|
|
db.SaveChanges();
|
|
}
|
|
return Ok(new error_mb { code = 200, message = "成功存入" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new { code = 500, message = "服务器错误", ex });
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除菜单
|
|
/// </summary>
|
|
[Route("api/v1/admin/menuDel")]
|
|
[HttpDelete]
|
|
[Authorize]
|
|
public async Task<ActionResult<object>> menuDel([FromQuery] string uid)
|
|
{
|
|
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 menumessage = db.mainMenu.FirstOrDefault(n => n.id == uid);
|
|
if (menumessage != null) db.Remove(menumessage);
|
|
else return Ok(new error_mb { code = 400, message = "数据不存在" });
|
|
db.SaveChanges();
|
|
return Ok(new error_mb { code = 200, message = "成功删除" });
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return Ok(new { code = 500, message = "服务器错误", ex });
|
|
}
|
|
}
|
|
}
|
|
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 int sort { get; set; } = 1;
|
|
public string adaptability { get; set; }
|
|
public List<menuFront> children { get; set; } = new List<menuFront>();
|
|
}
|
|
|
|
private List<menuFront> BuildTree(IEnumerable<menuDB> nodes, string? parentId)
|
|
{
|
|
try
|
|
{
|
|
return nodes.Where(x => x.parent_id == parentId)
|
|
.OrderBy(x => x.sort)
|
|
.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,
|
|
sort = x.sort,
|
|
adaptability = x.adaptability,
|
|
children = BuildTree(nodes, x.id)
|
|
}).ToList();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine($"Error in BuildTree: {ex.Message}");
|
|
throw;
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 查询菜单
|
|
/// </summary>
|
|
[Route("api/v1/admin/menuFind")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<object>> menuFind()
|
|
{
|
|
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 });
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|