123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- 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
- {
- /// <summary>
- /// 角色信息
- /// </summary>
- [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;
- }
- /// <summary>
- /// 获取系统角色管理
- /// </summary>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 获取所有角色列表
- /// </summary>
- /// <returns></returns>
- [HttpGet("/v1/tel/sysServer/role/list")]
- public IActionResult AllList()
- {
- return SUCCESS(sysRoleService.GetList(), TIME_FORMAT_FULL);
- }
- /// <summary>
- /// 根据角色编号获取详细信息
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- [HttpGet("{roleId}")]
- public IActionResult GetInfo(long roleId = 0)
- {
- var info = sysRoleService.SelectRoleById(roleId);
- return SUCCESS(info, TIME_FORMAT_FULL);
- }
- /// <summary>
- /// 添加角色
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [HttpPost("/v1/tel/sysServer/role/add")]
- public IActionResult RoleAdd([FromBody] SysRoleDto dto)
- {
- SysRole sysRoleDto = dto.Adapt<SysRole>();
- 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);
- }
- /// <summary>
- /// 修改角色
- /// </summary>
- /// <param name="dto"></param>
- /// <returns></returns>
- [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<SysRole>();
- 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}'失败,请联系管理员"));
- }
- /// <summary>
- /// 根据角色分配菜单
- /// </summary>
- /// <param name="sysRoleDto"></param>
- /// <returns></returns>
- [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<SysRole>();
- sysRoleDto.createBy = HttpContext.GetName();
- sysRoleService.CheckRoleAllowed(sysRole);
- bool result = sysRoleService.AuthDataScope(sysRoleDto);
- return SUCCESS(result);
- }
- /// <summary>
- /// 角色删除
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- [HttpDelete("/v1/tel/sysServer/role/removeById/{id}")]
- public IActionResult Remove(int id)
- {
- long[] roleIds = { id };
- int result = sysRoleService.DeleteRoleByRoleId(roleIds);
- return SUCCESS(result);
- }
- /// <summary>
- /// 修改角色状态
- /// </summary>
- /// <param name="roleDto">角色对象</param>
- /// <returns></returns>
- [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);
- }
- /// <summary>
- /// 角色导出
- /// </summary>
- /// <returns></returns>
- [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 });
- }
- /// <summary>
- /// 导出角色菜单
- /// </summary>
- /// <param name="roleId"></param>
- /// <returns></returns>
- [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);
- }
- }
- }
|