SysUserController.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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.core.controller.BaseController;
  20. import com.ruoyi.common.core.domain.AjaxResult;
  21. import com.ruoyi.common.core.domain.entity.SysDept;
  22. import com.ruoyi.common.core.domain.entity.SysRole;
  23. import com.ruoyi.common.core.domain.entity.SysUser;
  24. import com.ruoyi.common.core.page.TableDataInfo;
  25. import com.ruoyi.common.enums.BusinessType;
  26. import com.ruoyi.common.utils.SecurityUtils;
  27. import com.ruoyi.common.utils.StringUtils;
  28. import com.ruoyi.common.utils.poi.ExcelUtil;
  29. import com.ruoyi.system.service.ISysDeptService;
  30. import com.ruoyi.system.service.ISysPostService;
  31. import com.ruoyi.system.service.ISysRoleService;
  32. import com.ruoyi.system.service.ISysUserService;
  33. /**
  34. * 用户信息
  35. *
  36. * @author ruoyi
  37. */
  38. @RestController
  39. @RequestMapping("/system/user")
  40. public class SysUserController extends BaseController
  41. {
  42. @Autowired
  43. private ISysUserService userService;
  44. @Autowired
  45. private ISysRoleService roleService;
  46. @Autowired
  47. private ISysDeptService deptService;
  48. @Autowired
  49. private ISysPostService postService;
  50. /**
  51. * 获取用户列表
  52. */
  53. @PreAuthorize("@ss.hasPermi('system:user:list')")
  54. @GetMapping("/list")
  55. public TableDataInfo list(SysUser user)
  56. {
  57. startPage();
  58. List<SysUser> list = userService.selectUserList(user);
  59. return getDataTable(list);
  60. }
  61. @Log(title = "用户管理", businessType = BusinessType.EXPORT)
  62. @PreAuthorize("@ss.hasPermi('system:user:export')")
  63. @PostMapping("/export")
  64. public void export(HttpServletResponse response, SysUser user)
  65. {
  66. user.setPageSize(999999);
  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 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 (!userService.checkUserNameUnique(user))
  118. {
  119. return error("新增用户'" + user.getUserName() + "'失败,登录账号已存在");
  120. }
  121. if ( userService.checkJobNumber(user))
  122. {
  123. return error("新增用户,工号'" + user.getJobNumber() + "'失败,工号已存在");
  124. }
  125. // else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
  126. // {
  127. // return error("新增用户'" + user.getUserName() + "'失败,手机号码已存在");
  128. // }
  129. // else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
  130. // {
  131. // return error("新增用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  132. // }
  133. user.setCreateBy(getUsername());
  134. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  135. return toAjax(userService.insertUser(user));
  136. }
  137. public static void main(String[] args) {
  138. String s = SecurityUtils.encryptPassword("admin123");
  139. System.out.println(s);
  140. }
  141. /**
  142. * 查询用户编号
  143. */
  144. @GetMapping("/checkJobNumber")
  145. public AjaxResult checkJobNumber( @RequestBody SysUser user) {
  146. if (userService.checkJobNumber(user)) {
  147. return error("工号'" + user.getJobNumber() + "'已存在");
  148. }
  149. return success(1);
  150. }
  151. /**
  152. * 修改用户
  153. */
  154. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  155. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  156. @PutMapping
  157. public AjaxResult edit(@Validated @RequestBody SysUser user)
  158. {
  159. userService.checkUserAllowed(user);
  160. userService.checkUserDataScope(user.getUserId());
  161. if (!userService.checkUserNameUnique(user))
  162. {
  163. return error("修改用户'" + user.getUserName() + "'失败,登录账号已存在");
  164. }
  165. if (userService.checkJobNumber(user))
  166. {
  167. return error("修改用户,工号'" + user.getJobNumber() + "'失败,工号已存在");
  168. }
  169. // else if (StringUtils.isNotEmpty(user.getPhonenumber()) && !userService.checkPhoneUnique(user))
  170. // {
  171. // return error("修改用户'" + user.getUserName() + "'失败,手机号码已存在");
  172. // }
  173. // else if (StringUtils.isNotEmpty(user.getEmail()) && !userService.checkEmailUnique(user))
  174. // {
  175. // return error("修改用户'" + user.getUserName() + "'失败,邮箱账号已存在");
  176. // }
  177. user.setUpdateBy(getUsername());
  178. return toAjax(userService.updateUser(user));
  179. }
  180. /**
  181. * 删除用户
  182. */
  183. @PreAuthorize("@ss.hasPermi('system:user:remove')")
  184. @Log(title = "用户管理", businessType = BusinessType.DELETE)
  185. @DeleteMapping("/{userIds}")
  186. public AjaxResult remove(@PathVariable Long[] userIds)
  187. {
  188. if (ArrayUtils.contains(userIds, getUserId()))
  189. {
  190. return error("当前用户不能删除");
  191. }
  192. if (userService.checkProject(userIds)){
  193. return error("当前用户存在项目不能删除");
  194. }
  195. return toAjax(userService.deleteUserByIds(userIds));
  196. }
  197. /**
  198. * 重置密码
  199. */
  200. @PreAuthorize("@ss.hasPermi('system:user:resetPwd')")
  201. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  202. @PutMapping("/resetPwd")
  203. public AjaxResult resetPwd(@RequestBody SysUser user)
  204. {
  205. userService.checkUserAllowed(user);
  206. userService.checkUserDataScope(user.getUserId());
  207. user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
  208. user.setUpdateBy(getUsername());
  209. return toAjax(userService.resetPwd(user));
  210. }
  211. /**
  212. * 状态修改
  213. */
  214. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  215. @Log(title = "用户管理", businessType = BusinessType.UPDATE)
  216. @PutMapping("/changeStatus")
  217. public AjaxResult changeStatus(@RequestBody SysUser user)
  218. {
  219. userService.checkUserAllowed(user);
  220. userService.checkUserDataScope(user.getUserId());
  221. user.setUpdateBy(getUsername());
  222. return toAjax(userService.updateUserStatus(user));
  223. }
  224. /**
  225. * 根据用户编号获取授权角色
  226. */
  227. @PreAuthorize("@ss.hasPermi('system:user:query')")
  228. @GetMapping("/authRole/{userId}")
  229. public AjaxResult authRole(@PathVariable("userId") Long userId)
  230. {
  231. AjaxResult ajax = AjaxResult.success();
  232. SysUser user = userService.selectUserById(userId);
  233. List<SysRole> roles = roleService.selectRolesByUserId(userId);
  234. ajax.put("user", user);
  235. ajax.put("roles", SysUser.isAdmin(userId) ? roles : roles.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList()));
  236. return ajax;
  237. }
  238. /**
  239. * 用户授权角色
  240. */
  241. @PreAuthorize("@ss.hasPermi('system:user:edit')")
  242. @Log(title = "用户管理", businessType = BusinessType.GRANT)
  243. @PutMapping("/authRole")
  244. public AjaxResult insertAuthRole(Long userId, Long[] roleIds)
  245. {
  246. userService.checkUserDataScope(userId);
  247. userService.insertUserAuth(userId, roleIds);
  248. return success();
  249. }
  250. /**
  251. * 获取部门树列表
  252. */
  253. @PreAuthorize("@ss.hasPermi('system:user:list')")
  254. @GetMapping("/deptTree")
  255. public AjaxResult deptTree(SysDept dept)
  256. {
  257. return success(deptService.selectDeptTreeList(dept));
  258. }
  259. /**
  260. * 模糊查询昵称
  261. * @param name
  262. * @return
  263. */
  264. @GetMapping("/selectName")
  265. public AjaxResult selectName(String name, Integer type){
  266. return success(userService.selectName(name,type));
  267. }
  268. }