| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- package com.izouma.nineth.web;
- import com.alipay.api.AlipayApiException;
- import com.huifu.adapay.core.exception.BaseAdaPayException;
- import com.izouma.nineth.domain.User;
- import com.izouma.nineth.dto.*;
- import com.izouma.nineth.enums.AuthorityName;
- import com.izouma.nineth.exception.BusinessException;
- import com.izouma.nineth.repo.UserBankCardRepo;
- import com.izouma.nineth.repo.UserRepo;
- import com.izouma.nineth.security.Authority;
- import com.izouma.nineth.security.JwtTokenUtil;
- import com.izouma.nineth.security.JwtUserFactory;
- import com.izouma.nineth.service.FollowService;
- import com.izouma.nineth.service.UserBankCardService;
- import com.izouma.nineth.service.UserService;
- import com.izouma.nineth.utils.SecurityUtils;
- import com.izouma.nineth.utils.excel.ExcelUtils;
- import io.swagger.annotations.ApiOperation;
- import lombok.AllArgsConstructor;
- import me.chanjar.weixin.common.error.WxErrorException;
- import org.apache.commons.lang3.StringUtils;
- import org.springframework.data.domain.Page;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.security.access.prepost.PreAuthorize;
- import org.springframework.web.bind.annotation.*;
- import javax.servlet.http.HttpServletResponse;
- import java.io.IOException;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- @AllArgsConstructor
- @RestController
- @RequestMapping("/user")
- public class UserController extends BaseController {
- private UserRepo userRepo;
- private UserService userService;
- private JwtTokenUtil jwtTokenUtil;
- private FollowService followService;
- private UserBankCardRepo userBankCardRepo;
- private RedisTemplate<String, Object> redisTemplate;
- private UserBankCardService userBankCardService;
- @PostMapping("/register")
- public User register(@RequestParam String username,
- @RequestParam String password) {
- UserRegister user = UserRegister.builder()
- .username(username)
- .nickname(username)
- .password(password)
- .authorities(Collections.singleton(Authority.get(AuthorityName.ROLE_USER)))
- .build();
- return userService.create(user);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
- @PostMapping("/create")
- public User create(@RequestBody UserRegister userRegister) {
- return userService.create(userRegister);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
- @PostMapping("/save")
- public User save(@RequestBody User user) {
- if (user.getId() != null) {
- return userService.update(user);
- }
- return userService.save(user);
- }
- @PostMapping("/update")
- public User update(String nickname, String avatar, String sex, String bg, String intro,
- Boolean useCollectionPic, Boolean riskWarning, Integer level) {
- return userService.update(SecurityUtils.getAuthenticatedUser().getId(),
- nickname, avatar, sex, bg, intro, useCollectionPic, riskWarning, level);
- }
- @GetMapping("/my")
- public User my(@RequestParam(defaultValue = "false") boolean refresh) {
- if (refresh) {
- redisTemplate.delete("myUserInfo::" + SecurityUtils.getAuthenticatedUser().getId());
- }
- return userService.my(SecurityUtils.getAuthenticatedUser().getId());
- }
- @GetMapping("/myAdmin")
- @PreAuthorize("hasRole('ADMIN')")
- public User myAdmin() {
- return userRepo.findById(SecurityUtils.getAuthenticatedUser().getId())
- .orElseThrow(new BusinessException("用户不存在"));
- }
- @GetMapping("/myTrading")
- public Object myTrading(@RequestParam(defaultValue = "false") boolean refresh) {
- if (refresh) {
- redisTemplate.delete("myUserInfo::" + SecurityUtils.getAuthenticatedUser().getId());
- }
- return userService.myTrading(SecurityUtils.getAuthenticatedUser().getId());
- }
- // @PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/all")
- public Page<UserDTO> all(@RequestBody PageQuery pageQuery) {
- pageQuery.getQuery().put("minter", true);
- if (pageQuery.getSize() > 100) pageQuery.setSize(100);
- return userService.toDTO(userService.all(pageQuery).toPage());
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
- @PostMapping("/adminAll")
- public Page<User> adminAll(@RequestBody PageQuery pageQuery) {
- return userService.all(pageQuery).toPage();
- }
- @PostMapping("/minterList")
- public Page<Minter> toMinter(@RequestBody PageQuery pageQuery) {
- pageQuery.getQuery().put("minter", true);
- return userService.toMinterDTO(userService.all(pageQuery).toPage());
- }
- // @PreAuthorize("hasRole('ADMIN')")
- @GetMapping("/get/{id}")
- public UserDTO get(@PathVariable Long id) {
- return userService.toDTO(userRepo.findById(id).orElseThrow(new BusinessException("无记录")), true);
- }
- @PreAuthorize("hasRole('ADMIN')")
- @GetMapping("/adminGet/{id}")
- public User adminGet(@PathVariable Long id) {
- return userRepo.findById(id).orElseThrow(new BusinessException("无记录"));
- }
- @PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/del/{id}")
- public void del(@PathVariable Long id) {
- userService.del(id);
- }
- @GetMapping("/excel")
- @ResponseBody
- public void excel(HttpServletResponse response, PageQuery pageQuery) throws IOException {
- List<User> data = userService.all(pageQuery).getContent();
- ExcelUtils.export(response, data);
- }
- @PostMapping("/getMaUserInfo")
- @ApiOperation(value = "获取小程序用户信息")
- public User getMaUserInfo(String sessionKey, String rawData, String signature, String encryptedData, String iv) {
- User user = userService.getMaUserInfo(sessionKey, rawData, signature, encryptedData, iv);
- if (user != null) {
- return user;
- }
- throw new BusinessException("获取用户信息失败");
- }
- @PostMapping("/code2openId")
- @ApiOperation(value = "获取OpenId")
- public String code2openId(@RequestParam String code) throws WxErrorException {
- return userService.code2openId(code);
- }
- @PreAuthorize("hasRole('ADMIN')")
- @PostMapping("/setPasswordAdmin")
- public String setPasswordAdmin(@RequestParam Long userId, @RequestParam String password) {
- return userService.setPassword(userId, password);
- }
- @PostMapping("/changePassword")
- @ApiOperation("修改密码")
- public String changePassword(@RequestParam String password, @RequestParam String code) {
- return userService.setPassword(SecurityUtils.getAuthenticatedUser().getId(), code, password);
- }
- @PostMapping("/forgotPassword")
- @ApiOperation("忘记密码")
- public String forgotPassword(@RequestParam String phone, @RequestParam String password, @RequestParam String code) {
- return userService.forgotPassword(phone, password, code);
- }
- @PreAuthorize("hasRole('ADMIN')")
- @GetMapping("/getToken/{userId}")
- public String getToken(@PathVariable Long userId) {
- return jwtTokenUtil.generateToken(JwtUserFactory.create(userRepo.findById(userId)
- .orElseThrow(new BusinessException("用户不存在"))));
- }
- @PostMapping("/bindPhone")
- public void bindPhone(@RequestParam String phone) {
- userService.bindPhone(SecurityUtils.getAuthenticatedUser().getId(), phone);
- }
- @GetMapping("/{id}/follow")
- public void follow(@PathVariable Long id) {
- followService.follow(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/{id}/unfollow")
- public void unfollow(@PathVariable Long id) {
- followService.unfollow(SecurityUtils.getAuthenticatedUser().getId(), id);
- }
- @GetMapping("/myFollows")
- @ApiOperation("我的关注")
- public List<UserDTO> myFollows() {
- return userService.toDTO(userRepo.userFollows(SecurityUtils.getAuthenticatedUser().getId()));
- }
- @GetMapping("/myFollowers")
- @ApiOperation("关注我的")
- public List<UserDTO> myFollowers() {
- return userService.toDTO(userRepo.userFollowers(SecurityUtils.getAuthenticatedUser().getId()));
- }
- @PostMapping("/setTradeCode")
- @ApiOperation("修改交易密码")
- public void setTradeCode(@RequestParam String token, @RequestParam String tradeCode) {
- userService.setTradeCode(SecurityUtils.getAuthenticatedUser().getId(), token, tradeCode);
- }
- @PostMapping("/verifyTradeCode")
- @ApiOperation("验证交易密码")
- public void verifyTradeCode(@RequestParam String tradeCode) {
- userService.verifyTradeCode(SecurityUtils.getAuthenticatedUser().getId(), tradeCode);
- }
- @PostMapping("/searchByPhone")
- public Map<String, Object> searchByPhone(@RequestParam String phone) {
- return userService.searchByPhone(phone);
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'OPERATOR')")
- @PostMapping("/searchByPhoneAdmin")
- public Map<String, Object> searchByPhoneAdmin(@RequestParam String phone) {
- return userService.searchByPhoneAdmin(phone);
- }
- @GetMapping("/tradeCodeStatus")
- public Object tradeCodeStatus() {
- return new HashMap<String, Object>() {{
- put("set", StringUtils.isNotBlank(
- userRepo.findById(SecurityUtils.getAuthenticatedUser().getId()).map(User::getTradeCode).orElse(null)
- ));
- }};
- }
- @GetMapping("/myBankCard")
- public List<UserBankCard> myBankCard() {
- return userBankCardRepo.findByUserId(SecurityUtils.getAuthenticatedUser().getId());
- }
- @PostMapping("/addBankCard")
- public void addBankCard(@RequestParam String bankNo, @RequestParam String phone, @RequestParam String code) throws BaseAdaPayException {
- userService.addBankCard(SecurityUtils.getAuthenticatedUser().getId(), bankNo, phone, code);
- }
- @PostMapping("/removeBankCard")
- public void removeBankCard() throws BaseAdaPayException {
- userBankCardService.unbind(SecurityUtils.getAuthenticatedUser().getId());
- }
- @PostMapping("/removeBankCardAdmin")
- @PreAuthorize("hasAnyRole('ADMIN')")
- public void removeBankCardAdmin(@RequestParam Long userId) throws BaseAdaPayException {
- userBankCardService.unbind(userId);
- }
- @PostMapping("/removeAuthAdmin")
- @PreAuthorize("hasAnyRole('ADMIN')")
- public void removeAuthAdmin(@RequestParam Long userId) {
- userService.removeAuth(userId);
- }
- @PreAuthorize("hasAnyRole('ADMIN')")
- @PostMapping("/batchRegister")
- public Map<String, Object> batchRegister(@RequestParam String phones, @RequestParam String defaultPassword) {
- return userService.batchRegister(phones, defaultPassword);
- }
- @PreAuthorize("hasAnyRole('ADMIN')")
- @PostMapping("/exportInvite")
- @ResponseBody
- public void exportInvite(HttpServletResponse response, @RequestBody PageQuery pageQuery) throws IOException {
- // List<InvitePhoneDTO> data = userService.all(pageQuery)
- // .map(InvitePhoneDTO::new)
- // .getContent();
- Page<InvitePhoneDTO> user = (Page<InvitePhoneDTO>) this.invite(pageQuery).get("user");
- ExcelUtils.export(response, user.getContent());
- }
- @PostMapping("/invite")
- public Map<String, Object> invite(@RequestBody PageQuery pageQuery) {
- return userService.invite(pageQuery);
- }
- @GetMapping("/collectionInvite")
- public List<User> collectionInvite(@RequestParam Long collectionId) {
- return userRepo.findAllByCollectionIdAndCollectionInvitor(collectionId, SecurityUtils.getAuthenticatedUser()
- .getId());
- }
- @PreAuthorize("hasAnyRole('ADMIN')")
- @GetMapping("/checkSettleAccount")
- public String checkSettleAccount() {
- userService.checkSettleAccountAsync();
- return "ok";
- }
- @PreAuthorize("hasAnyRole('ADMIN')")
- @GetMapping("/scanWeakPass")
- public String scanWeakPass() {
- userService.scanWeakPassword();
- return "ok";
- }
- @PostMapping("/collectionInvitorList")
- public List<InvitorDTO> invitorList(@RequestParam Long collectionId) {
- return userService.findInviteOrderByCount(collectionId);
- }
- @PostMapping("/findMyInviteRecord")
- public InvitorDetailDTO invitorList(@RequestParam Long collectionId, @RequestParam Long userId) {
- return userService.findMyInviteRecord(userId, collectionId);
- }
- @PostMapping("/enableWallet")
- public void enableWallet() {
- userService.enableWallet(SecurityUtils.getAuthenticatedUser().getId());
- }
- @PreAuthorize("hasAnyRole('ADMIN', 'SHOWROOM')")
- @PostMapping("/companyAll")
- public Page<CompanyDTO> companyAll(@RequestBody PageQuery pageQuery) {
- return userService.companyList(pageQuery);
- }
- @PostMapping("/faceAuth")
- public Map<String, String> faceAuth(@RequestParam String name, @RequestParam String idNo) throws AlipayApiException {
- String certifyId = userService.prepareAliAuth("IDENTITY_CARD",
- SecurityUtils.getAuthenticatedUser().getId(), name, idNo);
- String url = userService.getAliAuthUrl(certifyId);
- Map<String, String> map = new HashMap<>();
- map.put("certifyId", certifyId);
- map.put("url", url);
- return map;
- }
- @GetMapping("/checkFaceAuth")
- public Map<String, Object> checkFaceAuth(@RequestParam String certifyId) throws AlipayApiException {
- return userService.checkFaceAuth(certifyId);
- }
- @GetMapping(value = "/faceAuthNotify/{certifyId}", produces = "text/html")
- public String faceAuthNotify(@PathVariable String certifyId) {
- userService.faceAuthNotify(certifyId);
- return "<!DOCTYPE html>\n" +
- "<html lang=\"en\">\n" +
- "\n" +
- "<head>\n" +
- " <meta charset=\"UTF-8\">\n" +
- " <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">\n" +
- " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\n" +
- " <title>认证完成</title>\n" +
- "</head>\n" +
- "\n" +
- "<body>\n" +
- " <script>\n" +
- " function ready(callback) {\n" +
- " // 如果jsbridge已经注入则直接调用\n" +
- " if (window.AlipayJSBridge) {\n" +
- " callback && callback();\n" +
- " } else {\n" +
- " // 如果没有注入则监听注入的事件\n" +
- " document.addEventListener('AlipayJSBridgeReady', callback, false);\n" +
- " }\n" +
- " }\n" +
- " ready(function () {\n" +
- " AlipayJSBridge.call('exitApp');\n" +
- " });\n" +
- " </script>\n" +
- "</body>\n" +
- "\n" +
- "</html>";
- }
- }
|