SysRoleController.java 6.8 KB

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