UserController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.system.controller;
  18. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  19. import com.baomidou.mybatisplus.core.metadata.IPage;
  20. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  21. import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
  22. import io.swagger.annotations.ApiImplicitParam;
  23. import io.swagger.annotations.ApiImplicitParams;
  24. import io.swagger.annotations.ApiOperation;
  25. import io.swagger.annotations.ApiParam;
  26. import lombok.AllArgsConstructor;
  27. import org.springblade.common.cache.CacheNames;
  28. import org.springblade.core.cache.utils.CacheUtil;
  29. import org.springblade.core.excel.util.ExcelUtil;
  30. import org.springblade.core.launch.constant.AppConstant;
  31. import org.springblade.core.mp.support.Condition;
  32. import org.springblade.core.mp.support.Query;
  33. import org.springblade.core.redis.cache.BladeRedis;
  34. import org.springblade.core.secure.BladeUser;
  35. import org.springblade.core.secure.annotation.PreAuth;
  36. import org.springblade.core.secure.utils.AuthUtil;
  37. import org.springblade.core.tenant.annotation.NonDS;
  38. import org.springblade.core.tool.api.R;
  39. import org.springblade.core.tool.constant.BladeConstant;
  40. import org.springblade.core.tool.constant.RoleConstant;
  41. import org.springblade.core.tool.utils.DateUtil;
  42. import org.springblade.core.tool.utils.Func;
  43. import org.springblade.core.tool.utils.StringPool;
  44. import org.springblade.modules.system.entity.User;
  45. import org.springblade.modules.system.excel.UserExcel;
  46. import org.springblade.modules.system.excel.UserImporter;
  47. import org.springblade.modules.system.service.IUserService;
  48. import org.springblade.modules.system.vo.UserLvVo;
  49. import org.springblade.modules.system.vo.UserVO;
  50. import org.springblade.modules.system.wrapper.UserWrapper;
  51. import org.springframework.web.bind.annotation.*;
  52. import org.springframework.web.multipart.MultipartFile;
  53. import springfox.documentation.annotations.ApiIgnore;
  54. import javax.servlet.http.HttpServletResponse;
  55. import javax.validation.Valid;
  56. import java.util.ArrayList;
  57. import java.util.List;
  58. import java.util.Map;
  59. import static org.springblade.core.cache.constant.CacheConstant.USER_CACHE;
  60. /**
  61. * 控制器
  62. *
  63. * @author Chill
  64. */
  65. @NonDS
  66. @ApiIgnore
  67. @RestController
  68. @RequestMapping(AppConstant.APPLICATION_USER_NAME)
  69. @AllArgsConstructor
  70. public class UserController {
  71. private final IUserService userService;
  72. private final BladeRedis bladeRedis;
  73. /**
  74. * 查询单条
  75. */
  76. @ApiOperationSupport(order = 1)
  77. @ApiOperation(value = "查看详情", notes = "传入id")
  78. @GetMapping("/detail")
  79. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  80. public R<UserVO> detail(User user) {
  81. User detail = userService.getOne(Condition.getQueryWrapper(user));
  82. return R.data(UserWrapper.build().entityVO(detail));
  83. }
  84. /**
  85. * 查询单条
  86. */
  87. @ApiOperationSupport(order = 2)
  88. @ApiOperation(value = "查看详情", notes = "传入id")
  89. @GetMapping("/info")
  90. public R<UserVO> info(BladeUser user) {
  91. User detail = userService.getById(user.getUserId());
  92. return R.data(UserWrapper.build().entityVO(detail));
  93. }
  94. /**
  95. * 用户列表
  96. */
  97. @GetMapping("/list")
  98. @ApiImplicitParams({
  99. @ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
  100. @ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
  101. })
  102. @ApiOperationSupport(order = 3)
  103. @ApiOperation(value = "列表", notes = "传入account和realName")
  104. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  105. public R<IPage<UserVO>> list(@ApiIgnore @RequestParam Map<String, Object> user, Query query, BladeUser bladeUser) {
  106. QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
  107. IPage<User> pages = userService.page(Condition.getPage(query), (!bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID)) ? queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId()) : queryWrapper);
  108. return R.data(UserWrapper.build().pageVO(pages));
  109. }
  110. /**
  111. * 自定义用户列表
  112. */
  113. @GetMapping("/page")
  114. @ApiImplicitParams({
  115. @ApiImplicitParam(name = "account", value = "账号名", paramType = "query", dataType = "string"),
  116. @ApiImplicitParam(name = "realName", value = "姓名", paramType = "query", dataType = "string")
  117. })
  118. @ApiOperationSupport(order = 3)
  119. @ApiOperation(value = "列表", notes = "传入account和realName")
  120. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  121. public R<IPage<UserVO>> page(@ApiIgnore User user, Query query, Long deptId, BladeUser bladeUser) {
  122. IPage<User> pages = userService.selectUserPage(Condition.getPage(query), user, deptId, (bladeUser.getTenantId().equals(BladeConstant.ADMIN_TENANT_ID) ? StringPool.EMPTY : bladeUser.getTenantId()));
  123. return R.data(UserWrapper.build().pageVO(pages));
  124. }
  125. /**
  126. * 新增或修改
  127. */
  128. @PostMapping("/submit")
  129. @ApiOperationSupport(order = 4)
  130. @ApiOperation(value = "新增或修改", notes = "传入User")
  131. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  132. public R submit(@Valid @RequestBody User user) {
  133. CacheUtil.clear(USER_CACHE);
  134. return R.status(userService.submit(user));
  135. }
  136. /**
  137. * 修改
  138. */
  139. @PostMapping("/update")
  140. @ApiOperationSupport(order = 5)
  141. @ApiOperation(value = "修改", notes = "传入User")
  142. public R update(@Valid @RequestBody User user) {
  143. CacheUtil.clear(USER_CACHE);
  144. return R.status(userService.updateUser(user));
  145. }
  146. /**
  147. * 删除
  148. */
  149. @PostMapping("/remove")
  150. @ApiOperationSupport(order = 6)
  151. @ApiOperation(value = "删除", notes = "传入id集合")
  152. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  153. public R remove(@RequestParam String ids) {
  154. CacheUtil.clear(USER_CACHE);
  155. return R.status(userService.removeUser(ids));
  156. }
  157. /**
  158. * 设置菜单权限
  159. */
  160. @PostMapping("/grant")
  161. @ApiOperationSupport(order = 7)
  162. @ApiOperation(value = "权限设置", notes = "传入roleId集合以及menuId集合")
  163. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  164. public R grant(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds,
  165. @ApiParam(value = "roleId集合", required = true) @RequestParam String roleIds) {
  166. boolean temp = userService.grant(userIds, roleIds);
  167. return R.status(temp);
  168. }
  169. /**
  170. * 重置密码
  171. */
  172. @PostMapping("/reset-password")
  173. @ApiOperationSupport(order = 8)
  174. @ApiOperation(value = "初始化密码", notes = "传入userId集合")
  175. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  176. public R resetPassword(@ApiParam(value = "userId集合", required = true) @RequestParam String userIds) {
  177. boolean temp = userService.resetPassword(userIds);
  178. return R.status(temp);
  179. }
  180. /**
  181. * 修改密码
  182. */
  183. @PostMapping("/update-password")
  184. @ApiOperationSupport(order = 9)
  185. @ApiOperation(value = "修改密码", notes = "传入密码")
  186. public R updatePassword(BladeUser user, @ApiParam(value = "旧密码", required = true) @RequestParam String oldPassword,
  187. @ApiParam(value = "新密码", required = true) @RequestParam String newPassword,
  188. @ApiParam(value = "新密码", required = true) @RequestParam String newPassword1) {
  189. boolean temp = userService.updatePassword(user.getUserId(), oldPassword, newPassword, newPassword1);
  190. return R.status(temp);
  191. }
  192. /**
  193. * 修改基本信息
  194. */
  195. @PostMapping("/update-info")
  196. @ApiOperationSupport(order = 10)
  197. @ApiOperation(value = "修改基本信息", notes = "传入User")
  198. public R updateInfo(@Valid @RequestBody User user) {
  199. CacheUtil.clear(USER_CACHE);
  200. return R.status(userService.updateUserInfo(user));
  201. }
  202. /**
  203. * 用户列表
  204. */
  205. @GetMapping("/user-list")
  206. @ApiOperationSupport(order = 11)
  207. @ApiOperation(value = "用户列表", notes = "传入user")
  208. public R<List<User>> userList(User user, BladeUser bladeUser) {
  209. QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user);
  210. List<User> list = userService.list((!AuthUtil.isAdministrator()) ? queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId()) : queryWrapper);
  211. return R.data(list);
  212. }
  213. /**
  214. * 导入用户
  215. */
  216. @PostMapping("import-user")
  217. @ApiOperationSupport(order = 12)
  218. @ApiOperation(value = "导入用户", notes = "传入excel")
  219. public R importUser(MultipartFile file, Integer isCovered) {
  220. UserImporter userImporter = new UserImporter(userService, isCovered == 1);
  221. ExcelUtil.save(file, userImporter, UserExcel.class);
  222. return R.success("操作成功");
  223. }
  224. /**
  225. * 导出用户
  226. */
  227. @GetMapping("export-user")
  228. @ApiOperationSupport(order = 13)
  229. @ApiOperation(value = "导出用户", notes = "传入user")
  230. public void exportUser(@ApiIgnore @RequestParam Map<String, Object> user, BladeUser bladeUser, HttpServletResponse response) {
  231. QueryWrapper<User> queryWrapper = Condition.getQueryWrapper(user, User.class);
  232. if (!AuthUtil.isAdministrator()) {
  233. queryWrapper.lambda().eq(User::getTenantId, bladeUser.getTenantId());
  234. }
  235. queryWrapper.lambda().eq(User::getIsDeleted, BladeConstant.DB_NOT_DELETED);
  236. List<UserExcel> list = userService.exportUser(queryWrapper);
  237. ExcelUtil.export(response, "用户数据" + DateUtil.time(), "用户数据表", list, UserExcel.class);
  238. }
  239. /**
  240. * 导出模板
  241. */
  242. @GetMapping("export-template")
  243. @ApiOperationSupport(order = 14)
  244. @ApiOperation(value = "导出模板")
  245. public void exportUser(HttpServletResponse response) {
  246. List<UserExcel> list = new ArrayList<>();
  247. ExcelUtil.export(response, "用户数据模板", "用户数据表", list, UserExcel.class);
  248. }
  249. /**
  250. * 第三方注册用户
  251. */
  252. @PostMapping("/register-guest")
  253. @ApiOperationSupport(order = 15)
  254. @ApiOperation(value = "第三方注册用户", notes = "传入user")
  255. public R registerGuest(User user, Long oauthId) {
  256. return R.status(userService.registerGuest(user, oauthId));
  257. }
  258. /**
  259. * 配置用户平台信息
  260. */
  261. @PostMapping("/update-platform")
  262. @ApiOperationSupport(order = 16)
  263. @ApiOperation(value = "配置用户平台信息", notes = "传入user")
  264. public R updatePlatform(Long userId, Integer userType, String userExt) {
  265. return R.status(userService.updatePlatform(userId, userType, userExt));
  266. }
  267. /**
  268. * 查看平台详情
  269. */
  270. @ApiOperationSupport(order = 17)
  271. @ApiOperation(value = "查看平台详情", notes = "传入id")
  272. @GetMapping("/platform-detail")
  273. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  274. public R<UserVO> platformDetail(User user) {
  275. return R.data(userService.platformDetail(user));
  276. }
  277. /**
  278. * 用户列表查询
  279. */
  280. @ApiImplicitParams({
  281. @ApiImplicitParam(name = "name", value = "人员姓名", paramType = "query", dataType = "string"),
  282. @ApiImplicitParam(name = "deptName", value = "部门名称", paramType = "query", dataType = "string"),
  283. @ApiImplicitParam(name = "postName", value = "职位名称", paramType = "query", dataType = "string"),
  284. @ApiImplicitParam(name = "current", value = "当前页数", paramType = "query", dataType = "int"),
  285. @ApiImplicitParam(name = "size", value = "每页数量", paramType = "query", dataType = "int")
  286. })
  287. @ApiOperationSupport(order = 18)
  288. @ApiOperation(value = "用户列表查询", notes = "用户列表查询")
  289. @GetMapping("/search/user")
  290. public R<IPage<UserVO>> userSearch(@ApiIgnore UserVO user, @ApiIgnore Query query) {
  291. return R.data(userService.selectUserSearch(user, query));
  292. }
  293. /**
  294. * 用户解锁
  295. */
  296. @PostMapping("/unlock")
  297. @ApiOperationSupport(order = 19)
  298. @ApiOperation(value = "账号解锁", notes = "传入id")
  299. public R unlock(String userIds) {
  300. List<User> userList = userService.list(Wrappers.<User>lambdaQuery().in(User::getId, Func.toLongList(userIds)));
  301. userList.forEach(user -> bladeRedis.del(CacheNames.tenantKey(user.getTenantId(), CacheNames.USER_FAIL_KEY, user.getAccount())));
  302. return R.success("操作成功");
  303. }
  304. /**
  305. * 获取用户层级树
  306. */
  307. @PostMapping("/getUserTree")
  308. @ApiOperationSupport(order = 20)
  309. @ApiOperation(value = "获取用户层级树")
  310. public R getUserTree( @ApiParam(value = "顶层用户Id", required = true) @RequestParam Long userId) {
  311. Map<Integer,List<UserLvVo>> map = userService.getUserTree(userId);
  312. return R.data(map);
  313. }
  314. }