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 { /// /// 管理员角色控制器 /// public class MenuRelationController : BackController { readonly Repository roleService; readonly Repository mrService; readonly LiveUserInfo userInfo; public MenuRelationController(Repository baseService, LiveUserInfo userInfo, Repository mrService) : base(mrService) { this.roleService = baseService; this.userInfo = userInfo; this.mrService = mrService; } public class SetMenuInput { public long RoleId { get; set; } public long[] MenuId { get; set; } } /// /// 获取角色菜单ids /// /// [HttpGet] public async Task RoleMenu(long roleId) { if (roleId == 0) Oh.Error("无效的角色"); if (!userInfo.IsSa) Oh.Error("无权访问"); var role = await roleService.GetByIdAsync(roleId); if (role == null) Oh.Error("无效的角色"); return await mrService.AsQueryable() .Where(s=>s.RoleId==roleId) .Select(s=>s.MenuId) .ToArrayAsync(); } /// /// 设置角色菜单 /// /// [HttpPost] public async Task SetMenu(SetMenuInput input) { if (input.MenuId == null || input.MenuId.Length==0) Oh.Error("选择了无可用的菜单"); if (!userInfo.IsSa) Oh.Error("登录了无效的用户"); var role = await roleService.GetByIdAsync(input.RoleId); if (role == null) Oh.Error("无效的角色"); var insertData= input.MenuId! .Select(s=>new MenuRelation() {RoleId = role!.Id,MenuId = s }) .ToArray(); await mrService.AsDeleteable().Where(s=>s.RoleId == role!.Id).ExecuteCommandAsync(); return await mrService.InsertRangeAsync(insertData); } } }