UserController.java 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. package com.izouma.nineth.web;
  2. import com.izouma.nineth.domain.User;
  3. import com.izouma.nineth.dto.PageQuery;
  4. import com.izouma.nineth.dto.UserBankCard;
  5. import com.izouma.nineth.dto.UserDTO;
  6. import com.izouma.nineth.dto.UserRegister;
  7. import com.izouma.nineth.enums.AuthorityName;
  8. import com.izouma.nineth.exception.BusinessException;
  9. import com.izouma.nineth.repo.UserBankCardRepo;
  10. import com.izouma.nineth.repo.UserRepo;
  11. import com.izouma.nineth.security.Authority;
  12. import com.izouma.nineth.security.JwtTokenUtil;
  13. import com.izouma.nineth.security.JwtUserFactory;
  14. import com.izouma.nineth.service.FollowService;
  15. import com.izouma.nineth.service.UserService;
  16. import com.izouma.nineth.utils.SecurityUtils;
  17. import com.izouma.nineth.utils.excel.ExcelUtils;
  18. import io.swagger.annotations.ApiOperation;
  19. import lombok.AllArgsConstructor;
  20. import me.chanjar.weixin.common.error.WxErrorException;
  21. import org.apache.commons.lang3.StringUtils;
  22. import org.springframework.data.domain.Page;
  23. import org.springframework.security.access.prepost.PreAuthorize;
  24. import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
  25. import org.springframework.web.bind.annotation.*;
  26. import javax.servlet.http.HttpServletResponse;
  27. import java.io.IOException;
  28. import java.util.Collections;
  29. import java.util.HashMap;
  30. import java.util.List;
  31. import java.util.Map;
  32. @AllArgsConstructor
  33. @RestController
  34. @RequestMapping("/user")
  35. public class UserController extends BaseController {
  36. private UserRepo userRepo;
  37. private UserService userService;
  38. private JwtTokenUtil jwtTokenUtil;
  39. private FollowService followService;
  40. private UserBankCardRepo userBankCardRepo;
  41. @PostMapping("/register")
  42. public User register(@RequestParam String username,
  43. @RequestParam String password) {
  44. UserRegister user = UserRegister.builder()
  45. .username(username)
  46. .nickname(username)
  47. .password(new BCryptPasswordEncoder().encode(password))
  48. .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
  49. .build();
  50. return userService.create(user);
  51. }
  52. @PreAuthorize("hasRole('ADMIN')")
  53. @PostMapping("/create")
  54. public User create(@RequestBody UserRegister userRegister) {
  55. return userService.create(userRegister);
  56. }
  57. @PostMapping("/save")
  58. public User save(@RequestBody User user) {
  59. if (user.getId() != null) {
  60. return userService.update(user);
  61. }
  62. return userRepo.save(user);
  63. }
  64. @GetMapping("/my")
  65. public User my() {
  66. return userRepo.findById(SecurityUtils.getAuthenticatedUser().getId())
  67. .orElseThrow(new BusinessException("用户不存在"));
  68. }
  69. @GetMapping("/myAdmin")
  70. @PreAuthorize("hasRole('ADMIN')")
  71. public User myAdmin() {
  72. return userRepo.findById(SecurityUtils.getAuthenticatedUser().getId())
  73. .orElseThrow(new BusinessException("用户不存在"));
  74. }
  75. // @PreAuthorize("hasRole('ADMIN')")
  76. @PostMapping("/all")
  77. public Page<UserDTO> all(@RequestBody PageQuery pageQuery) {
  78. if (!(SecurityUtils.getAuthenticatedUser() != null && SecurityUtils.getAuthenticatedUser().isAdmin())) {
  79. pageQuery.getQuery().put("hasRole", "ROLE_MINTER");
  80. }
  81. return userService.toDTO(userService.all(pageQuery));
  82. }
  83. // @PreAuthorize("hasRole('ADMIN')")
  84. @GetMapping("/get/{id}")
  85. public UserDTO get(@PathVariable Long id) {
  86. return userService.toDTO(userRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
  87. }
  88. @PreAuthorize("hasRole('ADMIN')")
  89. @PostMapping("/del/{id}")
  90. public void del(@PathVariable Long id) {
  91. userService.del(id);
  92. }
  93. @GetMapping("/excel")
  94. @ResponseBody
  95. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  96. List<User> data = userService.all(pageQuery).getContent();
  97. ExcelUtils.export(response, data);
  98. }
  99. @PostMapping("/getMaUserInfo")
  100. @ApiOperation(value = "获取小程序用户信息")
  101. public User getMaUserInfo(String sessionKey, String rawData, String signature, String encryptedData, String iv) {
  102. User user = userService.getMaUserInfo(sessionKey, rawData, signature, encryptedData, iv);
  103. if (user != null) {
  104. return user;
  105. }
  106. throw new BusinessException("获取用户信息失败");
  107. }
  108. @PostMapping("/code2openId")
  109. @ApiOperation(value = "获取OpenId")
  110. public String code2openId(@RequestParam String code) throws WxErrorException {
  111. return userService.code2openId(code);
  112. }
  113. @PreAuthorize("hasRole('ADMIN')")
  114. @PostMapping("/setPasswordAdmin")
  115. public String setPasswordAdmin(@RequestParam Long userId, @RequestParam String password) {
  116. return userService.setPassword(userId, password);
  117. }
  118. @PostMapping("/changePassword")
  119. @ApiOperation("修改密码")
  120. public String changePassword(@RequestParam String password, @RequestParam String code) {
  121. return userService.setPassword(SecurityUtils.getAuthenticatedUser().getId(), code, password);
  122. }
  123. @PostMapping("/forgotPassword")
  124. @ApiOperation("忘记密码")
  125. public String forgotPassword(@RequestParam String phone, @RequestParam String password, @RequestParam String code) {
  126. return userService.forgotPassword(phone, password, code);
  127. }
  128. @PreAuthorize("hasRole('ADMIN')")
  129. @GetMapping("/getToken/{userId}")
  130. public String getToken(@PathVariable Long userId) {
  131. return jwtTokenUtil.generateToken(JwtUserFactory.create(userRepo.findById(userId)
  132. .orElseThrow(new BusinessException("用户不存在"))));
  133. }
  134. @PostMapping("/bindPhone")
  135. public void bindPhone(@RequestParam String phone) {
  136. userService.bindPhone(SecurityUtils.getAuthenticatedUser().getId(), phone);
  137. }
  138. @GetMapping("/{id}/follow")
  139. public void follow(@PathVariable Long id) {
  140. followService.follow(SecurityUtils.getAuthenticatedUser().getId(), id);
  141. }
  142. @GetMapping("/{id}/unfollow")
  143. public void unfollow(@PathVariable Long id) {
  144. followService.unfollow(SecurityUtils.getAuthenticatedUser().getId(), id);
  145. }
  146. @GetMapping("/myFollows")
  147. @ApiOperation("我的关注")
  148. public List<UserDTO> myFollows() {
  149. return userService.toDTO(userRepo.userFollows(SecurityUtils.getAuthenticatedUser().getId()));
  150. }
  151. @GetMapping("/myFollowers")
  152. @ApiOperation("关注我的")
  153. public List<UserDTO> myFollowers() {
  154. return userService.toDTO(userRepo.userFollowers(SecurityUtils.getAuthenticatedUser().getId()));
  155. }
  156. @PostMapping("/setTradeCode")
  157. @ApiOperation("修改交易密码")
  158. public void setTradeCode(@RequestParam String token, @RequestParam String tradeCode) {
  159. userService.setTradeCode(SecurityUtils.getAuthenticatedUser().getId(), token, tradeCode);
  160. }
  161. @PostMapping("/verifyTradeCode")
  162. @ApiOperation("验证交易密码")
  163. public void verifyTradeCode(@RequestParam String tradeCode) {
  164. userService.verifyTradeCode(SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
  165. }
  166. @PostMapping("/searchByPhone")
  167. public Map<String, Object> searchByPhone(@RequestParam String phone) {
  168. return userService.searchByPhone(phone);
  169. }
  170. @PreAuthorize("hasAnyRole('ADMIN', 'OPERATOR')")
  171. @PostMapping("/searchByPhoneAdmin")
  172. public Map<String, Object> searchByPhoneAdmin(@RequestParam String phone) {
  173. return userService.searchByPhoneAdmin(phone);
  174. }
  175. @GetMapping("/tradeCodeStatus")
  176. public Object tradeCodeStatus() {
  177. return new HashMap<String, Object>() {{
  178. put("set", StringUtils.isNotBlank(
  179. userRepo.findById(SecurityUtils.getAuthenticatedUser().getId()).map(User::getTradeCode).orElse(null)
  180. ));
  181. }};
  182. }
  183. @GetMapping("/myBankCard")
  184. public List<UserBankCard> myBankCard() {
  185. return userBankCardRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
  186. }
  187. }