| | |
| | | package com.xinquan.user.service.impl; |
| | | |
| | | import com.alibaba.nacos.common.utils.StringUtils; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.crypto.SecureUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.xinquan.common.core.constant.Constants; |
| | | import com.xinquan.common.core.constant.SecurityConstants; |
| | | import com.xinquan.common.core.domain.R; |
| | | import com.xinquan.common.core.enums.AnswerTagEnum; |
| | | import com.xinquan.common.core.enums.AppUserStatusEnum; |
| | | import com.xinquan.common.core.enums.DisabledEnum; |
| | | import com.xinquan.common.core.enums.TreeLevelEnum; |
| | | import com.xinquan.common.core.exception.ServiceException; |
| | | import com.xinquan.common.core.utils.page.BeanUtils; |
| | | import com.xinquan.user.api.domain.AppUser; |
| | | import com.xinquan.common.core.utils.page.CollUtils; |
| | | import com.xinquan.common.security.service.TokenService; |
| | | import com.xinquan.common.security.utils.SecurityUtils; |
| | | import com.xinquan.meditation.api.feign.RemoteMeditationService; |
| | | import com.xinquan.system.api.RemoteUserService; |
| | | import com.xinquan.system.api.domain.SysUser; |
| | | import com.xinquan.system.api.feignClient.SysUserClient; |
| | | import com.xinquan.system.api.model.*; |
| | | import com.xinquan.system.api.domain.AppUser; |
| | | import com.xinquan.user.api.domain.dto.AppUserDTO; |
| | | import com.xinquan.system.api.domain.AppUserQuestion; |
| | | import com.xinquan.system.api.domain.AppUserTree; |
| | | import com.xinquan.system.api.domain.Tag; |
| | | import com.xinquan.user.domain.dto.UserAnswerDTO; |
| | | import com.xinquan.system.api.domain.vo.AppUserVO; |
| | | import com.xinquan.user.domain.vo.TagVO; |
| | | import com.xinquan.user.mapper.AppUserMapper; |
| | | import com.xinquan.user.service.AppUserQuestionService; |
| | | import com.xinquan.user.service.AppUserService; |
| | | import com.xinquan.user.service.AppUserTreeService; |
| | | import com.xinquan.user.service.TagService; |
| | | import java.math.BigDecimal; |
| | | import java.text.SimpleDateFormat; |
| | | import java.time.LocalDateTime; |
| | | import java.util.*; |
| | | import java.util.stream.Collectors; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | import org.springframework.util.StringUtils; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | /** |
| | | * <p> |
| | |
| | | * @since 2024-08-21 |
| | | */ |
| | | @Service |
| | | @RequiredArgsConstructor |
| | | public class AppUserServiceImpl extends ServiceImpl<AppUserMapper, AppUser> implements AppUserService { |
| | | |
| | | private final SysUserClient sysUserClient; |
| | | private final RemoteUserService remoteUserService; |
| | | private final AppUserTreeService appUserTreeService; |
| | | private final AppUserQuestionService appUserQuestionService; |
| | | private final TagService tagService; |
| | | @Resource |
| | | private RemoteMeditationService remoteMeditationService; |
| | | /** |
| | | * 获取用户信息 |
| | | * |
| | | * @param dto |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AppUser getUserByCondition(AppUserDTO dto) { |
| | | return this.lambdaQuery() |
| | | .eq(StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId()) |
| | | .eq(StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone, |
| | | public Optional<AppUser> getUserByCondition(AppUserDTO dto) { |
| | | String device = dto.getDevice(); |
| | | if (StringUtils.hasLength(dto.getDevice())) { |
| | | Optional<AppUser> one = this.lambdaQuery() |
| | | .eq(AppUser::getDevice, device) |
| | | .isNull(AppUser::getCellPhone).oneOpt(); |
| | | Optional<AppUser> appUser = this.lambdaQuery() |
| | | .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone, |
| | | dto.getCellPhone()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId, |
| | | dto.getWxOpenId()) |
| | | .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt(); |
| | | // 如果不存在 |
| | | if (!appUser.isPresent()){ |
| | | if (one.isPresent()) { |
| | | initUserTree(one.get().getId()); |
| | | this.updateById(one.get()); |
| | | return one; |
| | | } else { |
| | | return this.lambdaQuery() |
| | | .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone, |
| | | dto.getCellPhone()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId, |
| | | dto.getWxOpenId()) |
| | | .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt(); |
| | | } |
| | | } |
| | | |
| | | } |
| | | return this.lambdaQuery() |
| | | .eq(Objects.nonNull(dto.getId()), AppUser::getId, dto.getId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getAppleId()), AppUser::getAppleId, dto.getAppleId()) |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getCellPhone()), AppUser::getCellPhone, |
| | | dto.getCellPhone()) |
| | | .eq(StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId, |
| | | dto.getWxOpenId()).oneOpt().orElse(null); |
| | | .eq(com.alibaba.nacos.common.utils.StringUtils.isNotBlank(dto.getWxOpenId()), AppUser::getWxOpenId, |
| | | dto.getWxOpenId()) |
| | | .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()).oneOpt(); |
| | | } |
| | | @Override |
| | | public Optional<AppUser> getUserByCondition1(AppUserDTO dto) { |
| | | return this.lambdaQuery() |
| | | .eq(AppUser::getCellPhone,dto.getCellPhone()) |
| | | .ne(AppUser::getUserStatus,3).oneOpt(); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * app用户注册 |
| | |
| | | * @return |
| | | */ |
| | | @Override |
| | | public Boolean registerAppUser(AppUserDTO appUserDTO) { |
| | | public AppUser registerAppUser(AppUserDTO appUserDTO) { |
| | | if (StringUtils.hasLength(appUserDTO.getDevice())){ |
| | | AppUser one = this.lambdaQuery().eq(AppUser::getDevice, appUserDTO.getDevice()) |
| | | .isNull(AppUser::getCellPhone).one(); |
| | | if (one == null){ |
| | | AppUser appUser = BeanUtils.copyBean(appUserDTO, AppUser.class); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH"+format+randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | initUserTree(appUser.getId()); |
| | | return appUser; |
| | | }else{ |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | one.setCode("YH"+format+randomCode); |
| | | this.updateById(one); |
| | | initUserTree(one.getId()); |
| | | return one; |
| | | } |
| | | } |
| | | AppUser appUser = BeanUtils.copyBean(appUserDTO, AppUser.class); |
| | | return this.save(appUser); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH"+format+randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | initUserTree(appUser.getId()); |
| | | return appUser; |
| | | } |
| | | |
| | | /** |
| | | * APP微信登录 |
| | | * |
| | | * @param body |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AppLoginUser wxLogin(AppWXLoginBody body) { |
| | | AppLoginUser appLoginUser = new AppLoginUser(); |
| | | Optional<AppUser> optionalAppUser = this.getUserByCondition( |
| | | AppUserDTO.builder().wxOpenId(body.getWxOpenId()).device(body.getDevice()).build()); |
| | | AppUser appUser = new AppUser(); |
| | | SysUser sysUser = new SysUser(); |
| | | // 存在账户,查询关联系统用户 |
| | | if (optionalAppUser.isPresent()) { |
| | | appUser = optionalAppUser.get(); |
| | | if (appUser.getUserStatus().equals(AppUserStatusEnum.FROZEN.getCode())) { |
| | | throw new ServiceException("账号已被冻结"); |
| | | } |
| | | sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData(); |
| | | appUser.setAvatar(body.getHeadImgUrl()); |
| | | appUser.setWxName(body.getNickname()); |
| | | if (appUser.getCellPhone()!=null){ |
| | | appLoginUser.setBindStatus(DisabledEnum.NO.getCode()); |
| | | appLoginUser.setCellPhone(appUser.getCellPhone()); |
| | | }else{ |
| | | appLoginUser.setBindStatus(DisabledEnum.YES.getCode()); |
| | | } |
| | | } else { |
| | | if (org.springframework.util.StringUtils.hasLength(body.getDevice())){ |
| | | AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class) |
| | | .eq(AppUser::getDevice, body.getDevice()) |
| | | .isNull(AppUser::getCellPhone)); |
| | | if (one!=null){ |
| | | initUserTree(appUser.getId()); |
| | | appUser = one; |
| | | }else{ |
| | | // 不存在账户,创建账户 |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | sysUser.setNickName(body.getNickname()); |
| | | sysUser.setAvatar(body.getHeadImgUrl()); |
| | | sysUser.setUserName(body.getNickname()); |
| | | sysUser.setUserType("01"); // app用户 |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH"+format+randomCode); |
| | | appUser.setWxOpenId(body.getWxOpenId()); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | appUser.setNickname(nickname); |
| | | appUser.setDevice(body.getDevice()); |
| | | appUser.setWxName(body.getNickname()); |
| | | appUser.setAvatar(body.getHeadImgUrl()); |
| | | appUser.setGender(3); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | appLoginUser.setBindStatus(DisabledEnum.YES.getCode()); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | }else{ |
| | | // 不存在账户,创建账户 |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | sysUser.setNickName(body.getNickname()); |
| | | sysUser.setAvatar(body.getHeadImgUrl()); |
| | | sysUser.setUserName(body.getNickname()); |
| | | sysUser.setUserType("01"); // app用户 |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH"+format+randomCode); |
| | | appUser.setWxOpenId(body.getWxOpenId()); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | appUser.setNickname(nickname); |
| | | appUser.setWxName(body.getNickname()); |
| | | appUser.setAvatar(body.getHeadImgUrl()); |
| | | appUser.setDevice(body.getDevice()); |
| | | appUser.setGender(3); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | appLoginUser.setBindStatus(DisabledEnum.YES.getCode()); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | } |
| | | appLoginUser.setUserid(sysUser.getUserId()); |
| | | appLoginUser.setUsername(appUser.getNickname()); |
| | | appLoginUser.setAppUserId(appUser.getId()); |
| | | appLoginUser.setSysUser(sysUser); |
| | | this.updateById(appUser); |
| | | // if (Objects.nonNull(sysUser.getUserName())) { |
| | | // appLoginUser.setCellPhone(appUser.getCellPhone()); |
| | | // } |
| | | return appLoginUser; |
| | | } |
| | | |
| | | @Override |
| | | public AppLoginUser appleLogin(String appleId,String device) { |
| | | AppLoginUser appLoginUser = new AppLoginUser(); |
| | | Optional<AppUser> optionalAppUser = this.getUserByCondition( |
| | | AppUserDTO.builder().appleId(appleId).device(device).build()); |
| | | SysUser sysUser = new SysUser(); |
| | | AppUser appUser = new AppUser(); |
| | | // 存在账户,查询关联系统用户 |
| | | if (optionalAppUser.isPresent()) { |
| | | appUser = optionalAppUser.get(); |
| | | if (appUser.getUserStatus().equals(AppUserStatusEnum.FROZEN.getCode())) { |
| | | throw new ServiceException("账号已被冻结"); |
| | | } |
| | | sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData(); |
| | | if (appUser.getCellPhone() != null) { |
| | | appLoginUser.setBindStatus(DisabledEnum.NO.getCode()); |
| | | appLoginUser.setCellPhone(appUser.getCellPhone()); |
| | | } |
| | | } else { |
| | | if (org.springframework.util.StringUtils.hasLength(device)) { |
| | | AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class) |
| | | .eq(AppUser::getDevice, device) |
| | | .isNull(AppUser::getCellPhone)); |
| | | if (one != null) { |
| | | initUserTree(one.getId()); |
| | | appUser.setAppleId(appleId); |
| | | appUser = one; |
| | | this.updateById(appUser); |
| | | } else { |
| | | // 不存在账户,创建账户 |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); |
| | | sysUser.setUserName(nickname); |
| | | sysUser.setUserType("01"); // app用户 |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setAppleId(appleId); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | appUser.setNickname(nickname); |
| | | appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像 |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setDevice(device); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | appLoginUser.setBindStatus(DisabledEnum.YES.getCode()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH" + format + randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | }else{ |
| | | // 不存在账户,创建账户 |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); |
| | | sysUser.setUserName(nickname); |
| | | sysUser.setUserType("01"); // app用户 |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setAppleId(appleId); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | appUser.setNickname(nickname); |
| | | appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像 |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setDevice(device); |
| | | |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | appLoginUser.setBindStatus(DisabledEnum.YES.getCode()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH" + format + randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | } |
| | | appLoginUser.setUserid(sysUser.getUserId()); |
| | | appLoginUser.setUsername(appUser.getNickname()); |
| | | appLoginUser.setAppUserId(appUser.getId()); |
| | | appLoginUser.setSysUser(sysUser); |
| | | return appLoginUser; |
| | | } |
| | | |
| | | /** |
| | | * 验证码登录 |
| | | * |
| | | * @param body |
| | | * @return |
| | | */ |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public AppLoginUser appCaptchaLogin (AppCaptchaBody body){ |
| | | AppLoginUser appLoginUser = new AppLoginUser(); |
| | | Optional<AppUser> optionalAppUser = this.getUserByCondition( |
| | | AppUserDTO.builder().device(body.getDevice()).cellPhone(body.getCellPhone()).build()); |
| | | AppUser appUser = new AppUser(); |
| | | SysUser sysUser = new SysUser(); |
| | | if (optionalAppUser.isPresent()) { |
| | | appUser = optionalAppUser.get(); |
| | | sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData(); |
| | | } else { |
| | | if (org.springframework.util.StringUtils.hasLength(body.getDevice())) { |
| | | AppUser one = this.getOne(new LambdaQueryWrapper<>(AppUser.class) |
| | | .eq(AppUser::getDevice, body.getDevice()) |
| | | .isNull(AppUser::getCellPhone)); |
| | | if (one != null) { |
| | | initUserTree(appUser.getId()); |
| | | appUser = one; |
| | | } else { |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); |
| | | sysUser.setUserName(body.getCellPhone()); |
| | | sysUser.setUserType("01"); // app用户 |
| | | // 设置默认密码123456 |
| | | sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD)); |
| | | |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | appUser.setNickname(nickname); |
| | | appUser.setCellPhone(body.getCellPhone()); |
| | | appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像 |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH" + format + randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | } else { |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); |
| | | sysUser.setUserName(body.getCellPhone()); |
| | | sysUser.setUserType("01"); // app用户 |
| | | // 设置默认密码123456 |
| | | sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD)); |
| | | |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | appUser.setNickname(nickname); |
| | | appUser.setCellPhone(body.getCellPhone()); |
| | | appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像 |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH" + format + randomCode); |
| | | this.save(appUser); |
| | | remoteMeditationService.addHomeBackgroundMusicDefault(appUser.getId()); |
| | | |
| | | // 初始化用户树苗 |
| | | initUserTree(appUser.getId()); |
| | | } |
| | | } |
| | | appLoginUser.setUserid(sysUser.getUserId()); |
| | | appLoginUser.setUsername(appUser.getNickname()); |
| | | appLoginUser.setAppUserId(appUser.getId()); |
| | | appLoginUser.setSysUser(sysUser); |
| | | appLoginUser.setCellPhone(appUser.getCellPhone()); |
| | | return appLoginUser; |
| | | } |
| | | |
| | | /** |
| | | * 初始化用户树苗 |
| | | * |
| | | * @param appUserId 用户id |
| | | */ |
| | | private void initUserTree(Long appUserId) { |
| | | if (appUserTreeService.lambdaQuery().eq(AppUserTree::getSowAgain,2).eq(AppUserTree::getAppUserId,appUserId).one()!=null){ |
| | | return; |
| | | } |
| | | AppUserTree appUserTree = new AppUserTree(); |
| | | appUserTree.setAppUserId(appUserId); |
| | | appUserTree.setTreeLevelType(TreeLevelEnum.SEED.getCode()); |
| | | appUserTree.setGrowthValue(0); |
| | | appUserTree.setSowAgain(2); |
| | | appUserTree.setTime(LocalDateTime.now()); |
| | | appUserTreeService.save(appUserTree); |
| | | } |
| | | |
| | | /** |
| | | * 微信苹果登录验证手机号码操作 |
| | | * |
| | | * @param body |
| | | * @return |
| | | */ |
| | | @Override |
| | | public AppLoginUser verifyCellPhone(AppVerifyCellPhoneBody body) { |
| | | Integer loginType = body.getLoginType(); |
| | | String wxOrAppleId = body.getWxOrAppleId(); |
| | | SysUser sysUser; |
| | | AppUser appUser; |
| | | // 先通过手机号查询用户,如果用户先使用手机号码登录过,那么就已经存在这个手机号码的用户了。 |
| | | Optional<AppUser> baseOptionalAppUser = this.getUserByCondition( |
| | | AppUserDTO.builder().cellPhone(body.getCellPhone()).build()); |
| | | // 如果存在 更新wxOpenId 或appleId |
| | | if (baseOptionalAppUser.isPresent()) { |
| | | appUser = baseOptionalAppUser.get(); |
| | | if (appUser.getWxOpenId() !=null){ |
| | | throw new ServiceException("该手机号已绑定其他微信号,请更换手机号重新绑定"); |
| | | } |
| | | if (appUser.getAppleId() !=null){ |
| | | throw new ServiceException("该手机号已绑定其他苹果号,请更换手机号重新绑定"); |
| | | } |
| | | switch (body.getLoginType()){ |
| | | case 1: |
| | | this.remove(new LambdaQueryWrapper<AppUser>() |
| | | .eq(AppUser::getWxOpenId,body.getWxOrAppleId()) |
| | | .ne(AppUser::getId,appUser.getId())); |
| | | break; |
| | | case 2: |
| | | this.remove(new LambdaQueryWrapper<AppUser>() |
| | | .eq(AppUser::getAppleId,body.getWxOrAppleId()) |
| | | .ne(AppUser::getId,appUser.getId())); |
| | | break; |
| | | } |
| | | |
| | | if (loginType.equals(1)) { |
| | | appUser.setWxOpenId(wxOrAppleId); |
| | | } else { |
| | | appUser.setAppleId(wxOrAppleId); |
| | | } |
| | | appUser.setEmail(body.getEmail()); |
| | | appUser.setNickname(body.getName()); |
| | | // 查询系统用户 |
| | | sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData(); |
| | | if (Objects.isNull(sysUser)) { |
| | | throw new ServiceException("系统用户不存在"); |
| | | } |
| | | } else { |
| | | Optional<AppUser> userOptional = getAppUserByThirdId(loginType, wxOrAppleId); |
| | | if (!userOptional.isPresent()) { |
| | | if (loginType.equals(1)) { |
| | | // 微信登录 |
| | | handleUserNotPresent(userOptional, "该用户不存在,请重新进行微信授权登录"); |
| | | } else { |
| | | // 苹果登录 |
| | | handleUserNotPresent(userOptional, "该用户不存在,请重新进行苹果授权登录"); |
| | | } |
| | | } |
| | | appUser = userOptional.get(); |
| | | appUser.setCellPhone(body.getCellPhone()); |
| | | appUser.setEmail(body.getEmail()); |
| | | appUser.setNickname(body.getName()); |
| | | // 更新系统用户 |
| | | sysUser = sysUserClient.getSysUser(appUser.getUserId()).getData(); |
| | | if (Objects.isNull(sysUser)) { |
| | | throw new ServiceException("系统用户不存在"); |
| | | } |
| | | sysUser.setUserName(body.getCellPhone()); |
| | | sysUser.setPassword( |
| | | SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD)) |
| | | ; // 默认密码为a12345678 |
| | | sysUser.setPhonenumber(body.getCellPhone()); |
| | | sysUserClient.updateSysUser(sysUser); |
| | | } |
| | | // 更新用户信息 |
| | | this.updateById(appUser); |
| | | AppLoginUser appLoginUser = new AppLoginUser(); |
| | | appLoginUser.setAppUserId(appUser.getId()); |
| | | appLoginUser.setBindStatus(1); |
| | | appLoginUser.setCellPhone(body.getCellPhone()); |
| | | appLoginUser.setSysUser(sysUser); |
| | | return appLoginUser; |
| | | } |
| | | // 定义一个方法来处理用户不存在的情况 |
| | | private void handleUserNotPresent(Optional<AppUser> userOptional, String errorMessage) { |
| | | if (!userOptional.isPresent()) { |
| | | throw new ServiceException(errorMessage); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 根据第三方id获取未注销用户 |
| | | * |
| | | * @param loginType 登录类型 1=微信 2=苹果 |
| | | * @param wxOrAppleId 微信openid或苹果appleId |
| | | * @return Optional<AppUser> |
| | | */ |
| | | private Optional<AppUser> getAppUserByThirdId(Integer loginType, String wxOrAppleId) { |
| | | return this.lambdaQuery() |
| | | .eq(loginType.equals(1), AppUser::getWxOpenId, wxOrAppleId) |
| | | .eq(loginType.equals(2), AppUser::getAppleId, wxOrAppleId) |
| | | .ne(AppUser::getUserStatus, AppUserStatusEnum.LOGOUT.getCode()) |
| | | .oneOpt(); |
| | | } |
| | | |
| | | /** |
| | | * 保存计划引导页用户的答案 |
| | | * |
| | | * @param dto 用户计划引导答案数据传输对象 |
| | | */ |
| | | @Override |
| | | public void saveUserAnswers(UserAnswerDTO dto) { |
| | | if (tokenService.getLoginUser()==null){ |
| | | // 获取当前登录用户 |
| | | if (org.springframework.util.StringUtils.hasLength(dto.getDevice())){ |
| | | AppUser one = this.lambdaQuery().eq(AppUser::getDevice, dto.getDevice()) |
| | | .isNull(AppUser::getCellPhone).one(); |
| | | if (one==null){ |
| | | SysUser sysUser = new SysUser(); |
| | | AppUser appUser = new AppUser(); |
| | | // 注册用户信息 |
| | | sysUser = new SysUser(); |
| | | String nickname = "用户" + IdUtil.fastSimpleUUID().substring(0, 6); |
| | | sysUser.setNickName(nickname); |
| | | sysUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); |
| | | sysUser.setUserName(nickname); |
| | | sysUser.setUserType("01"); // app用户 |
| | | // 设置默认密码123456 |
| | | sysUser.setPassword(SecurityUtils.encryptPassword(Constants.DEFAULT_PASSWORD)); |
| | | |
| | | R<SysUser> registerResult = remoteUserService.registerAppUserInfo(sysUser, |
| | | SecurityConstants.INNER); |
| | | if (registerResult.getCode() == Constants.FAIL) { |
| | | throw new ServiceException(registerResult.getMsg()); |
| | | } |
| | | SysUser sysUserRes = registerResult.getData(); |
| | | appUser = new AppUser(); |
| | | appUser.setUserId(sysUserRes.getUserId()); |
| | | appUser.setNickname(nickname); |
| | | appUser.setAvatar("https://xqgwzh.obs.cn-south-1.myhuaweicloud.com/Logo%E7%A1%AE%E8%AE%A4%E7%89%88%281%29.jpg"); // TODO 待完善默认头像 |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setSanskritFlag(DisabledEnum.NO.getCode()); |
| | | appUser.setBalance(BigDecimal.ZERO); |
| | | appUser.setIncome(BigDecimal.ZERO); |
| | | appUser.setTotalEnergyValue(0); |
| | | appUser.setRegisterTime(LocalDateTime.now()); |
| | | appUser.setLevelSettingId(TreeLevelEnum.SEED.getCode()); |
| | | appUser.setDevice(dto.getDevice()); |
| | | SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); |
| | | String format = simpleDateFormat.format(new Date()); |
| | | // 生成四位随机数字 |
| | | String randomCode = String.valueOf((int) ((Math.random() * 9 + 1) * 1000)); |
| | | appUser.setCode("YH"+format+randomCode); |
| | | this.save(appUser); |
| | | Long userId = appUser.getId(); |
| | | List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList( |
| | | dto.getUserAnswerOneDTOList(), AppUserQuestion.class); |
| | | // 查询用户已填写的答案 |
| | | List<AppUserQuestion> baseAppUserQuestionList = appUserQuestionService.lambdaQuery() |
| | | .eq(AppUserQuestion::getAppUserId, appUser.getUserId()) |
| | | .list(); |
| | | if (CollUtils.isNotEmpty(baseAppUserQuestionList)) { |
| | | // 根据类型更新答案 |
| | | for (AppUserQuestion appUserQuestion : appUserQuestionList) { |
| | | |
| | | Optional<AppUserQuestion> baseAppUserQuestionOptional = baseAppUserQuestionList.stream() |
| | | .filter(baseAppUserQuestion -> baseAppUserQuestion.getType() |
| | | .equals(appUserQuestion.getType())).findFirst(); |
| | | baseAppUserQuestionOptional.ifPresent( |
| | | userQuestion -> appUserQuestion.setId(userQuestion.getId())); |
| | | appUserQuestion.setAppUserId(userId); |
| | | } |
| | | } |
| | | for (AppUserQuestion appUserQuestion : appUserQuestionList) { |
| | | appUserQuestion.setAppUserId(userId); |
| | | } |
| | | appUserQuestionService.saveOrUpdateBatch(appUserQuestionList); |
| | | // 筛选答案为是和偶尔有的问题,为用户设置对应的标签 |
| | | List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream() |
| | | .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1") |
| | | || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList()); |
| | | appUser.setTagId(dto.getTagIds()); |
| | | this.updateById(appUser); |
| | | }else{ |
| | | List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList( |
| | | dto.getUserAnswerOneDTOList(), AppUserQuestion.class); |
| | | // 查询用户已填写的答案 |
| | | // 删除之前答案 |
| | | appUserQuestionService.lambdaUpdate().eq(AppUserQuestion::getAppUserId,one.getId()).remove(); |
| | | |
| | | // 根据类型更新答案 |
| | | for (AppUserQuestion appUserQuestion : appUserQuestionList) { |
| | | appUserQuestion.setAppUserId(one.getId()); |
| | | } |
| | | appUserQuestionService.saveBatch(appUserQuestionList); |
| | | // 筛选答案为是和偶尔有的问题,为用户设置对应的标签 |
| | | List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream() |
| | | .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1") |
| | | || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList()); |
| | | one.setTagId(dto.getTagIds()); |
| | | this.updateById(one); |
| | | } |
| | | |
| | | } |
| | | }else{ |
| | | Long userId = tokenService.getLoginUser().getUserid(); |
| | | AppUser appUser = this.getById(userId); |
| | | List<AppUserQuestion> appUserQuestionList = BeanUtils.copyList( |
| | | dto.getUserAnswerOneDTOList(), AppUserQuestion.class); |
| | | // 查询用户已填写的答案 |
| | | // 删除之前答案 |
| | | appUserQuestionService.lambdaUpdate().eq(AppUserQuestion::getAppUserId,userId).remove(); |
| | | |
| | | // 根据类型更新答案 |
| | | for (AppUserQuestion appUserQuestion : appUserQuestionList) { |
| | | appUserQuestion.setAppUserId(userId); |
| | | } |
| | | appUserQuestionService.saveBatch(appUserQuestionList); |
| | | // 筛选答案为是和偶尔有的问题,为用户设置对应的标签 |
| | | List<AppUserQuestion> appUserQuestionYesList = appUserQuestionList.stream() |
| | | .filter(appUserQuestion -> appUserQuestion.getAnswer().equals("1") |
| | | || appUserQuestion.getAnswer().equals("3")).collect(Collectors.toList()); |
| | | appUser.setTagId(dto.getTagIds()); |
| | | this.updateById(appUser); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取AppUser |
| | | * |
| | | * @param userId 系统用户userId |
| | | * @return AppUser |
| | | */ |
| | | private AppUser getUserBySysUserId(Long userId) { |
| | | Optional<AppUser> appUserOptional = this.lambdaQuery().eq(AppUser::getUserId, userId) |
| | | .oneOpt(); |
| | | if (!appUserOptional.isPresent()) { |
| | | throw new ServiceException("获取用户信息失败,请重新登录"); |
| | | } |
| | | return appUserOptional.get(); |
| | | } |
| | | |
| | | /** |
| | | * 获取问题二的标签列表 |
| | | * |
| | | * @return List<TagVO> |
| | | */ |
| | | @Override |
| | | public List<TagVO> getTagList() { |
| | | List<Tag> list = tagService.list(Wrappers.lambdaQuery(Tag.class).eq(Tag::getTagType, 2)); |
| | | return BeanUtils.copyList(list, TagVO.class); |
| | | } |
| | | |
| | | /** |
| | | * 更新APP用户信息 |
| | | * |
| | | * @param dto APP用户数据传输对象 |
| | | */ |
| | | @Override |
| | | public void updateAppUser(AppUserDTO dto) { |
| | | // 拷贝数据 |
| | | AppUser appUser = BeanUtils.copyBean(dto, AppUser.class); |
| | | this.updateById(appUser); |
| | | } |
| | | |
| | | @Autowired |
| | | private TokenService tokenService; |
| | | /** |
| | | * 获取当前登录用户信息 |
| | | * |
| | | * @return 用户信息 |
| | | * @see AppUserVO |
| | | */ |
| | | @Override |
| | | public AppUserVO getCurrentUser() { |
| | | LoginUser loginUser = tokenService.getLoginUser(); |
| | | |
| | | Long userId = loginUser.getUserid(); |
| | | AppUser appUser = this.getById(userId); |
| | | return BeanUtils.copyBean(appUser, AppUserVO.class); |
| | | } |
| | | } |