You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

225 lines
7.0 KiB
C#

1 year ago
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
{
/// <summary>
/// SysMenuController
/// </summary>
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
public class SysMenuController : BaseApiUserController
{
/// <summary>
/// _sysMenuService
/// </summary>
private readonly ISysMenuService _sysMenuService;
private readonly IMapper _mapper;
/// <summary>
/// 构造方法
/// </summary>
/// <param name="SysMenuService"></param>
/// <param name="mapper"></param>
/// <param name="sysUserService"></param>
public SysMenuController(ISysMenuService SysMenuService, IMapper mapper, ISysUserService sysUserService) : base(sysUserService)
{
_sysUserService = sysUserService;
_sysMenuService = SysMenuService;
_mapper = mapper;
}
/// <summary>
/// 分页查询
/// </summary>
/// <param name="pageQuery"></param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<List<MenuView>>> GetByPage([FromBody] PageQuery<SysMenu> pageQuery)
{
if (!pageQuery.Query.IsNotEmptyOrNull())
{
pageQuery.Query = new SysMenu();
}
var model = await _sysMenuService.SelectMenuList(CurrentUser.UserID, pageQuery);
var r = _mapper.Map<List<MenuView>>(model);
return Success(r);
}
/// <summary>
/// 根据ID查询
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<SysMenu>> GetByID(int id)
{
return new MessageModel<SysMenu>()
{
msg = Resource_SysBase.OprateSuccess,
success = true,
data = await _sysMenuService.QueryByIdAsync(id)
};
}
/// <summary>
/// 新增一条数据
/// </summary>
/// <param name="request">SysMenu</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<int>> Add([FromBody] SysMenu request)
{
var data = new MessageModel<int>();
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;
}
/// <summary>
/// 更新一条数据
/// </summary>
/// <param name="request">SysMenu</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Update([FromBody] SysMenu request)
{
var data = new MessageModel<bool>();
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;
}
/// <summary>
/// 删除一条数据
/// </summary>
/// <param name="id">主键</param>
/// <returns></returns>
[HttpPost]
public async Task<MessageModel<bool>> Del(int id)
{
var data = new MessageModel<bool>();
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;
}
/// <summary>
/// 获取菜单下拉树列表
/// </summary>
[HttpGet]
public async Task<MessageModel<List<TreeSelect>>> Treeselect()
{
var menus = await _sysMenuService.SelectMenuList(CurrentUser.UserID);
return Success(_sysMenuService.BuildMenuTreeSelect(menus));
}
/// <summary>
/// 加载对应角色菜单列表树
/// </summary>
/// <param name="roleId">角色ID</param>
/// <returns></returns>
[HttpGet]
public async Task<MessageModel<RoleMenuTree>> 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
});
}
}
}