UserController.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. package com.izouma.nineth.web;
  2. import com.alipay.api.AlipayApiException;
  3. import com.huifu.adapay.core.exception.BaseAdaPayException;
  4. import com.izouma.nineth.domain.User;
  5. import com.izouma.nineth.dto.*;
  6. import com.izouma.nineth.enums.AuthorityName;
  7. import com.izouma.nineth.exception.BusinessException;
  8. import com.izouma.nineth.repo.UserBankCardRepo;
  9. import com.izouma.nineth.repo.UserRepo;
  10. import com.izouma.nineth.security.Authority;
  11. import com.izouma.nineth.security.JwtTokenUtil;
  12. import com.izouma.nineth.security.JwtUserFactory;
  13. import com.izouma.nineth.service.FollowService;
  14. import com.izouma.nineth.service.UserBankCardService;
  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.data.redis.core.RedisTemplate;
  24. import org.springframework.security.access.prepost.PreAuthorize;
  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. private RedisTemplate<String, Object> redisTemplate;
  42. private UserBankCardService userBankCardService;
  43. @PostMapping("/register")
  44. public User register(@RequestParam String username,
  45. @RequestParam String password) {
  46. UserRegister user = UserRegister.builder()
  47. .username(username)
  48. .nickname(username)
  49. .password(password)
  50. .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
  51. .build();
  52. return userService.create(user);
  53. }
  54. @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
  55. @PostMapping("/create")
  56. public User create(@RequestBody UserRegister userRegister) {
  57. return userService.create(userRegister);
  58. }
  59. @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
  60. @PostMapping("/save")
  61. public User save(@RequestBody User user) {
  62. if (user.getId() != null) {
  63. return userService.update(user);
  64. }
  65. return userService.save(user);
  66. }
  67. @PostMapping("/update")
  68. public User update(String nickname, String avatar, String sex, String bg, String intro,
  69. Boolean useCollectionPic, Boolean riskWarning, Integer level) {
  70. return userService.update(SecurityUtils.getAuthenticatedUser().getId(),
  71. nickname, avatar, sex, bg, intro, useCollectionPic, riskWarning, level);
  72. }
  73. @GetMapping("/my")
  74. public User my(@RequestParam(defaultValue = "false") boolean refresh) {
  75. if (refresh) {
  76. redisTemplate.delete("myUserInfo::" + SecurityUtils.getAuthenticatedUser().getId());
  77. }
  78. return userService.my(SecurityUtils.getAuthenticatedUser().getId());
  79. }
  80. @GetMapping("/myAdmin")
  81. @PreAuthorize("hasRole('ADMIN')")
  82. public User myAdmin() {
  83. return userRepo.findById(SecurityUtils.getAuthenticatedUser().getId())
  84. .orElseThrow(new BusinessException("用户不存在"));
  85. }
  86. @GetMapping("/myTrading")
  87. public Object myTrading(@RequestParam(defaultValue = "false") boolean refresh) {
  88. if (refresh) {
  89. redisTemplate.delete("myUserInfo::" + SecurityUtils.getAuthenticatedUser().getId());
  90. }
  91. return userService.myTrading(SecurityUtils.getAuthenticatedUser().getId());
  92. }
  93. // @PreAuthorize("hasRole('ADMIN')")
  94. @PostMapping("/all")
  95. public Page<UserDTO> all(@RequestBody PageQuery pageQuery) {
  96. pageQuery.getQuery().put("minter", true);
  97. if (pageQuery.getSize() > 100) pageQuery.setSize(100);
  98. return userService.toDTO(userService.all(pageQuery).toPage());
  99. }
  100. @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
  101. @PostMapping("/adminAll")
  102. public Page<User> adminAll(@RequestBody PageQuery pageQuery) {
  103. return userService.all(pageQuery).toPage();
  104. }
  105. @PostMapping("/minterList")
  106. public Page<Minter> toMinter(@RequestBody PageQuery pageQuery) {
  107. pageQuery.getQuery().put("minter", true);
  108. return userService.toMinterDTO(userService.all(pageQuery).toPage());
  109. }
  110. // @PreAuthorize("hasRole('ADMIN')")
  111. @GetMapping("/get/{id}")
  112. public UserDTO get(@PathVariable Long id) {
  113. return userService.toDTO(userRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
  114. }
  115. @PreAuthorize("hasRole('ADMIN')")
  116. @GetMapping("/adminGet/{id}")
  117. public User adminGet(@PathVariable Long id) {
  118. return userRepo.findById(id).orElseThrow(new BusinessException("无记录"));
  119. }
  120. @PreAuthorize("hasRole('ADMIN')")
  121. @PostMapping("/del/{id}")
  122. public void del(@PathVariable Long id) {
  123. userService.del(id);
  124. }
  125. @GetMapping("/excel")
  126. @ResponseBody
  127. public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
  128. List<User> data = userService.all(pageQuery).getContent();
  129. ExcelUtils.export(response, data);
  130. }
  131. @PostMapping("/getMaUserInfo")
  132. @ApiOperation(value = "获取小程序用户信息")
  133. public User getMaUserInfo(String sessionKey, String rawData, String signature, String encryptedData, String iv) {
  134. User user = userService.getMaUserInfo(sessionKey, rawData, signature, encryptedData, iv);
  135. if (user != null) {
  136. return user;
  137. }
  138. throw new BusinessException("获取用户信息失败");
  139. }
  140. @PostMapping("/code2openId")
  141. @ApiOperation(value = "获取OpenId")
  142. public String code2openId(@RequestParam String code) throws WxErrorException {
  143. return userService.code2openId(code);
  144. }
  145. @PreAuthorize("hasRole('ADMIN')")
  146. @PostMapping("/setPasswordAdmin")
  147. public String setPasswordAdmin(@RequestParam Long userId, @RequestParam String password) {
  148. return userService.setPassword(userId, password);
  149. }
  150. @PostMapping("/changePassword")
  151. @ApiOperation("修改密码")
  152. public String changePassword(@RequestParam String password, @RequestParam String code) {
  153. return userService.setPassword(SecurityUtils.getAuthenticatedUser().getId(), code, password);
  154. }
  155. @PostMapping("/forgotPassword")
  156. @ApiOperation("忘记密码")
  157. public String forgotPassword(@RequestParam String phone, @RequestParam String password, @RequestParam String code) {
  158. return userService.forgotPassword(phone, password, code);
  159. }
  160. @PreAuthorize("hasRole('ADMIN')")
  161. @GetMapping("/getToken/{userId}")
  162. public String getToken(@PathVariable Long userId) {
  163. return jwtTokenUtil.generateToken(JwtUserFactory.create(userRepo.findById(userId)
  164. .orElseThrow(new BusinessException("用户不存在"))));
  165. }
  166. @PostMapping("/bindPhone")
  167. public void bindPhone(@RequestParam String phone) {
  168. userService.bindPhone(SecurityUtils.getAuthenticatedUser().getId(), phone);
  169. }
  170. @GetMapping("/{id}/follow")
  171. public void follow(@PathVariable Long id) {
  172. followService.follow(SecurityUtils.getAuthenticatedUser().getId(), id);
  173. }
  174. @GetMapping("/{id}/unfollow")
  175. public void unfollow(@PathVariable Long id) {
  176. followService.unfollow(SecurityUtils.getAuthenticatedUser().getId(), id);
  177. }
  178. @GetMapping("/myFollows")
  179. @ApiOperation("我的关注")
  180. public List<UserDTO> myFollows() {
  181. return userService.toDTO(userRepo.userFollows(SecurityUtils.getAuthenticatedUser().getId()));
  182. }
  183. @GetMapping("/myFollowers")
  184. @ApiOperation("关注我的")
  185. public List<UserDTO> myFollowers() {
  186. return userService.toDTO(userRepo.userFollowers(SecurityUtils.getAuthenticatedUser().getId()));
  187. }
  188. @PostMapping("/setTradeCode")
  189. @ApiOperation("修改交易密码")
  190. public void setTradeCode(@RequestParam String token, @RequestParam String tradeCode) {
  191. userService.setTradeCode(SecurityUtils.getAuthenticatedUser().getId(), token, tradeCode);
  192. }
  193. @PostMapping("/verifyTradeCode")
  194. @ApiOperation("验证交易密码")
  195. public void verifyTradeCode(@RequestParam String tradeCode) {
  196. userService.verifyTradeCode(SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
  197. }
  198. @PostMapping("/searchByPhone")
  199. public Map<String, Object> searchByPhone(@RequestParam String phone) {
  200. return userService.searchByPhone(phone);
  201. }
  202. @PreAuthorize("hasAnyRole('ADMIN', 'OPERATOR')")
  203. @PostMapping("/searchByPhoneAdmin")
  204. public Map<String, Object> searchByPhoneAdmin(@RequestParam String phone) {
  205. return userService.searchByPhoneAdmin(phone);
  206. }
  207. @GetMapping("/tradeCodeStatus")
  208. public Object tradeCodeStatus() {
  209. return new HashMap<String, Object>() {{
  210. put("set", StringUtils.isNotBlank(
  211. userRepo.findById(SecurityUtils.getAuthenticatedUser().getId()).map(User::getTradeCode).orElse(null)
  212. ));
  213. }};
  214. }
  215. @GetMapping("/myBankCard")
  216. public List<UserBankCard> myBankCard() {
  217. return userBankCardRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
  218. }
  219. @PostMapping("/addBankCard")
  220. public void addBankCard(@RequestParam String bankNo, @RequestParam String phone, @RequestParam String code) throws BaseAdaPayException {
  221. userService.addBankCard(SecurityUtils.getAuthenticatedUser().getId(), bankNo, phone, code);
  222. }
  223. @PostMapping("/removeBankCard")
  224. public void removeBankCard() throws BaseAdaPayException {
  225. userBankCardService.unbind(SecurityUtils.getAuthenticatedUser().getId());
  226. }
  227. @PostMapping("/removeBankCardAdmin")
  228. @PreAuthorize("hasAnyRole('ADMIN')")
  229. public void removeBankCardAdmin(@RequestParam Long userId) throws BaseAdaPayException {
  230. userBankCardService.unbind(userId);
  231. }
  232. @PostMapping("/removeAuthAdmin")
  233. @PreAuthorize("hasAnyRole('ADMIN')")
  234. public void removeAuthAdmin(@RequestParam Long userId) {
  235. userService.removeAuth(userId);
  236. }
  237. @PreAuthorize("hasAnyRole('ADMIN')")
  238. @PostMapping("/batchRegister")
  239. public Map<String, Object> batchRegister(@RequestParam String phones, @RequestParam String defaultPassword) {
  240. return userService.batchRegister(phones, defaultPassword);
  241. }
  242. @PreAuthorize("hasAnyRole('ADMIN')")
  243. @PostMapping("/exportInvite")
  244. @ResponseBody
  245. public void exportInvite(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
  246. // List<InvitePhoneDTO> data = userService.all(pageQuery)
  247. // .map(InvitePhoneDTO::new)
  248. // .getContent();
  249. Page<InvitePhoneDTO> user = (Page<InvitePhoneDTO>) this.invite(pageQuery).get("user");
  250. ExcelUtils.export(response, user.getContent());
  251. }
  252. @PostMapping("/invite")
  253. public Map<String, Object> invite(@RequestBody PageQuery pageQuery) {
  254. return userService.invite(pageQuery);
  255. }
  256. @GetMapping("/collectionInvite")
  257. public List<User> collectionInvite(@RequestParam Long collectionId) {
  258. return userRepo.findAllByCollectionIdAndCollectionInvitor(collectionId, SecurityUtils.getAuthenticatedUser()
  259. .getId());
  260. }
  261. @PreAuthorize("hasAnyRole('ADMIN')")
  262. @GetMapping("/checkSettleAccount")
  263. public String checkSettleAccount() {
  264. userService.checkSettleAccountAsync();
  265. return "ok";
  266. }
  267. @PreAuthorize("hasAnyRole('ADMIN')")
  268. @GetMapping("/scanWeakPass")
  269. public String scanWeakPass() {
  270. userService.scanWeakPassword();
  271. return "ok";
  272. }
  273. @PostMapping("/collectionInvitorList")
  274. public List<InvitorDTO> invitorList(@RequestParam Long collectionId) {
  275. return userService.findInviteOrderByCount(collectionId);
  276. }
  277. @PostMapping("/findMyInviteRecord")
  278. public InvitorDetailDTO invitorList(@RequestParam Long collectionId, @RequestParam Long userId) {
  279. return userService.findMyInviteRecord(userId, collectionId);
  280. }
  281. @PostMapping("/enableWallet")
  282. public void enableWallet() {
  283. userService.enableWallet(SecurityUtils.getAuthenticatedUser().getId());
  284. }
  285. @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
  286. @PostMapping("/companyAll")
  287. public Page<CompanyDTO> companyAll(@RequestBody PageQuery pageQuery) {
  288. return userService.companyList(pageQuery);
  289. }
  290. @PostMapping("/faceAuth")
  291. public Map<String, String> faceAuth(@RequestParam String name, @RequestParam String idNo) throws AlipayApiException {
  292. String certifyId = userService.prepareAliAuth("IDENTITY_CARD",
  293. SecurityUtils.getAuthenticatedUser().getId(), name, idNo);
  294. String url = userService.getAliAuthUrl(certifyId);
  295. Map<String, String> map = new HashMap<>();
  296. map.put("certifyId", certifyId);
  297. map.put("url", url);
  298. return map;
  299. }
  300. @GetMapping("/checkFaceAuth")
  301. public Map<String, Object> checkFaceAuth(@RequestParam String certifyId) throws AlipayApiException {
  302. return userService.checkFaceAuth(certifyId);
  303. }
  304. @GetMapping(value = "/faceAuthNotify/{certifyId}", produces = "text/html")
  305. public String faceAuthNotify(@PathVariable String certifyId) {
  306. userService.faceAuthNotify(certifyId);
  307. return "<!DOCTYPE html>\n" +
  308. "<html lang=\"en\">\n" +
  309. "\n" +
  310. "<head>\n" +
  311. " <meta charset=\"UTF-8\">\n" +
  312. " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
  313. " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
  314. " <title>认证完成</title>\n" +
  315. "</head>\n" +
  316. "\n" +
  317. "<body>\n" +
  318. " <script>\n" +
  319. " function ready(callback) {\n" +
  320. " // 如果jsbridge已经注入则直接调用\n" +
  321. " if (window.AlipayJSBridge) {\n" +
  322. " callback && callback();\n" +
  323. " } else {\n" +
  324. " // 如果没有注入则监听注入的事件\n" +
  325. " document.addEventListener('AlipayJSBridgeReady', callback, false);\n" +
  326. " }\n" +
  327. " }\n" +
  328. " ready(function () {\n" +
  329. " AlipayJSBridge.call('exitApp');\n" +
  330. " });\n" +
  331. " </script>\n" +
  332. "</body>\n" +
  333. "\n" +
  334. "</html>";
  335. }
  336. }