SysUserController.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. package com.ruoyi.web.controller.system;
  2. import java.util.List;
  3. import java.util.stream.Collectors;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.apache.commons.lang3.ArrayUtils;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.security.access.prepost.PreAuthorize;
  8. import org.springframework.validation.annotation.Validated;
  9. import org.springframework.web.bind.annotation.DeleteMapping;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.PutMapping;
  14. import org.springframework.web.bind.annotation.RequestBody;
  15. import org.springframework.web.bind.annotation.RequestMapping;
  16. import org.springframework.web.bind.annotation.RestController;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import com.ruoyi.common.annotation.Log;
  19. import com.ruoyi.common.constant.UserConstants;
  20. import com.ruoyi.common.core.controller.BaseController;
  21. import com.ruoyi.common.core.domain.AjaxResult;
  22. import com.ruoyi.common.core.domain.entity.SysDept;
  23. import com.ruoyi.common.core.domain.entity.SysRole;
  24. import com.ruoyi.common.core.domain.entity.SysUser;
  25. import com.ruoyi.common.core.page.TableDataInfo;
  26. import com.ruoyi.common.enums.BusinessType;
  27. import com.ruoyi.common.utils.SecurityUtils;
  28. import com.ruoyi.common.utils.StringUtils;
  29. import com.ruoyi.common.utils.poi.ExcelUtil;
  30. import com.ruoyi.system.service.ISysDeptService;
  31. import com.ruoyi.system.service.ISysPostService;
  32. import com.ruoyi.system.service.ISysRoleService;
  33. import com.ruoyi.system.service.ISysUserService;
  34. /**
  35. * 用户信息
  36. *
  37. * @author ruoyi
  38. */
  39. @RestController
  40. @RequestMapping("/system/user")
  41. public class SysUserController extends BaseController
  42. {
  43. @Autowired
  44. private ISysUserService userService;
  45. @Autowired
  46. private ISysRoleService roleService;
  47. @Autowired
  48. private ISysDeptService deptService;
  49. @Autowired
  50. private ISysPostService postService;
  51. /**
  52. * 获取用户列表
  53. */
  54. @PreAuthorize("@ss.hasPermi('system:user:list')")
  55. @GetMapping("/list")
  56. public TableDataInfo list(SysUser user)
  57. {
  58. startPage();
  59. List<SysUser> list = userService.selectUserList(user);
  60. return getDataTable(list);
  61. }
  62. @Log(title = "用户管理", businessType = BusinessType.EXPORT)
  63. @PreAuthorize("@ss.hasPermi('system:user:export')")
  64. @PostMapping("/export")
  65. public void export(HttpServletResponse response, SysUser user)
  66. {
  67. List<SysUser> list = userService.selectUserList(user);
  68. ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
  69. util.exportExcel(response, list, "用户数据");
  70. }
  71. @Log(title = "用户管理", businessType = BusinessType.IMPORT)
  72. @PreAuthorize("@ss.hasPermi('system:user:import')")
  73. @PostMapping("/importData")
  74. public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
  75. {
  76. ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
  77. List<SysUser> userList = util.importExcel(file.getInputStream());
  78. String operName = getUsername();
  79. String message = userService.importUser(userList, updateSupport, operName);
  80. return AjaxResult.success(message);
  81. }
  82. @PostMapping("/importTemplate")
  83. public void importTemplate(HttpServletResponse response)
  84. {
  85. ExcelUtil<SysUser> util = new ExcelUtil<SysUser>(SysUser.class);
  86. util.importTemplateExcel(response, "用户数据");
  87. }
  88. /**
  89. * 根据用户编号获取详细信息
  90. */
  91. @PreAuthorize("@ss.hasPermi('system:user:query')")
  92. @GetMapping(value = { "/", "/{userId}" })
  93. public AjaxResult getInfo(@PathVariable(value = "userId", required = false) Long userId)
  94. {
  95. userService.checkUserDataScope(userId);
  96. AjaxResult ajax = AjaxResult.success();
  97. List<SysRole> roles = roleService.selectRoleAll();
  98. ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
  99. ajax.put("posts", postService.selectPostAll());
  100. if (StringUtils.isNotNull(userId))
  101. {
  102. SysUser sysUser = userService.selectUserById(userId);
  103. ajax.put(AjaxResult.DATA_TAG, sysUser);
  104. ajax.put("postIds", postService.selectPostListByUserId(userId));
  105. ajax.put("roleIds", sysUser.getRoles().stream().map(SysRole::getRoleId).collect(Collectors.toList()));
  106. }
  107. return ajax;
  108. }
  109. /**
  110. * 新增用户
  111. */
  112. @PreAuthorize("@ss.hasPermi('system:user:add')")
  113. @Log(title = "用户管理", businessType = BusinessType.INSERT)
  114. @PostMapping
  115. public AjaxResult add(@Validated @RequestBody SysUser user)
  116. {
  117. if (UserConstants.NOT_UNIQUE.equals(userService.checkUserNameUnique(user.getUserName())))
  118. {
  119. return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
  120. }
  121. else if (StringUtils.isNotEmpty(user.getPhonenumber())
  122. && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
  123. {
  124. return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
  125. }
  126. else if (StringUtils.isNotEmpty(user.getEmail())
  127. && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
  128. {
  129. return AjaxResult.error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  130. }
  131. user.setCreateBy(getUsername());
  132. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  133. return toAjax(userService.insertUser(user));
  134. }
  135. /**
  136. * 修改用户
  137. */
  138. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  139. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  140. @PutMapping
  141. public AjaxResult edit(@Validated @RequestBody SysUser user)
  142. {
  143. userService.checkUserAllowed(user);
  144. userService.checkUserDataScope(user.getUserId());
  145. if (StringUtils.isNotEmpty(user.getPhonenumber())
  146. && UserConstants.NOT_UNIQUE.equals(userService.checkPhoneUnique(user)))
  147. {
  148. return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
  149. }
  150. else if (StringUtils.isNotEmpty(user.getEmail())
  151. && UserConstants.NOT_UNIQUE.equals(userService.checkEmailUnique(user)))
  152. {
  153. return AjaxResult.error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  154. }
  155. user.setUpdateBy(getUsername());
  156. return toAjax(userService.updateUser(user));
  157. }
  158. /**
  159. * 删除用户
  160. */
  161. @PreAuthorize("@ss.hasPermi('system:user:remove')")
  162. @Log(title = "用户管理", businessType = BusinessType.DELETE)
  163. @DeleteMapping("/{userIds}")
  164. public AjaxResult remove(@PathVariable Long[] userIds)
  165. {
  166. if (ArrayUtils.contains(userIds, getUserId()))
  167. {
  168. return error("当前用户不能删除");
  169. }
  170. return toAjax(userService.deleteUserByIds(userIds));
  171. }
  172. /**
  173. * 重置密码
  174. */
  175. @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
  176. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  177. @PutMapping("/resetPwd")
  178. public AjaxResult resetPwd(@RequestBody SysUser user)
  179. {
  180. userService.checkUserAllowed(user);
  181. userService.checkUserDataScope(user.getUserId());
  182. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  183. user.setUpdateBy(getUsername());
  184. return toAjax(userService.resetPwd(user));
  185. }
  186. /**
  187. * 状态修改
  188. */
  189. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  190. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  191. @PutMapping("/changeStatus")
  192. public AjaxResult changeStatus(@RequestBody SysUser user)
  193. {
  194. userService.checkUserAllowed(user);
  195. userService.checkUserDataScope(user.getUserId());
  196. user.setUpdateBy(getUsername());
  197. return toAjax(userService.updateUserStatus(user));
  198. }
  199. /**
  200. * 根据用户编号获取授权角色
  201. */
  202. @PreAuthorize("@ss.hasPermi('system:user:query')")
  203. @GetMapping("/authRole/{userId}")
  204. public AjaxResult authRole(@PathVariable("userId") Long userId)
  205. {
  206. AjaxResult ajax = AjaxResult.success();
  207. SysUser user = userService.selectUserById(userId);
  208. List<SysRole> roles = roleService.selectRolesByUserId(userId);
  209. ajax.put("user", user);
  210. ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
  211. return ajax;
  212. }
  213. /**
  214. * 用户授权角色
  215. */
  216. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  217. @Log(title = "用户管理", businessType = BusinessType.GRANT)
  218. @PutMapping("/authRole")
  219. public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
  220. {
  221. userService.checkUserDataScope(userId);
  222. userService.insertUserAuth(userId, roleIds);
  223. return success();
  224. }
  225. /**
  226. * 获取部门树列表
  227. */
  228. @PreAuthorize("@ss.hasPermi('system:user:list')")
  229. @GetMapping("/deptTree")
  230. public AjaxResult deptTree(SysDept dept)
  231. {
  232. return AjaxResult.success(deptService.selectDeptTreeList(dept));
  233. }
  234. }