UserController.java 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. package com.xintong.visualinspection.controller;
  2. import java.util.List;
  3. import org.springframework.beans.factory.annotation.Autowired;
  4. import org.springframework.beans.factory.annotation.Value;
  5. import org.springframework.security.access.prepost.PreAuthorize;
  6. import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
  7. import org.springframework.security.core.context.SecurityContextHolder;
  8. import org.springframework.web.bind.annotation.PathVariable;
  9. import org.springframework.web.bind.annotation.RequestBody;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestMethod;
  12. import org.springframework.web.bind.annotation.RestController;
  13. import com.github.pagehelper.PageHelper;
  14. import com.github.pagehelper.PageInfo;
  15. import com.xintong.system.err.BusinessException;
  16. import com.xintong.system.securityTools.RedisCacheUtil;
  17. import com.xintong.visualinspection.bean.User;
  18. import com.xintong.visualinspection.service.AuthService;
  19. import com.xintong.visualinspection.service.UserService;
  20. /**
  21. * 文件名:TestController
  22. * 版本信息:日期:2017/3/30 Copyright 江苏省交通规划设计院 Corporation 2017 版权所有.
  23. */
  24. /**
  25. * @author wenhongquan
  26. *
  27. */
  28. @RestController
  29. @RequestMapping("/user")
  30. public class UserController extends BaseController {
  31. @Autowired
  32. private UserService userService;
  33. @Autowired
  34. private AuthService authService;
  35. @Autowired
  36. private RedisCacheUtil redisCacheUtil;
  37. @Value("${jwt.header}")
  38. private String tokenHeader;
  39. @RequestMapping(value = "/auth/login",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
  40. public String login(@RequestBody User user){
  41. User u = authService.login(user.getUsername(), user.getPassword());
  42. return returnSuccessResult("登陆成功", u);
  43. }
  44. @RequestMapping(value = "/logout",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
  45. public String logout(){
  46. //获取用户名
  47. String username = SecurityContextHolder.getContext().getAuthentication().getName();
  48. if(username!=null){
  49. redisCacheUtil.removeForUserName(username);
  50. }
  51. //返回成功
  52. return returnSuccessResult("退出成功");
  53. }
  54. /**
  55. * 添加用户
  56. * @return
  57. * String
  58. * @exception
  59. * @since 1.0.0
  60. */
  61. @PreAuthorize("hasRole('ADMIN')")
  62. @RequestMapping(value = "/addUser",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
  63. public String addUser(@RequestBody User user) throws Exception{
  64. user.setPassword(new Md5PasswordEncoder().encodePassword(user.getPassword(), null));
  65. userService.insert(user);
  66. return returnResult(0, "添加成功", null);
  67. }
  68. /**
  69. * 修改用户
  70. * @return
  71. * String
  72. * @exception
  73. * @since 1.0.0
  74. */
  75. @RequestMapping(value = "/updateUser/{userid}",method=RequestMethod.PUT,produces="application/json;charset=UTF-8")
  76. public String updateUser(@RequestBody User user,@PathVariable int userid){
  77. try{
  78. user.setId(userid);
  79. userService.update(user);
  80. return super.returnResult(0, "修改成功", null);
  81. }catch(Exception e){
  82. throw new BusinessException(20003);
  83. }
  84. }
  85. /**
  86. * 删除用户(软删除)
  87. * @return
  88. * String
  89. * @exception
  90. * @since 1.0.0
  91. */
  92. @PreAuthorize("hasRole('ADMIN')")
  93. @RequestMapping(value = "/deleteUser/{userid}",method=RequestMethod.DELETE,produces="application/json;charset=UTF-8")
  94. public String deleteUser(@PathVariable Integer userid){
  95. try{
  96. userService.delete((userid));
  97. return returnResult(0, "删除成功", null);
  98. }catch(Exception e){
  99. throw new BusinessException(20002);
  100. }
  101. }
  102. /**
  103. * 获取所用用户
  104. * @param page
  105. * @param size
  106. * @return
  107. */
  108. @SuppressWarnings("unchecked")
  109. @PreAuthorize("hasRole('ADMIN')")
  110. @RequestMapping(value = "/get/all/{page}/{size}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
  111. public String getallUsers(@PathVariable Integer page,@PathVariable Integer size ){
  112. try{
  113. PageHelper.startPage(page, size);
  114. List<User> users= userService.getAll();
  115. return returnResult(0, "获取成功", new PageInfo(users));
  116. }catch(Exception e){
  117. throw new BusinessException(20001);
  118. }
  119. }
  120. @PreAuthorize("hasRole('ADMIN')")
  121. @RequestMapping(value = "/getUserList/{page}/{size}",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
  122. public String getUsers(@PathVariable Integer page,@PathVariable Integer size,@RequestBody User user){
  123. try{
  124. PageHelper.startPage(page, size);
  125. List<User> users= userService.getUsers(user);
  126. return returnResult(0, "获取成功", new PageInfo(users));
  127. }catch(Exception e){
  128. throw new BusinessException(20001);
  129. }
  130. }
  131. @RequestMapping(value = "/getUserById/{userid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
  132. public String getUser(@PathVariable Integer userid){
  133. try{
  134. User u = userService.getOne(userid);
  135. return returnResult(0, "获取成功", u);
  136. }catch(Exception e){
  137. throw new BusinessException(20001);
  138. }
  139. }
  140. }