From 04d378247c812637f33ef2f0d17535105a51f2cf Mon Sep 17 00:00:00 2001 From: huangyw <1207046171@qq.com> Date: Wed, 20 Nov 2024 21:26:03 +0800 Subject: [PATCH] =?UTF-8?q?release-=20=E9=82=80=E8=AF=B7=E7=A0=81=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=EF=BC=9A=20=E7=AC=AC=E4=B8=80=E7=AC=AC=E4=BA=8C?= =?UTF-8?q?=E4=BD=8D=E6=98=AF=E5=A4=A7=E5=86=99=E8=8B=B1=E6=96=87=EF=BC=8C?= =?UTF-8?q?=E5=90=8E=E9=9D=A2=E5=9B=9B=E4=BD=8D=E6=95=B0=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../luoo/user/service/UserInfoService.java | 1661 +++++++++-------- 1 file changed, 849 insertions(+), 812 deletions(-) diff --git a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java index 28d154b..e5a7dc5 100644 --- a/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java +++ b/luoo_user/src/main/java/com/luoo/user/service/UserInfoService.java @@ -57,338 +57,351 @@ import java.util.stream.Collectors; */ @Service public class UserInfoService { - private final UserInfoDao userInfoDao; - private final IdWorker idWorker; - - private final RedisTemplate redisTemplate; - - private final RabbitTemplate rabbitTemplate; - - private final BCryptPasswordEncoder encoder; - - private final HttpServletRequest request; - - private final JwtUtil jwtUtil; - - private final UserRealNameDao userRealNameDao; - - private final UserBankDao userBankDao; - - private final WithdrawDao withdrawDao; - - private final UserPointLogService userPointLogService; - - private final UserPointLogDao userPointLogDao; - - public UserInfoService(UserInfoDao userInfoDao, IdWorker idWorker, RedisTemplate redisTemplate, - RabbitTemplate rabbitTemplate, BCryptPasswordEncoder encoder, HttpServletRequest request, - JwtUtil jwtUtil, UserRealNameDao userRealNameDao, UserBankDao userBankDao, - WithdrawDao withdrawDao, UserPointLogService userPointLogService, - UserPointLogDao userPointLogDao) { - this.userInfoDao = userInfoDao; - this.idWorker = idWorker; - this.redisTemplate = redisTemplate; - this.rabbitTemplate = rabbitTemplate; - this.encoder = encoder; - this.request = request; - this.jwtUtil = jwtUtil; - this.userRealNameDao = userRealNameDao; - this.userBankDao = userBankDao; - this.withdrawDao = withdrawDao; - this.userPointLogService = userPointLogService; - this.userPointLogDao = userPointLogDao; - } - - /** - * token解析示例 - */ - public void testParseToken() { - - String userInfoid = (String) request.getAttribute("userInfoid"); - String mobile = (String) request.getAttribute("mobile"); - System.out.println("用户ID" + userInfoid); - System.out.println("手机号" + mobile); - } - - /** - * 查询全部列表 - * - * @return - */ - public List findAll() { - return userInfoDao.findAll(); - } - - /** - * 根据ID查询实体 - * - * @param id - * @return - */ - public UserInfo findById(String id) { - return userInfoDao.getById(id); - } - - /** - * 修改 - * - * @param userInfo - */ - public void update(UserInfo userInfo) { - userInfoDao.save(userInfo); - } - - /** - * 删除 - * - * @param id - */ - public void deleteById(String id) { - - String token = (String) request.getAttribute("claims_admin"); - if (token == null || "".equals(token)) { - throw new RuntimeException("权限不足"); - } - userInfoDao.deleteById(id); - } - - public void sendSms(String deviceId, String mobile) { - // 生成6位数字随机数 - String checkcode = RandomStringUtils.randomNumeric(6); - // 向缓存中放一份 - String redisKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + deviceId; - redisTemplate.opsForValue().set(redisKey, checkcode, 15, TimeUnit.MINUTES); - - // 向用户发一份 - Map map = new HashMap<>(); - map.put("mobile", mobile); - map.put("checkcode", checkcode); - rabbitTemplate.convertAndSend("sms", map); - - // 在控制台放一份(方便测试) - - System.out.println("验证码是:" + checkcode); - } - - public UserInfo login(String mobile, String password) { + private final UserInfoDao userInfoDao; + + private final IdWorker idWorker; + + private final RedisTemplate redisTemplate; + + private final RabbitTemplate rabbitTemplate; + + private final BCryptPasswordEncoder encoder; + + private final HttpServletRequest request; + + private final JwtUtil jwtUtil; + + private final UserRealNameDao userRealNameDao; + + private final UserBankDao userBankDao; + + private final WithdrawDao withdrawDao; + + private final UserPointLogService userPointLogService; + + private final UserPointLogDao userPointLogDao; + + public UserInfoService(UserInfoDao userInfoDao, IdWorker idWorker, RedisTemplate redisTemplate, + RabbitTemplate rabbitTemplate, BCryptPasswordEncoder encoder, + HttpServletRequest request, + JwtUtil jwtUtil, UserRealNameDao userRealNameDao, UserBankDao userBankDao, + WithdrawDao withdrawDao, UserPointLogService userPointLogService, + UserPointLogDao userPointLogDao) { + this.userInfoDao = userInfoDao; + this.idWorker = idWorker; + this.redisTemplate = redisTemplate; + this.rabbitTemplate = rabbitTemplate; + this.encoder = encoder; + this.request = request; + this.jwtUtil = jwtUtil; + this.userRealNameDao = userRealNameDao; + this.userBankDao = userBankDao; + this.withdrawDao = withdrawDao; + this.userPointLogService = userPointLogService; + this.userPointLogDao = userPointLogDao; + } + + /** + * token解析示例 + */ + public void testParseToken() { + + String userInfoid = (String) request.getAttribute("userInfoid"); + String mobile = (String) request.getAttribute("mobile"); + System.out.println("用户ID" + userInfoid); + System.out.println("手机号" + mobile); + } + + /** + * 查询全部列表 + * + * @return + */ + public List findAll() { + return userInfoDao.findAll(); + } + + /** + * 根据ID查询实体 + * + * @param id + * @return + */ + public UserInfo findById(String id) { + return userInfoDao.getById(id); + } + + /** + * 修改 + * + * @param userInfo + */ + public void update(UserInfo userInfo) { + userInfoDao.save(userInfo); + } + + /** + * 删除 + * + * @param id + */ + public void deleteById(String id) { + + String token = (String) request.getAttribute("claims_admin"); + if (token == null || "".equals(token)) { + throw new RuntimeException("权限不足"); + } + userInfoDao.deleteById(id); + } + + public void sendSms(String deviceId, String mobile) { + // 生成6位数字随机数 + String checkcode = RandomStringUtils.randomNumeric(6); + // 向缓存中放一份 + String redisKey = Constants.REDIS_KEY_MOBILE_CHECK_CODE + deviceId; + redisTemplate.opsForValue().set(redisKey, checkcode, 15, TimeUnit.MINUTES); + + // 向用户发一份 + Map map = new HashMap<>(); + map.put("mobile", mobile); + map.put("checkcode", checkcode); + rabbitTemplate.convertAndSend("sms", map); + + // 在控制台放一份(方便测试) + + System.out.println("验证码是:" + checkcode); + } + + public UserInfo login(String mobile, String password) { // UserInfo userInfo = userInfoDao.findByLoginname(loginname); - UserInfo userInfo = userInfoDao.findByMobile(mobile); - if (userInfo != null && encoder.matches(password, userInfo.getPassword())) { - return userInfo; - } - return null; - } - - @Transactional - public void updatefanscountandfollowcount(int x, String userInfoid, String friendid) { - userInfoDao.updatefanscount(x, friendid); - userInfoDao.updatefollowcount(x, userInfoid); - } - - public UserInfo loginOrRegister(String mobile) { - UserInfo userInfo = userInfoDao.findByMobile(mobile); - if (null == userInfo) { - userInfo = new UserInfo(); - userInfo.setJoinTime(new Date()); - userInfo.setId(String.valueOf(idWorker.nextId())); - userInfo.setMobile(mobile); - userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - userInfoDao.save(userInfo); - } else { - userInfo.setLastLoginTime(new Date()); - userInfoDao.save(userInfo); - } - return userInfo; - } - - public UserInfo findByMobile(String mobile) { - UserInfo userInfo = userInfoDao.findByMobile(mobile); - return userInfo; - } - - public String autoLogin(String authorization, String deviceId, String deviceBrand, String ip) { - UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(authorization); - if (null == userLoginDto) { - return null; - } - UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfo.setLastLoginIp(ip); - userInfo.setLastUseDeviceId(deviceId); - userInfo.setLastUseDeviceBrand(deviceBrand); - userInfoDao.save(userInfo); - return Constants.TOKEN_PREFIX + jwtUtil.createJWT(userLoginDto.getUserId(), userInfo.getNickName(), - userLoginDto.getRoles(), userLoginDto.getAvatar()); - } - - public long countByNickName(String nickName) { - return userInfoDao.countByNickName(nickName); - } - - /** - * 注册判断是否有邀请码 - * - * @param loginUserInfo 登录用户 - * @param loginReq 邀请码 - * @return token - */ - public String loginOrRegisterWithInvite(UserInfo loginUserInfo, LoginReq loginReq) { - UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); - boolean isNewUser = false; - if (null == userInfo) { - userInfo = loginUserInfo; - userInfo.setId(String.valueOf(idWorker.nextId())); + UserInfo userInfo = userInfoDao.findByMobile(mobile); + if (userInfo != null && encoder.matches(password, userInfo.getPassword())) { + return userInfo; + } + return null; + } + + @Transactional + public void updatefanscountandfollowcount(int x, String userInfoid, String friendid) { + userInfoDao.updatefanscount(x, friendid); + userInfoDao.updatefollowcount(x, userInfoid); + } + + public UserInfo loginOrRegister(String mobile) { + UserInfo userInfo = userInfoDao.findByMobile(mobile); + if (null == userInfo) { + userInfo = new UserInfo(); + userInfo.setJoinTime(new Date()); + userInfo.setId(String.valueOf(idWorker.nextId())); + userInfo.setMobile(mobile); + userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); + userInfoDao.save(userInfo); + } else { + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + } + return userInfo; + } + + public UserInfo findByMobile(String mobile) { + UserInfo userInfo = userInfoDao.findByMobile(mobile); + return userInfo; + } + + public String autoLogin(String authorization, String deviceId, String deviceBrand, String ip) { + UserLoginDto userLoginDto = jwtUtil.getUserLoginDto(authorization); + if (null == userLoginDto) { + return null; + } + UserInfo userInfo = userInfoDao.findById(userLoginDto.getUserId()).get(); + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfo.setLastLoginIp(ip); + userInfo.setLastUseDeviceId(deviceId); + userInfo.setLastUseDeviceBrand(deviceBrand); + userInfoDao.save(userInfo); + return Constants.TOKEN_PREFIX + jwtUtil.createJWT(userLoginDto.getUserId(), + userInfo.getNickName(), + userLoginDto.getRoles(), userLoginDto.getAvatar()); + } + + public long countByNickName(String nickName) { + return userInfoDao.countByNickName(nickName); + } + + /** + * 注册判断是否有邀请码 + * + * @param loginUserInfo 登录用户 + * @param loginReq 邀请码 + * @return token + */ + public String loginOrRegisterWithInvite(UserInfo loginUserInfo, LoginReq loginReq) { + UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); + boolean isNewUser = false; + if (null == userInfo) { + userInfo = loginUserInfo; + userInfo.setId(String.valueOf(idWorker.nextId())); // userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - userInfo.setNickName(NickNameUtil2.generateRandomString()); - Date curDate = new Date(); - userInfo.setJoinTime(curDate); - userInfo.setLastLoginTime(curDate); - userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); - userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); - userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); - userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); - userInfo.setIsAuthor(0); - userInfo.setPoint(0); - userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); - - isNewUser = true; - } else { + userInfo.setNickName(NickNameUtil2.generateRandomString()); + Date curDate = new Date(); + userInfo.setJoinTime(curDate); + userInfo.setLastLoginTime(curDate); + userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); + userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); + userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); + userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + userInfo.setIsAuthor(0); + userInfo.setPoint(0); + userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); + + isNewUser = true; + } else { // userInfo.setAppleId(loginUserInfo.getAppleId()); - userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); - userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); - userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); - } - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfoDao.save(userInfo); - String token = Constants.TOKEN_PREFIX - + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); - if (isNewUser) { - // 只有新用户会获得积分 - userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); - } - return token; - } - - /** - * 绑定邀请人 - * - * @param userInfo 用户 - * @param invitationCode 邀请码 - */ - @Transactional(rollbackFor = Exception.class) - public void bindInvitationCode(UserInfo userInfo, String invitationCode) { - if (StringUtils.isNotBlank(invitationCode)) { - UserInfo userInfoDaoByInvitationCode = userInfoDao.findByInvitationCode(invitationCode); - if (null != userInfoDaoByInvitationCode) { - // 设置邀请人 - userInfo.setInvitationUserId(userInfoDaoByInvitationCode.getId()); - this.update(userInfo); - // 邀请人与被邀请人同时获取积分 - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE, userInfoDaoByInvitationCode.getId()); - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE, userInfo.getId()); - } - - List list = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId(userInfoDaoByInvitationCode.getId(), TaskPointIdConstants.NEW_USER_INVITE); - if (list.size() == 3) { - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_3, userInfoDaoByInvitationCode.getId()); - } else if (list.size() == 6) { - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_6, userInfoDaoByInvitationCode.getId()); - } else if (list.size() == 10) { - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_10, userInfoDaoByInvitationCode.getId()); - } else if (list.size() == 20) { - userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_20, userInfoDaoByInvitationCode.getId()); - } - - } - } - - public String loginOrRegister(UserInfo loginUserInfo) { - UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); - boolean isNewUser = false; - if (null == userInfo) { - userInfo = loginUserInfo; - userInfo.setId(String.valueOf(idWorker.nextId())); + userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); + userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); + userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); + } + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + String token = Constants.TOKEN_PREFIX + + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), + Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + if (isNewUser) { + // 只有新用户会获得积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); + } + return token; + } + + /** + * 绑定邀请人 + * + * @param userInfo 用户 + * @param invitationCode 邀请码 + */ + @Transactional(rollbackFor = Exception.class) + public void bindInvitationCode(UserInfo userInfo, String invitationCode) { + if (StringUtils.isNotBlank(invitationCode)) { + UserInfo userInfoDaoByInvitationCode = userInfoDao.findByInvitationCode(invitationCode); + if (null != userInfoDaoByInvitationCode) { + // 设置邀请人 + userInfo.setInvitationUserId(userInfoDaoByInvitationCode.getId()); + this.update(userInfo); + // 邀请人与被邀请人同时获取积分 + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE, + userInfoDaoByInvitationCode.getId()); + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.NEW_USER_INVITE, + userInfo.getId()); + } + + List list = userPointLogDao.findUserPointLogsByUserIdAndTaskPointId( + userInfoDaoByInvitationCode.getId(), TaskPointIdConstants.NEW_USER_INVITE); + if (list.size() == 3) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_3, + userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 6) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_6, + userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 10) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_10, + userInfoDaoByInvitationCode.getId()); + } else if (list.size() == 20) { + userPointLogService.addByTaskDailyAndUserId(TaskPointIdConstants.INVITE_USER_20, + userInfoDaoByInvitationCode.getId()); + } + + } + } + + public String loginOrRegister(UserInfo loginUserInfo) { + UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); + boolean isNewUser = false; + if (null == userInfo) { + userInfo = loginUserInfo; + userInfo.setId(String.valueOf(idWorker.nextId())); // userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - userInfo.setNickName(NickNameUtil2.generateRandomString()); - Date curDate = new Date(); - userInfo.setJoinTime(curDate); - userInfo.setLastLoginTime(curDate); - userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); - userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); - userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); - userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); - userInfo.setIsAuthor(0); - userInfo.setPoint(0); - userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); - - isNewUser = true; - } else { + userInfo.setNickName(NickNameUtil2.generateRandomString()); + Date curDate = new Date(); + userInfo.setJoinTime(curDate); + userInfo.setLastLoginTime(curDate); + userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); + userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); + userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); + userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + userInfo.setIsAuthor(0); + userInfo.setPoint(0); + userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); + + isNewUser = true; + } else { // userInfo.setAppleId(loginUserInfo.getAppleId()); - userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); - userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); - userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); - } - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfoDao.save(userInfo); - String token = Constants.TOKEN_PREFIX - + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); - - if (isNewUser) { - // 只有新用户会获得积分 - userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); - } - return token; - } - - public String appleLoginOrRegister(UserInfo loginUserInfo) { - UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId()); - boolean isNewUser = false; - if (null == userInfo) { + userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); + userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); + userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); + } + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + String token = Constants.TOKEN_PREFIX + + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), + Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + + if (isNewUser) { + // 只有新用户会获得积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); + } + return token; + } + + public String appleLoginOrRegister(UserInfo loginUserInfo) { + UserInfo userInfo = userInfoDao.findByAppleId(loginUserInfo.getAppleId()); + boolean isNewUser = false; + if (null == userInfo) { // return "未注册"; - userInfo = loginUserInfo; - userInfo.setId(String.valueOf(idWorker.nextId())); - userInfo.setNickName(NickNameUtil2.generateRandomString()); + userInfo = loginUserInfo; + userInfo.setId(String.valueOf(idWorker.nextId())); + userInfo.setNickName(NickNameUtil2.generateRandomString()); // userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - Date curDate = new Date(); - userInfo.setJoinTime(curDate); - userInfo.setLastLoginTime(curDate); - userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); - userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); - userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); - userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); - userInfo.setIsAuthor(0); - userInfo.setPoint(0); - userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); - - isNewUser = true; - } else { - userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); - userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); - userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); - } - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfoDao.save(userInfo); - String token = Constants.TOKEN_PREFIX - + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); - if (isNewUser) { - // 只有新用户会获得积分 - userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); - } - return token; - } + Date curDate = new Date(); + userInfo.setJoinTime(curDate); + userInfo.setLastLoginTime(curDate); + userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); + userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); + userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); + userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + userInfo.setIsAuthor(0); + userInfo.setPoint(0); + userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); + + isNewUser = true; + } else { + userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); + userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); + userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); + } + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + String token = Constants.TOKEN_PREFIX + + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), + Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + if (isNewUser) { + // 只有新用户会获得积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); + } + return token; + } // // public String wechatloginOrRegister(UserInfo loginUserInfo) { @@ -420,501 +433,525 @@ public class UserInfoService { // } - public String wechatlogin(UserInfo loginUserInfo) { + public String wechatlogin(UserInfo loginUserInfo) { + if (!UserStatusEnum.ENABLE.getStatus().equals(loginUserInfo.getStatus())) { + return null; + } + loginUserInfo.setLastLoginTime(new Date()); + userInfoDao.save(loginUserInfo); + return Constants.TOKEN_PREFIX + + jwtUtil.createJWT(loginUserInfo.getId(), loginUserInfo.getNickName(), + Constants.TOKEN_ROLE_APP_USER, loginUserInfo.getAvatar()); + } - if (!UserStatusEnum.ENABLE.getStatus().equals(loginUserInfo.getStatus())) { - return null; - } - loginUserInfo.setLastLoginTime(new Date()); - userInfoDao.save(loginUserInfo); - return Constants.TOKEN_PREFIX - + jwtUtil.createJWT(loginUserInfo.getId(), loginUserInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, loginUserInfo.getAvatar()); - } + public String wechatRegister(UserInfo loginUserInfo) { + UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); - public String wechatRegister(UserInfo loginUserInfo) { - UserInfo userInfo = userInfoDao.findByMobile(loginUserInfo.getMobile()); + boolean isNewUser = false; - boolean isNewUser = false; - - if (null == userInfo) { - userInfo = loginUserInfo; - userInfo.setId(String.valueOf(idWorker.nextId())); - userInfo.setNickName(NickNameUtil2.generateRandomString()); + if (null == userInfo) { + userInfo = loginUserInfo; + userInfo.setId(String.valueOf(idWorker.nextId())); + userInfo.setNickName(NickNameUtil2.generateRandomString()); // userInfo.setNickName("雀乐-" + NickNameUtil.getRandomNickName()); - Date curDate = new Date(); - userInfo.setJoinTime(curDate); - userInfo.setLastLoginTime(curDate); - userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); - userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); - userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); - userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); - userInfo.setIsAuthor(0); - userInfo.setPoint(0); - userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); - - isNewUser = true; - } else { - userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); - userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); - userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); - } - - userInfo.setWxId(loginUserInfo.getWxId()); - if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { - return null; - } - userInfo.setLastLoginTime(new Date()); - userInfoDao.save(userInfo); - String token = Constants.TOKEN_PREFIX - + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); - if (isNewUser) { - // 只有新用户会获得积分 - userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); - } - return token; - } - - - public List orderByField(List idList) { - return userInfoDao.orderByField(idList); - } - - public List cmsOrderByField(List idList) { - return orderByField(idList).stream().map(this::updateAvatarPath).collect(Collectors.toList()); - } - - public List getSimpleUserOrderByField(List idList) { - return userInfoDao.getSimpleUserOrderByField(idList); - } - - public Page findSearch(UserQueryReq queryReq) { - Specification specification = createUserInfoSpecification(queryReq); - PageRequest pageRequest = PageRequest.of(queryReq.getPageNum() - 1, queryReq.getPageSize()); - Page userInfoPage = userInfoDao.findAll(specification, pageRequest); - return userInfoPage.map(this::updateAvatarPath); - } - - private UserInfo updateAvatarPath(UserInfo userInfo) { - if (null != userInfo && null != userInfo.getAvatar()) { - userInfo.setAvatar(Constants.RESOURCE_PREFIX + userInfo.getAvatar()); - } - return userInfo; - } - - public Page findSearch(UserQueryReq queryReq, int page, int size) { - Specification specification = createUserInfoSpecification(queryReq); - PageRequest pageRequest = PageRequest.of(page - 1, size); - return userInfoDao.findAll(specification, pageRequest); - } - - private Specification createUserInfoSpecification(UserQueryReq queryReq) { - return (root, criteriaQuery, criteriaBuilder) -> { - List predicateList = new ArrayList<>(); - if (StringUtils.isNotBlank(queryReq.getId())) { - predicateList.add(criteriaBuilder.equal(root.get("id"), queryReq.getId())); - } - if (StringUtils.isNotBlank(queryReq.getMobile())) { - predicateList.add(criteriaBuilder.equal(root.get("mobile").as(String.class), queryReq.getMobile())); - } - if (StringUtils.isNotBlank(queryReq.getNickName())) { - predicateList.add(criteriaBuilder.like(root.get("nickName"), '%' + queryReq.getNickName() + '%')); - } - if (StringUtils.isNotBlank(queryReq.getBadge())) { - predicateList.add(criteriaBuilder.equal(root.get("badge").as(String.class), queryReq.getBadge())); - } - if (Objects.nonNull(queryReq.getStatus())) { - predicateList.add(criteriaBuilder.equal(root.get("status").as(Integer.class), queryReq.getStatus())); - } - /* - * if(Objects.nonNull(queryReq.getOnlineStatus())){ - * predicateList.add(criteriaBuilder.equal(root.get("onlineStatus").as(Integer. - * class), queryReq.getOnlineStatus())); } - */ - - if (Objects.nonNull(queryReq.getCreateDateStart())) { - predicateList.add(criteriaBuilder.greaterThanOrEqualTo(root.get("joinTime").as(Date.class), - queryReq.getCreateDateStart())); - } - if (Objects.nonNull(queryReq.getCreateDateEnd())) { - predicateList.add(criteriaBuilder.lessThanOrEqualTo(root.get("joinTime").as(Date.class), - queryReq.getCreateDateEnd())); - } - Predicate[] predicates = new Predicate[predicateList.size()]; - criteriaQuery.orderBy(criteriaBuilder.desc(root.get("joinTime").as(Date.class))); - return criteriaBuilder.and(predicateList.toArray(predicates)); - }; - } - - public UserInfo cmsFindById(String id) { - return updateAvatarPath(userInfoDao.getById(id)); - } - - public List getThanks() { - return userInfoDao.getThanks(); - } - - - public void bindMobile(UserInfo userInfo) { - userInfoDao.save(userInfo); - } - - /** - * 校验用户名密码是否匹配 - * - * @param userName 用户名 - * @param password 密码 - * @return 成功返回该用户,失败返回null - */ - public UserInfo findByUserNameAndPassword(String userName, String password) { - UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(userName); - if (userInfoByUserName != null && encoder.matches(password, userInfoByUserName.getPassword())) { - return userInfoByUserName; - } else { - return null; - } - } - - /** - * 提交实名认证 - * - * @param token token - * @param userRealNameFormDto 实名认证对象 - */ - @Transactional(rollbackFor = Exception.class) - public void approveRealName(String token, UserRealNameFormDto userRealNameFormDto) { - UserRealName userRealName = new UserRealName(); - BeanUtils.copyProperties(userRealNameFormDto, userRealName); - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user != null) { - userRealName.setCreateUser(user.getUserId()); - } else { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - // 按业务要求提交后直接是审核通过或不通过 - // todo 需要集成第三方审核服务 - userRealName.setState(UserRealNameStateEnum.PASS.getCode()); - - // 校验是否允许 - List userRealNames = userRealNameDao.checkExist(user.getUserId()); - if (!userRealNames.isEmpty()) { - // 该用户实名认证已存在 - throw new BizException(ErrorConstants.REAL_NAME_APPROVE_IS_EXISTS); - } - - userRealNameDao.save(userRealName); - } - - /** - * 审核实名认证 - * - * @param token token - * @param userRealNameCheckDto 审核对象 - */ - @Transactional(rollbackFor = Exception.class) - public void checkRealName(String token, UserRealNameCheckDto userRealNameCheckDto) { - - UserRealName userRealName = userRealNameDao.findById(userRealNameCheckDto.getId()).get(); - if (userRealName == null) { - // 实名认证错误 - throw new BizException(ErrorConstants.REAL_NAME_ERROR); - } - - if (ObjectUtils.notEqual(UserRealNameStateEnum.CREATE.getCode(), userRealName.getState())) { - // 该用户实名认证已审核完成 - throw new BizException(ErrorConstants.REAL_NAME_IS_CHECKED); - } - - if (ObjectUtils.equals(UserRealNameStateEnum.FAIL.getCode(), userRealNameCheckDto.getState()) - && StringUtils.isBlank(userRealNameCheckDto.getContent())) { - // 必须填写拒绝理由 - throw new BizException(ErrorConstants.REASONS_FOR_REFUSAL_MUST_BE_GIVEN); - } - - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user != null) { - userRealName.setCreateUser(user.getUserId()); - } else { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - - userRealName.setContent(userRealNameCheckDto.getContent()); - userRealName.setState(userRealNameCheckDto.getState()); - userRealName.setUpdateUser(user.getUserId()); - userRealNameDao.save(userRealName); - } - - /** - * 解绑实名认证 - * - * @param token token - * @param id 实名认证id - */ - public void unBindRealName(String token, String id) { - UserRealName userRealName = userRealNameDao.findById(id).get(); - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user != null) { - if (ObjectUtils.notEqual(user.getUserId(), userRealName.getCreateUser())) { - // 必须本人操作 - throw new BizException(ErrorConstants.MUST_OPERATE_IN_PERSON); - } - } else { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - - userRealName.setState(UserRealNameStateEnum.UNBIND.getCode()); - userRealName.setUpdateUser(user.getUserId()); - userRealNameDao.save(userRealName); - } - - /** - * 查询实名认证列表 - * - * @return 列表 - */ - public List getUserRealNameList() { - return userRealNameDao.findAll(); - } - - /** - * 根据id查询实名认证详情 - * - * @param id id - * @return 实名认证详情 - */ - public UserRealName getUserRealName(String id) { - return userRealNameDao.findById(id).get(); - } - - /** - * 根据用户id获取是否有已通过的实名认证信息 - * - * @param userId 用户id - * @return 是否通过 - */ - public Boolean getRealNameInfo(String userId) { - List userRealNames = userRealNameDao.passList(userId); - return !userRealNames.isEmpty(); - } - - /** - * 新增用户绑定银行卡 - * - * @param token token - * @param userBankAddDto 用户绑定银行卡对象 - */ - @Transactional(rollbackFor = Exception.class) - public void addUserBank(String token, UserBankAddDto userBankAddDto) { - UserBank userBank = new UserBank(); - BeanUtils.copyProperties(userBankAddDto, userBank); - userBank.setId(String.valueOf(idWorker.nextId())); - - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user != null) { - userBank.setUserId(user.getUserId()); - } else { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - userBank.setCreateUser(user.getUserId()); - userBank.setState(UserBankStateEnum.BIND.getCode()); - - List checkList = userBankDao.findAllByBankCardAndStateAndUserId(userBank.getBankCard(), userBank.getState(), userBank.getUserId()); - if (checkList.isEmpty()) { - // 先解绑所有已绑定的银行卡 - userBankDao.untieAllCard(user.getUserId()); - // 保存本次的银行卡信息 - userBankDao.save(userBank); - } else { - // 该银行卡已绑定 - throw new BizException(ErrorConstants.THE_BANK_CARD_HAS_BEEN_BOUND); - } - - } - - /** - * 用户解绑银行卡 - * - * @param token token - * @param id 用户绑定银行卡的id - */ - @Transactional(rollbackFor = Exception.class) - public void untieCard(String token, String id) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user == null) { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - UserBank userBank = userBankDao.findById(id).get(); - userBank.setState(UserBankStateEnum.UNBIND.getCode()); - userBank.setUpdateUser(user.getUserId()); - userBankDao.save(userBank); - } - - /** - * 查询用户绑定的银行卡 - * - * @param token 查询人的token - * @return 绑定的银行卡对象 - */ - public UserBank getUserBank(String token) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user == null) { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - - return userBankDao.findAllByUserIdAndState(user.getUserId(), UserBankStateEnum.BIND.getCode()); - } - - /** - * 发起提现申请 - * - * @param token 发起人的token - * @param amount 金额 - */ - @Transactional(rollbackFor = Exception.class) - public void approveWithdraw(String token, BigDecimal amount) { - UserBank userBank = getUserBank(token); - if (userBank == null) { - // 没有绑定银行卡 - throw new BizException(ErrorConstants.NO_BANK_CARD); - } - - if (amount.compareTo(BigDecimal.ZERO) <= 0) { - // 金额必须大于0 - throw new BizException(ErrorConstants.AMOUNT_MUST_BE_GREATER_THAN_0); - } - - UserLoginDto user = jwtUtil.getUserLoginDto(token); - Withdraw withdraw = Withdraw.builder() - .id(String.valueOf(idWorker.nextId())) - .userId(user.getUserId()) - .createUser(user.getUserId()) - .amount(amount) - .state(WithdrawStateEnum.INITIATED.getCode()) - .build(); - withdrawDao.save(withdraw); - } - - /** - * 分页查询用户的提现记录 - * - * @param token 登录token - * @param page 页码 - * @param size 每页数量 - * @return 列表 - */ - public PageResult withdrawPageResult(String token, Integer page, Integer size) { - Pageable pageable = PageRequest.of(page - 1, size); - UserLoginDto user = jwtUtil.getUserLoginDto(token); - if (user == null) { - // 用户校验失败,请重新登录 - throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); - } - Page withdrawPage = withdrawDao.findListByUserId(user.getUserId(), pageable); - long totalElements = withdrawPage.getTotalElements(); - List content = withdrawPage.getContent(); - for (Withdraw withdraw : content) { - if (withdraw.getState() != null) { - withdraw.setStateStr(Objects.requireNonNull(WithdrawStateEnum.getByCode(withdraw.getState())).getDesc()); - } - } - return new PageResult<>(totalElements, content); - } - - /** - * 修改密码 - * - * @param userName 用户名 - * @param password 新密码 - */ - @Transactional(rollbackFor = Exception.class) - public void changePassword(String userName, String password) { - UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(userName); - userInfoByUserName.setPassword(encoder.encode(password)); - userInfoDao.save(userInfoByUserName); - } - - /** - * 确定身份以及判断是否二维码过期等 - * - * @param userId 用户id - * @param token 二维码uuid - * @param type 类型,1-扫码 2-确认 - * @return token - */ - public void scan(String userId, String token, Integer type) throws Exception { - Object o = redisTemplate.opsForValue().get(token); - if (o == null) { - throw new BizException("二维码失效!"); - } - if (!String.valueOf(o).equals("0") && !String.valueOf(o).equals("1")) { - throw new BizException("登录认证已完成"); - } - - if (type == 1) { - // 扫码 - redisTemplate.opsForValue().set(token, 1, 60, TimeUnit.SECONDS); - } else { - // 确认 - UserInfo loginUserInfo = userInfoDao.findById(userId).get(); - String authorization = loginOrRegister(loginUserInfo); - redisTemplate.opsForValue().set(token, authorization, 60, TimeUnit.SECONDS); - } - } - - public List getAuthors() { - return userInfoDao.findAllByIsAuthor(1); - } - - public List getUserInfo(String nickName) { - if (StringUtils.isBlank(nickName)) { - return null; - } else { - return userInfoDao.getUserInfosByNickNameLikeLimit50("%" + nickName + "%"); - } - } - - @Transactional(rollbackFor = Exception.class) - public String createInvitationCode(String token) { - UserLoginDto user = jwtUtil.getUserLoginDto(token); - UserInfo byId = userInfoDao.getById(user.getUserId()); - if (StringUtils.isNotBlank(byId.getInvitationCode())) { - return byId.getInvitationCode(); - } - - String invitationCode = RandomStringUtils.randomNumeric(6); - while (userInfoDao.findByInvitationCode(invitationCode) != null) { - invitationCode = RandomStringUtils.randomNumeric(6); - } - - byId.setInvitationCode(invitationCode); - userInfoDao.save(byId); - - return invitationCode; - } - - /** - * 获取已过期的会员 - * - * @return 列表 - */ - public List getExpireVipList() { - return userInfoDao.getExpireVipList(); - } + Date curDate = new Date(); + userInfo.setJoinTime(curDate); + userInfo.setLastLoginTime(curDate); + userInfo.setStatus(UserStatusEnum.ENABLE.getStatus()); + userInfo.setAvatar(Constants.DEFAULT_USER_AVATAR); + userInfo.setThumbnail(Constants.DEFAULT_USER_THUMBNAIL); + userInfo.setSignature(Constants.DEFAULT_USER_SIGNATURE); + userInfo.setIsAuthor(0); + userInfo.setPoint(0); + userInfo.setVipStatus(UserVipStatusEnum.INITIAL.getCode()); + + isNewUser = true; + } else { + userInfo.setLastUseDeviceId(loginUserInfo.getLastUseDeviceId()); + userInfo.setLastUseDeviceBrand(loginUserInfo.getLastUseDeviceBrand()); + userInfo.setLastLoginIp(loginUserInfo.getLastLoginIp()); + } + + userInfo.setWxId(loginUserInfo.getWxId()); + if (!UserStatusEnum.ENABLE.getStatus().equals(userInfo.getStatus())) { + return null; + } + userInfo.setLastLoginTime(new Date()); + userInfoDao.save(userInfo); + String token = Constants.TOKEN_PREFIX + + jwtUtil.createJWT(userInfo.getId(), userInfo.getNickName(), + Constants.TOKEN_ROLE_APP_USER, userInfo.getAvatar()); + if (isNewUser) { + // 只有新用户会获得积分 + userPointLogService.addByTaskNew(TaskPointIdConstants.NEW_USER_WELCOME_AWARD, token); + } + return token; + } + + + public List orderByField(List idList) { + return userInfoDao.orderByField(idList); + } + + public List cmsOrderByField(List idList) { + return orderByField(idList).stream().map(this::updateAvatarPath) + .collect(Collectors.toList()); + } + + public List getSimpleUserOrderByField(List idList) { + return userInfoDao.getSimpleUserOrderByField(idList); + } + + public Page findSearch(UserQueryReq queryReq) { + Specification specification = createUserInfoSpecification(queryReq); + PageRequest pageRequest = PageRequest.of(queryReq.getPageNum() - 1, queryReq.getPageSize()); + Page userInfoPage = userInfoDao.findAll(specification, pageRequest); + return userInfoPage.map(this::updateAvatarPath); + } + + private UserInfo updateAvatarPath(UserInfo userInfo) { + if (null != userInfo && null != userInfo.getAvatar()) { + userInfo.setAvatar(Constants.RESOURCE_PREFIX + userInfo.getAvatar()); + } + return userInfo; + } + + public Page findSearch(UserQueryReq queryReq, int page, int size) { + Specification specification = createUserInfoSpecification(queryReq); + PageRequest pageRequest = PageRequest.of(page - 1, size); + return userInfoDao.findAll(specification, pageRequest); + } + + private Specification createUserInfoSpecification(UserQueryReq queryReq) { + return (root, criteriaQuery, criteriaBuilder) -> { + List predicateList = new ArrayList<>(); + if (StringUtils.isNotBlank(queryReq.getId())) { + predicateList.add(criteriaBuilder.equal(root.get("id"), queryReq.getId())); + } + if (StringUtils.isNotBlank(queryReq.getMobile())) { + predicateList.add(criteriaBuilder.equal(root.get("mobile").as(String.class), + queryReq.getMobile())); + } + if (StringUtils.isNotBlank(queryReq.getNickName())) { + predicateList.add(criteriaBuilder.like(root.get("nickName"), + '%' + queryReq.getNickName() + '%')); + } + if (StringUtils.isNotBlank(queryReq.getBadge())) { + predicateList.add(criteriaBuilder.equal(root.get("badge").as(String.class), + queryReq.getBadge())); + } + if (Objects.nonNull(queryReq.getStatus())) { + predicateList.add(criteriaBuilder.equal(root.get("status").as(Integer.class), + queryReq.getStatus())); + } + /* + * if(Objects.nonNull(queryReq.getOnlineStatus())){ + * predicateList.add(criteriaBuilder.equal(root.get("onlineStatus").as(Integer. + * class), queryReq.getOnlineStatus())); } + */ + + if (Objects.nonNull(queryReq.getCreateDateStart())) { + predicateList.add( + criteriaBuilder.greaterThanOrEqualTo(root.get("joinTime").as(Date.class), + queryReq.getCreateDateStart())); + } + if (Objects.nonNull(queryReq.getCreateDateEnd())) { + predicateList.add( + criteriaBuilder.lessThanOrEqualTo(root.get("joinTime").as(Date.class), + queryReq.getCreateDateEnd())); + } + Predicate[] predicates = new Predicate[predicateList.size()]; + criteriaQuery.orderBy(criteriaBuilder.desc(root.get("joinTime").as(Date.class))); + return criteriaBuilder.and(predicateList.toArray(predicates)); + }; + } + + public UserInfo cmsFindById(String id) { + return updateAvatarPath(userInfoDao.getById(id)); + } + + public List getThanks() { + return userInfoDao.getThanks(); + } + + + public void bindMobile(UserInfo userInfo) { + userInfoDao.save(userInfo); + } + + /** + * 校验用户名密码是否匹配 + * + * @param userName 用户名 + * @param password 密码 + * @return 成功返回该用户,失败返回null + */ + public UserInfo findByUserNameAndPassword(String userName, String password) { + UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(userName); + if (userInfoByUserName != null && encoder.matches(password, + userInfoByUserName.getPassword())) { + return userInfoByUserName; + } else { + return null; + } + } + + /** + * 提交实名认证 + * + * @param token token + * @param userRealNameFormDto 实名认证对象 + */ + @Transactional(rollbackFor = Exception.class) + public void approveRealName(String token, UserRealNameFormDto userRealNameFormDto) { + UserRealName userRealName = new UserRealName(); + BeanUtils.copyProperties(userRealNameFormDto, userRealName); + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user != null) { + userRealName.setCreateUser(user.getUserId()); + } else { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + // 按业务要求提交后直接是审核通过或不通过 + // todo 需要集成第三方审核服务 + userRealName.setState(UserRealNameStateEnum.PASS.getCode()); + + // 校验是否允许 + List userRealNames = userRealNameDao.checkExist(user.getUserId()); + if (!userRealNames.isEmpty()) { + // 该用户实名认证已存在 + throw new BizException(ErrorConstants.REAL_NAME_APPROVE_IS_EXISTS); + } + + userRealNameDao.save(userRealName); + } + + /** + * 审核实名认证 + * + * @param token token + * @param userRealNameCheckDto 审核对象 + */ + @Transactional(rollbackFor = Exception.class) + public void checkRealName(String token, UserRealNameCheckDto userRealNameCheckDto) { + + UserRealName userRealName = userRealNameDao.findById(userRealNameCheckDto.getId()).get(); + if (userRealName == null) { + // 实名认证错误 + throw new BizException(ErrorConstants.REAL_NAME_ERROR); + } + + if (ObjectUtils.notEqual(UserRealNameStateEnum.CREATE.getCode(), userRealName.getState())) { + // 该用户实名认证已审核完成 + throw new BizException(ErrorConstants.REAL_NAME_IS_CHECKED); + } + + if (ObjectUtils.equals(UserRealNameStateEnum.FAIL.getCode(), + userRealNameCheckDto.getState()) + && StringUtils.isBlank(userRealNameCheckDto.getContent())) { + // 必须填写拒绝理由 + throw new BizException(ErrorConstants.REASONS_FOR_REFUSAL_MUST_BE_GIVEN); + } + + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user != null) { + userRealName.setCreateUser(user.getUserId()); + } else { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + + userRealName.setContent(userRealNameCheckDto.getContent()); + userRealName.setState(userRealNameCheckDto.getState()); + userRealName.setUpdateUser(user.getUserId()); + userRealNameDao.save(userRealName); + } + + /** + * 解绑实名认证 + * + * @param token token + * @param id 实名认证id + */ + public void unBindRealName(String token, String id) { + UserRealName userRealName = userRealNameDao.findById(id).get(); + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user != null) { + if (ObjectUtils.notEqual(user.getUserId(), userRealName.getCreateUser())) { + // 必须本人操作 + throw new BizException(ErrorConstants.MUST_OPERATE_IN_PERSON); + } + } else { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + + userRealName.setState(UserRealNameStateEnum.UNBIND.getCode()); + userRealName.setUpdateUser(user.getUserId()); + userRealNameDao.save(userRealName); + } + + /** + * 查询实名认证列表 + * + * @return 列表 + */ + public List getUserRealNameList() { + return userRealNameDao.findAll(); + } + + /** + * 根据id查询实名认证详情 + * + * @param id id + * @return 实名认证详情 + */ + public UserRealName getUserRealName(String id) { + return userRealNameDao.findById(id).get(); + } + + /** + * 根据用户id获取是否有已通过的实名认证信息 + * + * @param userId 用户id + * @return 是否通过 + */ + public Boolean getRealNameInfo(String userId) { + List userRealNames = userRealNameDao.passList(userId); + return !userRealNames.isEmpty(); + } + + /** + * 新增用户绑定银行卡 + * + * @param token token + * @param userBankAddDto 用户绑定银行卡对象 + */ + @Transactional(rollbackFor = Exception.class) + public void addUserBank(String token, UserBankAddDto userBankAddDto) { + UserBank userBank = new UserBank(); + BeanUtils.copyProperties(userBankAddDto, userBank); + userBank.setId(String.valueOf(idWorker.nextId())); + + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user != null) { + userBank.setUserId(user.getUserId()); + } else { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + userBank.setCreateUser(user.getUserId()); + userBank.setState(UserBankStateEnum.BIND.getCode()); + + List checkList = userBankDao.findAllByBankCardAndStateAndUserId( + userBank.getBankCard(), userBank.getState(), userBank.getUserId()); + if (checkList.isEmpty()) { + // 先解绑所有已绑定的银行卡 + userBankDao.untieAllCard(user.getUserId()); + // 保存本次的银行卡信息 + userBankDao.save(userBank); + } else { + // 该银行卡已绑定 + throw new BizException(ErrorConstants.THE_BANK_CARD_HAS_BEEN_BOUND); + } + + } + + /** + * 用户解绑银行卡 + * + * @param token token + * @param id 用户绑定银行卡的id + */ + @Transactional(rollbackFor = Exception.class) + public void untieCard(String token, String id) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user == null) { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + UserBank userBank = userBankDao.findById(id).get(); + userBank.setState(UserBankStateEnum.UNBIND.getCode()); + userBank.setUpdateUser(user.getUserId()); + userBankDao.save(userBank); + } + + /** + * 查询用户绑定的银行卡 + * + * @param token 查询人的token + * @return 绑定的银行卡对象 + */ + public UserBank getUserBank(String token) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user == null) { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + + return userBankDao.findAllByUserIdAndState(user.getUserId(), + UserBankStateEnum.BIND.getCode()); + } + + /** + * 发起提现申请 + * + * @param token 发起人的token + * @param amount 金额 + */ + @Transactional(rollbackFor = Exception.class) + public void approveWithdraw(String token, BigDecimal amount) { + UserBank userBank = getUserBank(token); + if (userBank == null) { + // 没有绑定银行卡 + throw new BizException(ErrorConstants.NO_BANK_CARD); + } + + if (amount.compareTo(BigDecimal.ZERO) <= 0) { + // 金额必须大于0 + throw new BizException(ErrorConstants.AMOUNT_MUST_BE_GREATER_THAN_0); + } + + UserLoginDto user = jwtUtil.getUserLoginDto(token); + Withdraw withdraw = Withdraw.builder() + .id(String.valueOf(idWorker.nextId())) + .userId(user.getUserId()) + .createUser(user.getUserId()) + .amount(amount) + .state(WithdrawStateEnum.INITIATED.getCode()) + .build(); + withdrawDao.save(withdraw); + } + + /** + * 分页查询用户的提现记录 + * + * @param token 登录token + * @param page 页码 + * @param size 每页数量 + * @return 列表 + */ + public PageResult withdrawPageResult(String token, Integer page, Integer size) { + Pageable pageable = PageRequest.of(page - 1, size); + UserLoginDto user = jwtUtil.getUserLoginDto(token); + if (user == null) { + // 用户校验失败,请重新登录 + throw new BizException(ErrorConstants.USER_VERIFICATION_FAILURE); + } + Page withdrawPage = withdrawDao.findListByUserId(user.getUserId(), pageable); + long totalElements = withdrawPage.getTotalElements(); + List content = withdrawPage.getContent(); + for (Withdraw withdraw : content) { + if (withdraw.getState() != null) { + withdraw.setStateStr( + Objects.requireNonNull(WithdrawStateEnum.getByCode(withdraw.getState())) + .getDesc()); + } + } + return new PageResult<>(totalElements, content); + } + + /** + * 修改密码 + * + * @param userName 用户名 + * @param password 新密码 + */ + @Transactional(rollbackFor = Exception.class) + public void changePassword(String userName, String password) { + UserInfo userInfoByUserName = userInfoDao.findUserInfoByUserName(userName); + userInfoByUserName.setPassword(encoder.encode(password)); + userInfoDao.save(userInfoByUserName); + } + + /** + * 确定身份以及判断是否二维码过期等 + * + * @param userId 用户id + * @param token 二维码uuid + * @param type 类型,1-扫码 2-确认 + * @return token + */ + public void scan(String userId, String token, Integer type) throws Exception { + Object o = redisTemplate.opsForValue().get(token); + if (o == null) { + throw new BizException("二维码失效!"); + } + if (!String.valueOf(o).equals("0") && !String.valueOf(o).equals("1")) { + throw new BizException("登录认证已完成"); + } + + if (type == 1) { + // 扫码 + redisTemplate.opsForValue().set(token, 1, 60, TimeUnit.SECONDS); + } else { + // 确认 + UserInfo loginUserInfo = userInfoDao.findById(userId).get(); + String authorization = loginOrRegister(loginUserInfo); + redisTemplate.opsForValue().set(token, authorization, 60, TimeUnit.SECONDS); + } + } + + public List getAuthors() { + return userInfoDao.findAllByIsAuthor(1); + } + + public List getUserInfo(String nickName) { + if (StringUtils.isBlank(nickName)) { + return null; + } else { + return userInfoDao.getUserInfosByNickNameLikeLimit50("%" + nickName + "%"); + } + } + + @Transactional(rollbackFor = Exception.class) + public String createInvitationCode(String token) { + UserLoginDto user = jwtUtil.getUserLoginDto(token); + UserInfo byId = userInfoDao.getById(user.getUserId()); + if (StringUtils.isNotBlank(byId.getInvitationCode())) { + return byId.getInvitationCode(); + } + + String invitationCode = generateUniqueInvitationCode(); + while (userInfoDao.findByInvitationCode(invitationCode) != null) { + invitationCode = generateUniqueInvitationCode(); + } + + byId.setInvitationCode(invitationCode); + userInfoDao.save(byId); + + return invitationCode; + } /** - * 根据邀请码查询用户信息 - * - * @param invitationCode 邀请码 - * @return 用户信息 + * 邀请码改为: 第一第二位是大写英文,后面四位数字 + * @return 邀请码 */ - public UserInfo getUserInfoByInvitationCode(String invitationCode) { - return userInfoDao.findByInvitationCode(invitationCode); - } + private static String generateUniqueInvitationCode() { + String firstTwoChars = RandomStringUtils.random(2, true, false).toUpperCase(); + String lastFourChars = RandomStringUtils.randomNumeric(4); + return firstTwoChars + lastFourChars; + } + + /** + * 获取已过期的会员 + * + * @return 列表 + */ + public List getExpireVipList() { + return userInfoDao.getExpireVipList(); + } + + /** + * 根据邀请码查询用户信息 + * + * @param invitationCode 邀请码 + * @return 用户信息 + */ + public UserInfo getUserInfoByInvitationCode(String invitationCode) { + return userInfoDao.findByInvitationCode(invitationCode); + } }