UserController.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package com.fdage.controller;
  2. import com.fdage.aop.WebControllerLog;
  3. import com.fdage.constant.ConstantUrl;
  4. import com.fdage.enums.ResponEnum;
  5. import com.fdage.pojo.TbRole;
  6. import com.fdage.pojo.TbUser;
  7. import com.fdage.request.RequestUser;
  8. import com.fdage.respon.ResponUser;
  9. import com.fdage.service.IRoleService;
  10. import com.fdage.service.IUserService;
  11. import com.fdage.util.AjaxJson;
  12. import com.fdage.util.FileUtil;
  13. import com.fdage.util.PasswordUtils;
  14. import com.github.pagehelper.PageInfo;
  15. import io.swagger.annotations.Api;
  16. import io.swagger.annotations.ApiImplicitParam;
  17. import io.swagger.annotations.ApiImplicitParams;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.extern.slf4j.Slf4j;
  20. import org.apache.commons.lang3.StringUtils;
  21. import org.springframework.beans.BeanUtils;
  22. import org.springframework.beans.factory.annotation.Autowired;
  23. import org.springframework.beans.factory.annotation.Value;
  24. import org.springframework.stereotype.Controller;
  25. import org.springframework.web.bind.annotation.*;
  26. import org.springframework.web.multipart.MultipartFile;
  27. import java.util.List;
  28. /**
  29. * Created by Hb_zzZ on 2019/9/11.
  30. */
  31. @Controller
  32. @RequestMapping("/zhoushan/user")
  33. @Slf4j
  34. @Api(tags = "sys-用户管理模块")
  35. public class UserController {
  36. @Autowired
  37. private IUserService userService;
  38. @Autowired
  39. private IRoleService roleService;
  40. @Value("${upload.head}")
  41. private String userHead;
  42. @PostMapping("addUser")
  43. @ResponseBody
  44. @WebControllerLog(description = "用户管理-新增用户")
  45. @ApiOperation("新增用户")
  46. @ApiImplicitParams({
  47. @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"),
  48. @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String"),
  49. @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String"),
  50. @ApiImplicitParam(name = "head", value = "头像连接地址", dataType = "String"),
  51. @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")})
  52. public AjaxJson addUser(@RequestBody RequestUser bo){
  53. if(bo == null || StringUtils.isEmpty(bo.getUserName()) ||
  54. bo.getRoleId() == null || StringUtils.isEmpty(bo.getPhone()) || bo.getState() == null){
  55. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  56. }
  57. TbUser user = userService.findByUserName(bo.getUserName());
  58. if(user != null){
  59. return AjaxJson.failure(ResponEnum.USER_EXIST.getCode(), ResponEnum.USER_EXIST.getMessage());
  60. }
  61. user = new TbUser();
  62. BeanUtils.copyProperties(bo, user);
  63. user.setPassword(PasswordUtils.encrypt("1234abcd", bo.getUserName(), PasswordUtils.getStaticSalt()));
  64. userService.insert(user);
  65. roleService.saveRoleUser(bo.getRoleId(), user.getId());
  66. return AjaxJson.success(user);
  67. }
  68. @PostMapping("updateUser")
  69. @ResponseBody
  70. @WebControllerLog(description = "用户管理-修改用户")
  71. @ApiOperation("修改用户")
  72. @ApiImplicitParams({
  73. @ApiImplicitParam(name = "id", value = "id", dataType = "String"),
  74. @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"),
  75. @ApiImplicitParam(name = "roleId", value = "角色id", dataType = "String"),
  76. @ApiImplicitParam(name = "phone", value = "手机号码", dataType = "String"),
  77. @ApiImplicitParam(name = "head", value = "头像连接地址", dataType = "String"),
  78. @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")})
  79. public AjaxJson updateUser(@RequestBody RequestUser bo){
  80. if(bo == null || StringUtils.isEmpty(bo.getUserName()) ||
  81. bo.getRoleId() == null || StringUtils.isEmpty(bo.getPhone()) || bo.getState() == null ){
  82. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  83. }
  84. TbUser user = userService.findByUserName(bo.getUserName());
  85. if(user == null){
  86. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  87. }
  88. bo.setId(user.getId());
  89. BeanUtils.copyProperties(bo, user);
  90. user.setPassword(PasswordUtils.encrypt(bo.getPassword(), bo.getUserName(), PasswordUtils.getStaticSalt()));
  91. userService.update(user);
  92. roleService.deleteRoleUser(user.getId());
  93. roleService.saveRoleUser(bo.getRoleId(), user.getId());
  94. return AjaxJson.success();
  95. }
  96. @PostMapping("deleteUser")
  97. @ResponseBody
  98. @WebControllerLog(description = "用户管理-删除用户")
  99. @ApiOperation("删除用户")
  100. @ApiImplicitParams({
  101. @ApiImplicitParam(name = "id", value = "id", dataType = "String")})
  102. public AjaxJson deleteUser(@RequestBody RequestUser bo){
  103. if(bo == null || bo.getId() == null){
  104. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  105. }
  106. TbUser user = userService.findById(bo.getId());
  107. if(user == null){
  108. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  109. }
  110. userService.delete(user.getId());
  111. roleService.deleteRoleUser(user.getId());
  112. return AjaxJson.success();
  113. }
  114. @PostMapping("updateState")
  115. @ResponseBody
  116. @WebControllerLog(description = "用户管理-用户启用/停用")
  117. @ApiOperation("用户启用/停用")
  118. @ApiImplicitParams({
  119. @ApiImplicitParam(name = "id", value = "id", dataType = "String"),
  120. @ApiImplicitParam(name = "state", value = "状态,0:启用,1:禁用", dataType = "String")})
  121. public AjaxJson updateState(@RequestBody RequestUser bo){
  122. if(bo == null || bo.getId() == null || bo.getState() == null){
  123. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  124. }
  125. TbUser user = userService.findById(bo.getId());
  126. if(user == null){
  127. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  128. }
  129. user.setState(bo.getState());
  130. userService.update(user);
  131. return AjaxJson.success();
  132. }
  133. @PostMapping("findById")
  134. @ResponseBody
  135. @ApiOperation("通过id查找用户")
  136. @ApiImplicitParams({
  137. @ApiImplicitParam(name = "id", value = "id", dataType = "String")})
  138. public AjaxJson findById(@RequestBody RequestUser bo){
  139. if(bo == null || bo.getId() == null){
  140. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  141. }
  142. TbUser user = userService.findById(bo.getId());
  143. if(user == null){
  144. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  145. }
  146. user.setPassword("");
  147. return AjaxJson.success(user);
  148. }
  149. @PostMapping("updatePassword")
  150. @ResponseBody
  151. @WebControllerLog(description = "用户管理-用户修改密码")
  152. @ApiOperation("用户修改密码")
  153. @ApiImplicitParams({
  154. @ApiImplicitParam(name = "id", value = "id", dataType = "String"),
  155. @ApiImplicitParam(name = "repeatPassword", value = "重复新密码", dataType = "String"),
  156. @ApiImplicitParam(name = "newPassword", value = "新密码", dataType = "String"),
  157. @ApiImplicitParam(name = "password", value = "当前密码", dataType = "String")})
  158. public AjaxJson updatePassword(@RequestBody RequestUser bo){
  159. if(bo == null || bo.getId() == null || StringUtils.isEmpty(bo.getPassword()) ||
  160. StringUtils.isEmpty(bo.getRepeatPassword()) || StringUtils.isEmpty(bo.getNewPassword())){
  161. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  162. }
  163. if(!bo.getRepeatPassword().equals(bo.getNewPassword())){
  164. return AjaxJson.failure(ResponEnum.PASSWORD_INCONSISTENCY.getCode(), ResponEnum.PASSWORD_INCONSISTENCY.getMessage());
  165. }
  166. TbUser user = userService.findById(bo.getId());
  167. if(user == null){
  168. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  169. }
  170. String encryptPwd = PasswordUtils.encrypt(bo.getPassword(), user.getUserName(), PasswordUtils.getStaticSalt());
  171. if(!encryptPwd.equals(user.getPassword())){
  172. return AjaxJson.failure(ResponEnum.PASSWORD_ERROR.getCode(), ResponEnum.PASSWORD_ERROR.getMessage());
  173. }
  174. user.setPassword(PasswordUtils.encrypt(bo.getNewPassword(), user.getUserName(), PasswordUtils.getStaticSalt()));
  175. userService.update(user);
  176. return AjaxJson.success();
  177. }
  178. @PostMapping("resetPassword")
  179. @ResponseBody
  180. @WebControllerLog(description = "用户管理-用户重置密码")
  181. @ApiOperation("用户重置密码")
  182. @ApiImplicitParams({
  183. @ApiImplicitParam(name = "id", value = "id", dataType = "String")})
  184. public AjaxJson resetPassword(@RequestBody RequestUser bo){
  185. if(bo == null || bo.getId() == null){
  186. return AjaxJson.failure(ResponEnum.NOT_NULL.getCode(), ResponEnum.NOT_NULL.getMessage());
  187. }
  188. TbUser user = userService.findById(bo.getId());
  189. if(user == null){
  190. return AjaxJson.failure(ResponEnum.USER_NOT_EXIST.getCode(), ResponEnum.USER_NOT_EXIST.getMessage());
  191. }
  192. user.setPassword(PasswordUtils.encrypt("1234abcd", user.getUserName(), PasswordUtils.getStaticSalt()));
  193. userService.update(user);
  194. return AjaxJson.success();
  195. }
  196. @PostMapping("list")
  197. @ResponseBody
  198. @WebControllerLog(description = "用户管理-获取用户列表")
  199. @ApiOperation("获取用户列表")
  200. @ApiImplicitParams({
  201. @ApiImplicitParam(name = "status", value = "状态,0:启用,1:禁用", dataType = "String"),
  202. @ApiImplicitParam(name = "userName", value = "用户名", dataType = "String"),
  203. @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "String"),
  204. @ApiImplicitParam(name = "pageSize", value = "每页数量", dataType = "String")})
  205. public AjaxJson list(@RequestBody RequestUser bo){
  206. List<ResponUser> list = userService.findUserList(bo);
  207. PageInfo<ResponUser> pageInfo = new PageInfo<>(list);
  208. return AjaxJson.success(pageInfo);
  209. }
  210. @PostMapping("roleList")
  211. @ResponseBody
  212. @ApiOperation("获取所有角色")
  213. public AjaxJson roleList(){
  214. List<TbRole> list = userService.roleList();
  215. return AjaxJson.success(list);
  216. }
  217. @RequestMapping("/uploadHead")
  218. @ResponseBody
  219. @WebControllerLog(description = "用户管理-用户上传头像")
  220. @ApiOperation("用户上传头像")
  221. @ApiImplicitParams({
  222. @ApiImplicitParam(name = "file", value = "文件流", dataType = "String")})
  223. public AjaxJson upload(@RequestParam(value = "filename", defaultValue = "") String name,@RequestParam("file") MultipartFile file){
  224. if(file == null){
  225. return AjaxJson.failure("参数不能为空");
  226. }
  227. String fileName = System.currentTimeMillis() + "_";
  228. if(StringUtils.isNotEmpty(name)){
  229. fileName = fileName + name;
  230. }else {
  231. fileName = fileName + file.getOriginalFilename();
  232. }
  233. log.info("图片地址:" + fileName);
  234. boolean flag = FileUtil.upload(file, userHead, fileName);
  235. if(!flag){
  236. return AjaxJson.failure("上传图片失败");
  237. }
  238. // Map<String, String> map = new HashMap<>();
  239. // map.put(path + "/" + fileName, "company/" + fileName);
  240. // uploadToAlibabaService.upload(map);
  241. return AjaxJson.success((Object) ("/head/" + fileName));
  242. }
  243. }