UserController.java 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. u.setPassword(null);
  43. return returnSuccessResult("登陆成功", u);
  44. }
  45. @RequestMapping(value = "/logout",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
  46. public String logout(){
  47. //获取用户名
  48. String username = SecurityContextHolder.getContext().getAuthentication().getName();
  49. if(username!=null){
  50. redisCacheUtil.removeForUserName(username);
  51. }
  52. //返回成功
  53. return returnSuccessResult("退出成功");
  54. }
  55. /**
  56. * 添加用户
  57. * @return
  58. * String
  59. * @exception
  60. * @since 1.0.0
  61. */
  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. if(user.getPassword()!=null){
  80. user.setPassword(new Md5PasswordEncoder().encodePassword(user.getPassword(), null));
  81. }
  82. userService.update(user);
  83. return super.returnResult(0, "修改成功", null);
  84. }catch(Exception e){
  85. throw new BusinessException(20003);
  86. }
  87. }
  88. /**
  89. * 删除用户(软删除)
  90. * @return
  91. * String
  92. * @exception
  93. * @since 1.0.0
  94. */
  95. @RequestMapping(value = "/deleteUser/{userid}",method=RequestMethod.DELETE,produces="application/json;charset=UTF-8")
  96. public String deleteUser(@PathVariable Integer userid){
  97. try{
  98. userService.delete((userid));
  99. return returnResult(0, "删除成功", null);
  100. }catch(Exception e){
  101. throw new BusinessException(20002);
  102. }
  103. }
  104. /**
  105. * 获取所用用户
  106. * @param page
  107. * @param size
  108. * @return
  109. */
  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. @RequestMapping(value = "/getUserList",method=RequestMethod.POST,produces="application/json;charset=UTF-8")
  121. public String getUsers(@RequestBody User user){
  122. try{
  123. List<User> users= userService.getUsers(user);
  124. return returnResult(0, "获取成功", users);
  125. }catch(Exception e){
  126. throw new BusinessException(20001);
  127. }
  128. }
  129. @RequestMapping(value = "/getUserById/{userid}",method=RequestMethod.GET,produces="application/json;charset=UTF-8")
  130. public String getUser(@PathVariable Integer userid){
  131. try{
  132. User u = userService.getOne(userid);
  133. return returnResult(0, "获取成功", u);
  134. }catch(Exception e){
  135. throw new BusinessException(20001);
  136. }
  137. }
  138. }