using Admin.Core.Model; using Admin.Core.Common.Resource; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using System; using System.Collections.Generic; using System.Linq.Expressions; using System.Threading.Tasks; using Admin.Core.Common; using AutoMapper; using Admin.Core.IService.ISys; using Admin.Core.Model.Sys; namespace Admin.Core.Api { /// /// SysMenuController /// [Route("api/[controller]/[action]")] [ApiController] [Authorize(Permissions.Name)] public class SysMenuController : BaseApiUserController { /// /// _sysMenuService /// private readonly ISysMenuService _sysMenuService; private readonly IMapper _mapper; /// /// 构造方法 /// /// /// /// public SysMenuController(ISysMenuService SysMenuService, IMapper mapper, ISysUserService sysUserService) : base(sysUserService) { _sysUserService = sysUserService; _sysMenuService = SysMenuService; _mapper = mapper; } /// /// 分页查询 /// /// /// [HttpPost] public async Task>> GetByPage([FromBody] PageQuery pageQuery) { if (!pageQuery.Query.IsNotEmptyOrNull()) { pageQuery.Query = new SysMenu(); } var model = await _sysMenuService.SelectMenuList(CurrentUser.UserID, pageQuery); var r = _mapper.Map>(model); return Success(r); } /// /// 根据ID查询 /// /// 主键 /// [HttpGet] public async Task> GetByID(int id) { return new MessageModel() { msg = Resource_SysBase.OprateSuccess, success = true, data = await _sysMenuService.QueryByIdAsync(id) }; } /// /// 新增一条数据 /// /// SysMenu /// [HttpPost] public async Task> Add([FromBody] SysMenu request) { var data = new MessageModel(); if (!await _sysMenuService.CheckMenuNameUnique(request)) { data.success = false; data.msg = "菜单名称已存在!"; return data; } if (SysConst.YES_FRAME == request.IsFrame && !StringHelper.IsUrl(request.Path)) { data.success = false; data.msg = "地址必须以http(s)://开头!"; return data; } request.CreateBy = CurrentUser.LoginName; request.UpdateBy = CurrentUser.LoginName; data.data = await _sysMenuService.InsertMenu(request); if (data.data > 0) { data.success = true; data.msg = Resource_SysBase.OprateSuccess; } else { data.success = false; data.msg = Resource_SysBase.OprateFail; } return data; } /// /// 更新一条数据 /// /// SysMenu /// [HttpPost] public async Task> Update([FromBody] SysMenu request) { var data = new MessageModel(); if (!await _sysMenuService.CheckMenuNameUnique(request)) { data.success = false; data.msg = "菜单名称已存在!"; return data; } else if (SysConst.YES_FRAME == request.IsFrame && !StringHelper.IsUrl(request.Path)) { data.success = false; data.msg = "地址必须以http(s)://开头!"; return data; } else if (request.MenuID == request.ParentID) { data.success = false; data.msg = "上级菜单不能选择自己!"; return data; } request.UpdateBy = CurrentUser.LoginName; request.ParentID = request.ParentID ?? 0; data.success = await _sysMenuService.UpdateMenu(request); if (data.success) { data.msg = Resource_SysBase.OprateSuccess; } else { data.msg = Resource_SysBase.OprateFail; } return data; } /// /// 删除一条数据 /// /// 主键 /// [HttpPost] public async Task> Del(int id) { var data = new MessageModel(); if (await _sysMenuService.HasChildByMenuId(id)) { data.success = false; data.msg = "存在子菜单,不允许删除!"; return data; } if ((await _sysMenuService.CheckMenuExistRole(id)) > 0) { data.success = false; data.msg = "菜单已分配,不允许删除!"; return data; } data.success = await _sysMenuService.DeleteMenuById(id); if (data.success) { data.msg = Resource_SysBase.OprateSuccess; } else { data.msg = Resource_SysBase.OprateFail; } return data; } /// /// 获取菜单下拉树列表 /// [HttpGet] public async Task>> Treeselect() { var menus = await _sysMenuService.SelectMenuList(CurrentUser.UserID); return Success(_sysMenuService.BuildMenuTreeSelect(menus)); } /// /// 加载对应角色菜单列表树 /// /// 角色ID /// [HttpGet] public async Task> RoleMenuTreeselect(int roleId) { var menus = await _sysMenuService.SelectMenuList(CurrentUser.UserID); var checkedKeys = await _sysMenuService.SelectMenuListByRoleId(roleId); var trees = _sysMenuService.BuildMenuTreeSelect(menus); return Success(new RoleMenuTree { CheckedKeys = checkedKeys, Menus = trees }); } } }