123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224 |
- 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
- {
- /// <summary>
- /// 系统菜单
- /// </summary>
- [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;
- }
- /// <summary>
- /// 获取菜单树列表
- /// </summary>
- /// <returns></returns>
- [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");
- }
- /// <summary>
- /// 获取菜单列表
- /// </summary>
- /// <returns></returns>
- [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");
- }
- /// <summary>
- /// 根据菜单编号获取详细信息
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- [HttpGet("{menuId}")]
- [ActionPermissionFilter(Permission = "system:menu:query")]
- public IActionResult GetMenuInfo(int menuId = 0)
- {
- return SUCCESS(sysMenuService.GetMenuByMenuId(menuId), "yyyy-MM-dd HH:mm:ss");
- }
- /// <summary>
- /// 根据菜单编号获取菜单列表,菜单管理首次进入
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- [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");
- }
- /// <summary>
- /// 获取角色菜单信息
- /// 加载对应角色菜单列表树
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- [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));
- }
- /// <summary>
- /// 修改菜单
- /// </summary>
- /// <param name="menuDto"></param>
- /// <returns></returns>
- [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<SysMenu, MenuDto>()
- .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);//忽略字段名称的大小写;//忽略除以上配置的所有字段
- var modal = menuDto.Adapt<SysMenu>(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);
- }
- /// <summary>
- /// 添加菜单
- /// </summary>
- /// <param name="menuDto"></param>
- /// <returns></returns>
- [HttpPost("/v1/tel/sysServer/menu/add")]
- public IActionResult MenuAdd([FromBody] MenuDto menuDto)
- {
- var config = new TypeAdapterConfig();
- //映射规则
- config.ForType<SysMenu, MenuDto>()
- .NameMatchingStrategy(NameMatchingStrategy.IgnoreCase);
- var menu = menuDto.Adapt<SysMenu>(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);
- }
- /// <summary>
- /// 菜单删除
- /// </summary>
- /// <param name="menuId"></param>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 保存排序
- /// </summary>
- /// <param name="id"></param>
- /// <param name="value"></param>
- /// <returns></returns>
- [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);
- }
- }
- }
|