using Attribute; using Common; 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 { /// /// 角色信息 /// [Verify] public class SysRoleController : BaseController { private readonly ISysRoleService sysRoleService; private readonly ISysMenuService sysMenuService; public SysRoleController( ISysRoleService sysRoleService, ISysMenuService sysMenuService) { this.sysRoleService = sysRoleService; this.sysMenuService = sysMenuService; } /// /// 获取系统角色管理 /// /// [HttpGet("/v1/tel/sysServer/role/page")] public IActionResult List([FromQuery] SysRole role, [FromQuery] PagerInfo pager) { var list = sysRoleService.SelectRoleList(role, pager); return SUCCESS(list, TIME_FORMAT_FULL); } /// /// 获取所有角色列表 /// /// [HttpGet("/v1/tel/sysServer/role/list")] public IActionResult AllList() { return SUCCESS(sysRoleService.GetList(), TIME_FORMAT_FULL); } /// /// 根据角色编号获取详细信息 /// /// /// [HttpGet("{roleId}")] public IActionResult GetInfo(long roleId = 0) { var info = sysRoleService.SelectRoleById(roleId); return SUCCESS(info, TIME_FORMAT_FULL); } /// /// 添加角色 /// /// /// [HttpPost("/v1/tel/sysServer/role/add")] public IActionResult RoleAdd([FromBody] SysRoleDto dto) { SysRole sysRoleDto = dto.Adapt(); if (UserConstants.NOT_UNIQUE.Equals(sysRoleService.CheckRoleKeyUnique(sysRoleDto))) { return ToResponse(ApiResult.Error((int)ResultCode.CUSTOM_ERROR, $"新增角色'{sysRoleDto.roleName}'失败,角色权限已存在")); } TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext); sysRoleDto.CreateBy = loginUser.username; sysRoleDto.UpdateBy = loginUser.username; sysRoleDto.DelFlag = "0"; long roleId = sysRoleService.InsertRole(sysRoleDto); return SUCCESS(roleId); } /// /// 修改角色 /// /// /// [HttpPut("/v1/tel/sysServer/role/update")] public IActionResult RoleEdit([FromBody] SysRoleDto dto) { if (dto == null || dto.roleId <= 0) { return ToResponse(ApiResult.Error(101, "请求参数错误")); } SysRole sysRoleDto = dto.Adapt(); sysRoleService.CheckRoleAllowed(sysRoleDto); var info = sysRoleService.SelectRoleById(sysRoleDto.roleId); if (info != null && info.roleCode != sysRoleDto.roleCode) { if (UserConstants.NOT_UNIQUE.Equals(sysRoleService.CheckRoleKeyUnique(sysRoleDto))) { return ToResponse(ApiResult.Error($"编辑角色'{sysRoleDto.roleName}'失败,角色权限已存在")); } } TokenModel loginUser = JwtUtil.GetLoginUser(HttpContext); sysRoleDto.UpdateBy = loginUser.username; int upResult = sysRoleService.UpdateRole(sysRoleDto); if (upResult > 0) { return SUCCESS(upResult); } return SUCCESS(ApiResult.Error($"修改角色'{sysRoleDto.roleName}'失败,请联系管理员")); } /// /// 根据角色分配菜单 /// /// /// [HttpPut("/v1/tel/sysServer/role/menu")] public IActionResult DataScope([FromBody] SysRoleDto sysRoleDto) { if (sysRoleDto == null || sysRoleDto.roleId <= 0) return ToResponse(ApiResult.Error(101, "请求参数错误")); SysRole sysRole = sysRoleDto.Adapt(); sysRoleDto.createBy = HttpContext.GetName(); sysRoleService.CheckRoleAllowed(sysRole); bool result = sysRoleService.AuthDataScope(sysRoleDto); return SUCCESS(result); } /// /// 角色删除 /// /// /// [HttpDelete("/v1/tel/sysServer/role/removeById/{id}")] public IActionResult Remove(int id) { long[] roleIds = { id }; int result = sysRoleService.DeleteRoleByRoleId(roleIds); return SUCCESS(result); } /// /// 修改角色状态 /// /// 角色对象 /// [HttpPut("changeStatus")] [Log(Title = "修改角色状态", BusinessType = BusinessType.UPDATE)] [ActionPermissionFilter(Permission = "system:role:edit")] public IActionResult ChangeStatus([FromBody] SysRole roleDto) { sysRoleService.CheckRoleAllowed(roleDto); int result = sysRoleService.UpdateRoleStatus(roleDto); return ToResponse(result); } /// /// 角色导出 /// /// [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "角色导出")] [HttpGet("export")] [ActionPermissionFilter(Permission = "system:role:export")] public IActionResult Export() { var list = sysRoleService.SelectRoleAll(); string sFileName = ExportExcel(list, "sysrole", "角色"); return SUCCESS(new { path = "/export/" + sFileName, fileName = sFileName }); } /// /// 导出角色菜单 /// /// /// [Log(BusinessType = BusinessType.EXPORT, IsSaveResponseData = false, Title = "角色菜单导出")] [HttpGet("exportRoleMenu")] [ActionPermissionFilter(Permission = "system:role:export")] public IActionResult ExportRoleMenu(int roleId) { MenuQueryDto dto = new() { delFlag = "0", menuTypeIds = "M,C,F" }; var list = sysMenuService.SelectRoleMenuListByRole(dto, roleId); var result = ExportExcelMini(list, roleId.ToString(), "角色菜单"); return ExportExcel(result.Item2, result.Item1); } } }