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; namespace asg_form.Controllers { 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 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; } } private readonly UserManager userManager; /// /// 新增菜单 /// [Route("api/v1/admin/menuAdd")] [HttpPost] [Authorize] public async Task> 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, }; 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; db.SaveChanges(); } return Ok(new error_mb { code = 200, message = "成功存入" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", ex }); } } } /// /// 删除菜单 /// [Route("api/v1/admin/menuDel")] [HttpDelete] [Authorize] public async Task> 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 { db.Remove(db.mainMenu.FirstOrDefaultAsync(n => n.id==uid)); await db.SaveChangesAsync(); return Ok(new error_mb { code = 200, message = "成功删除" }); } catch (Exception ex) { return Ok(new { code = 500, message = "服务器错误", ex }); } } } } }