UserController.java 10 KB

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