using Attribute;
using Dto;
using Enums;
using Extensions;
using Filters;
using Infrastructure;
using Infrastructure.Model;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Middleware;
using Model;
using Model.Base;
using Services;
using Util;
namespace Controllers
{
///
/// 系统菜单
///
[ApiExplorerSettings(GroupName = "sys")]
public class SysMenuController : BaseController
{
private readonly ISysRoleService sysRoleService;
private readonly ISysMenuService sysMenuService;
private readonly ISysRoleMenuService sysRoleMenuService;
public SysMenuController(
ISysRoleService sysRoleService,
ISysMenuService sysMenuService,
ISysRoleMenuService sysRoleMenuService)
{
this.sysRoleService = sysRoleService;
this.sysMenuService = sysMenuService;
this.sysRoleMenuService = sysRoleMenuService;
}
///
/// 获取菜单树列表
///
///
[HttpGet("/v1/tel/sysServer/menu/tree")]
public IActionResult TreeMenuList([FromQuery] MenuQueryDto menu)
{
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
long userId = loginUser.userId;
return SUCCESS(sysMenuService.SelectTreeMenuList(menu, userId), "yyyy-MM-dd HH:mm:ss");
}
///
/// 获取菜单列表
///
///
[ActionPermissionFilter(Permission = "system:menu:list")]
[HttpGet("list")]
public IActionResult MenuList([FromQuery] MenuQueryDto menu)
{
long userId = HttpContext.GetUId();
return SUCCESS(sysMenuService.SelectMenuList(menu, userId), "yyyy-MM-dd HH:mm:ss");
}
///
/// 根据菜单编号获取详细信息
///
///
///
[HttpGet("{menuId}")]
[ActionPermissionFilter(Permission = "system:menu:query")]
public IActionResult GetMenuInfo(int menuId = 0)
{
return SUCCESS(sysMenuService.GetMenuByMenuId(menuId), "yyyy-MM-dd HH:mm:ss");
}
///
/// 根据菜单编号获取菜单列表,菜单管理首次进入
///
///
///
[HttpGet("list/{menuId}")]
[ActionPermissionFilter(Permission = "system:menu:query")]
public IActionResult GetMenuList(int menuId = 0)
{
long userId = HttpContext.GetUId();
return SUCCESS(sysMenuService.GetMenusByMenuId(menuId, userId), "yyyy-MM-dd HH:mm:ss");
}
///
/// 获取角色菜单信息
/// 加载对应角色菜单列表树
///
///
///
[HttpGet("/v1/tel/sysServer/menu/getRoleTree")]
public IActionResult RoleMenuTreeselect([FromQuery] RoleMenuQueryDto menu)
{
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
var menus = sysMenuService.SelectMenuList(new MenuQueryDto(), loginUser.userId);
var setCheckedKeys = sysRoleService.SelectUserRoleMenus(menu.roleId);
return SUCCESS(new
{
setCheckedKeys,
menus = sysMenuService.BuildMenuTreeSelect(menus),
});
}
[Verify]
[HttpGet("/v1/tel/sysServer/menu/getUserMenu")]
public IActionResult GetMenus()
{
long userId = HttpContext.GetUId();
var menus = sysMenuService.SelectMenuList(new MenuQueryDto(), userId);
return SUCCESS(sysMenuService.BuildLeftMenuTreeSelect(menus));
}
///
/// 修改菜单
///
///
///
[HttpPut("/v1/tel/sysServer/menu/update")]
public IActionResult MenuEdit([FromBody] MenuDto menuDto)
{
if (menuDto == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
//if (UserConstants.NOT_UNIQUE.Equals(sysMenuService.CheckMenuNameUnique(MenuDto)))
//{
// return ToResponse(ApiResult.Error($"修改菜单'{MenuDto.menuName}'失败,菜单名称已存在"));
//}
var config = new TypeAdapterConfig();
//映射规则
config.ForType()
.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);//忽略字段名称的大小写;//忽略除以上配置的所有字段
var modal = menuDto.Adapt(config).ToUpdate(HttpContext);
if (modal.menuType == "3" && !modal.path.StartsWith("http"))
{
return ToResponse(ApiResult.Error($"修改菜单'{modal.name}'失败,地址必须以http(s)://开头"));
}
if (modal.menuId.Equals(modal.parentId))
{
return ToResponse(ApiResult.Error($"修改菜单'{modal.name}'失败,上级菜单不能选择自己"));
}
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
modal.UpdateBy = loginUser.username;
long result = sysMenuService.EditMenu(modal);
return SUCCESS(result);
}
///
/// 添加菜单
///
///
///
[HttpPost("/v1/tel/sysServer/menu/add")]
public IActionResult MenuAdd([FromBody] MenuDto menuDto)
{
var config = new TypeAdapterConfig();
//映射规则
config.ForType()
.NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);
var menu = menuDto.Adapt(config).ToCreate(HttpContext);
if (menu == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
if (UserConstants.NOT_UNIQUE.Equals(sysMenuService.CheckMenuNameUnique(menu)))
{
return ToResponse(ApiResult.Error($"新增菜单'{menu.name}'失败,菜单名称已存在"));
}
if (menu.menuType == "3" && !menu.path.StartsWith("http"))
{
return ToResponse(ApiResult.Error($"新增菜单'{menu.name}'失败,地址必须以http(s)://开头"));
}
TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext);
menu.CreateBy = loginUser.username;
menu.UpdateBy = loginUser.username;
menu.DelFlag = "0";
long result = sysMenuService.AddMenu(menu);
return SUCCESS(result);
}
///
/// 菜单删除
///
///
///
[HttpDelete("/v1/tel/sysServer/menu/removeById/{id}")]
public IActionResult Remove(int id)
{
if (sysMenuService.HasChildByMenuId(id))
{
return ToResponse(ResultCode.CUSTOM_ERROR, "存在子菜单,不允许删除");
}
if (sysRoleMenuService.CheckMenuExistRole(id))
{
return ToResponse(ResultCode.CUSTOM_ERROR, "菜单已分配,不允许删除");
}
int result = sysMenuService.DeleteMenuById(id);
return SUCCESS(result);
}
///
/// 保存排序
///
///
///
///
[ActionPermissionFilter(Permission = "system:menu:update")]
[HttpGet("ChangeSort")]
[Log(Title = "保存排序", BusinessType = BusinessType.UPDATE)]
public IActionResult ChangeSort(int id = 0, int value = 0)
{
MenuDto MenuDto = new()
{
menuId = id,
sortOrder = value
};
if (MenuDto == null) { return ToResponse(ApiResult.Error(101, "请求参数错误")); }
int result = sysMenuService.ChangeSortMenu(MenuDto);
return ToResponse(result);
}
}
}