TopSysRoleController.java 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package com.zhongzheng.controller.top;
  2. import cn.hutool.core.lang.Validator;
  3. import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
  4. import com.zhongzheng.common.annotation.Log;
  5. import com.zhongzheng.common.constant.UserConstants;
  6. import com.zhongzheng.common.core.controller.BaseController;
  7. import com.zhongzheng.common.core.domain.AjaxResult;
  8. import com.zhongzheng.common.core.domain.model.TopLoginUser;
  9. import com.zhongzheng.common.core.page.TableDataInfo;
  10. import com.zhongzheng.common.enums.BusinessType;
  11. import com.zhongzheng.common.utils.SecurityUtils;
  12. import com.zhongzheng.common.utils.ServletUtils;
  13. import com.zhongzheng.common.utils.poi.ExcelUtil;
  14. import com.zhongzheng.framework.web.service.TokenService;
  15. import com.zhongzheng.framework.web.service.TopSysPermissionService;
  16. import com.zhongzheng.modules.top.domain.TopSysRole;
  17. import com.zhongzheng.modules.top.service.ITopSysRoleService;
  18. import com.zhongzheng.modules.top.service.ITopSysUserService;
  19. import io.swagger.annotations.Api;
  20. import io.swagger.annotations.ApiOperation;
  21. import lombok.RequiredArgsConstructor;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.security.access.prepost.PreAuthorize;
  24. import org.springframework.validation.annotation.Validated;
  25. import org.springframework.web.bind.annotation.*;
  26. import java.util.List;
  27. /**
  28. * 角色信息Controller
  29. *
  30. * @author ruoyi
  31. * @date 2023-03-10
  32. */
  33. @Api(value = "角色信息控制器", tags = {"角色信息管理"})
  34. @RequiredArgsConstructor(onConstructor_ = @Autowired)
  35. @RestController
  36. @RequestMapping("/system/top/role")
  37. public class TopSysRoleController extends BaseController {
  38. @Autowired
  39. private ITopSysRoleService topSysRoleService;
  40. @Autowired
  41. private TokenService tokenService;
  42. @Autowired
  43. private TopSysPermissionService topSysPermissionService;
  44. @Autowired
  45. private ITopSysUserService topSysUserService;
  46. @ApiOperation("角色列表")
  47. @PreAuthorize("@ss.hasPermi('system:role:list')")
  48. @GetMapping("/list")
  49. public TableDataInfo list(TopSysRole role)
  50. {
  51. startPage();
  52. List<TopSysRole> list = topSysRoleService.selectRoleList(role);
  53. return getDataTable(list);
  54. }
  55. @Log(title = "角色管理", businessType = BusinessType.EXPORT)
  56. @PreAuthorize("@ss.hasPermi('system:role:export')")
  57. @GetMapping("/export")
  58. public AjaxResult export(TopSysRole role)
  59. {
  60. List<TopSysRole> list = topSysRoleService.selectRoleList(role);
  61. ExcelUtil<TopSysRole> util = new ExcelUtil<TopSysRole>(TopSysRole.class);
  62. return util.exportExcel(list, "角色数据");
  63. }
  64. /**
  65. * 根据角色编号获取详细信息
  66. */
  67. @ApiOperation("根据角色编号获取详细信息")
  68. @PreAuthorize("@ss.hasPermi('system:role:query')")
  69. @GetMapping(value = "/{roleId}")
  70. public AjaxResult getInfo(@PathVariable Long roleId)
  71. {
  72. return AjaxResult.success(topSysRoleService.selectRoleById(roleId));
  73. }
  74. /**
  75. * 新增角色
  76. */
  77. @ApiOperation("新增角色")
  78. @ApiOperationSupport(ignoreParameters = {"createBy","createTime","dataScope","delFlag","updateTime"
  79. ,"flag","params","roleId","updateBy","deptIds","deptCheckStrictly","menuCheckStrictly"})
  80. @PreAuthorize("@ss.hasPermi('system:role:add')")
  81. @Log(title = "角色管理", businessType = BusinessType.INSERT)
  82. @PostMapping
  83. public AjaxResult add(@Validated @RequestBody TopSysRole role)
  84. {
  85. if (UserConstants.NOT_UNIQUE.equals(topSysRoleService.checkRoleNameUnique(role)))
  86. {
  87. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色名称已存在");
  88. }
  89. else if (UserConstants.NOT_UNIQUE.equals(topSysRoleService.checkRoleKeyUnique(role)))
  90. {
  91. return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败,角色权限已存在");
  92. }
  93. role.setCreateBy(SecurityUtils.getUsername());
  94. return toAjax(topSysRoleService.insertRole(role));
  95. }
  96. /**
  97. * 修改保存角色
  98. */
  99. @ApiOperation("修改保存角色")
  100. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  101. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  102. @PostMapping("/edit")
  103. public AjaxResult edit(@Validated @RequestBody TopSysRole role)
  104. {
  105. topSysRoleService.checkRoleAllowed(role);
  106. if (UserConstants.NOT_UNIQUE.equals(topSysRoleService.checkRoleNameUnique(role)))
  107. {
  108. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色名称已存在");
  109. }
  110. else if (UserConstants.NOT_UNIQUE.equals(topSysRoleService.checkRoleKeyUnique(role)))
  111. {
  112. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,角色权限已存在");
  113. }
  114. role.setUpdateBy(SecurityUtils.getUsername());
  115. if (topSysRoleService.updateRole(role) > 0)
  116. {
  117. // 更新缓存用户权限
  118. TopLoginUser loginUser = tokenService.getTopLoginUser(ServletUtils.getRequest());
  119. if (Validator.isNotNull(loginUser.getUser()) && !loginUser.getUser().isAdmin())
  120. {
  121. loginUser.setPermissions(topSysPermissionService.getMenuPermission(loginUser.getUser()));
  122. loginUser.setUser(topSysUserService.selectUserByUserName(loginUser.getUser().getUserName()));
  123. tokenService.setTopLoginUser(loginUser);
  124. }
  125. return AjaxResult.success();
  126. }
  127. return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败,请联系管理员");
  128. }
  129. /**
  130. * 修改保存数据权限
  131. */
  132. @ApiOperation("修改保存数据权限")
  133. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  134. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  135. @PostMapping("/dataScope")
  136. public AjaxResult dataScope(@RequestBody TopSysRole role)
  137. {
  138. topSysRoleService.checkRoleAllowed(role);
  139. return toAjax(topSysRoleService.authDataScope(role));
  140. }
  141. /**
  142. * 状态修改
  143. */
  144. @ApiOperation("状态修改")
  145. @PreAuthorize("@ss.hasPermi('system:role:edit')")
  146. @Log(title = "角色管理", businessType = BusinessType.UPDATE)
  147. @PostMapping("/changeStatus")
  148. public AjaxResult changeStatus(@RequestBody TopSysRole role)
  149. {
  150. topSysRoleService.checkRoleAllowed(role);
  151. role.setUpdateBy(SecurityUtils.getUsername());
  152. return toAjax(topSysRoleService.updateRoleStatus(role));
  153. }
  154. /**
  155. * 删除角色
  156. */
  157. @ApiOperation("删除角色")
  158. @PreAuthorize("@ss.hasPermi('system:role:remove')")
  159. @Log(title = "角色管理", businessType = BusinessType.DELETE)
  160. @PostMapping("/delete/{roleIds}")
  161. public AjaxResult remove(@PathVariable Long[] roleIds)
  162. {
  163. return toAjax(topSysRoleService.deleteRoleByIds(roleIds));
  164. }
  165. /**
  166. * 获取角色选择框列表
  167. */
  168. @ApiOperation("获取角色选择框列表")
  169. @PreAuthorize("@ss.hasPermi('system:role:query')")
  170. @GetMapping("/optionselect")
  171. public AjaxResult optionselect()
  172. {
  173. return AjaxResult.success(topSysRoleService.selectRoleAll());
  174. }
  175. }