package vvpkassistant.User.service; import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.temp.SaTempUtil; import cn.hutool.core.bean.BeanUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import vvpkassistant.Data.WxChatParam; import vvpkassistant.Tools.BcryptUtils; import vvpkassistant.Tools.VVTools; import vvpkassistant.User.mapper.UserDao; import vvpkassistant.User.model.DTO.UserModelDTO; import vvpkassistant.User.model.UserModel; import vvpkassistant.User.model.UserModelVO; import vvpkassistant.common.ErrorCode; import vvpkassistant.exception.BusinessException; import vvpkassistant.mail.service.MailService; import javax.annotation.Resource; /* * @author: ziin * @date: 2025/8/4 16:19 */ @Service public class UserServiceImpl extends ServiceImpl implements UserService { @Resource private UserDao userDao; @Resource private WxChatParam wxChatParam; @Autowired private MailService mailService; @Override public UserModelVO loginWithMail(UserModelDTO model) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UserModel::getEmail,model.getEmail()) .in(UserModel::getStatus, 0,2); UserModel userModel = userDao.selectOne(lambdaQueryWrapper); if (userModel == null) { throw new BusinessException(ErrorCode.USER_DOES_NOT_EXIST); } String password = userModel.getPassword(); UserModelVO userModelVO = BeanUtil.copyProperties(userModel, UserModelVO.class); if (BcryptUtils.matchPassword(password, model.getPassword())) { StpUtil.login(userModel.getId()); userModelVO.setToken(StpUtil.getTokenValue()); userModelVO.setChatInfo(wxChatParam); return userModelVO; }else { throw new BusinessException(ErrorCode.PASSWORD_ERROR); } } @Override public UserModelVO updateUserInfo(UserModelDTO userModelDTO) { UserModel userInfo = userDao.selectById(userModelDTO.getId()); if (userInfo == null) { throw new BusinessException(ErrorCode.USER_DOES_NOT_EXIST); } // 用户没有密码的情况下设置密码 if (userInfo.getPassword() == null) { if (!userModelDTO.getNewPassword().isEmpty()){ if (userModelDTO.getNewPassword().length()<6){ throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度不能小于 6 位"); } userModelDTO.setPassword(BcryptUtils.encryptPassword(userModelDTO.getNewPassword())); } } if (userModelDTO.getEmail() != null) { mailService.sendVerificationMail(userModelDTO.getEmail(), userModelDTO.getId()); } // 用户有密码的情况下重新设置密码 if (userInfo.getPassword() != null && userModelDTO.getOldPassword() != null) { if (BcryptUtils.matchPassword(userModelDTO.getOldPassword(), userInfo.getPassword())) { userModelDTO.setPassword(BcryptUtils.encryptPassword(userModelDTO.getNewPassword())); }else { throw new BusinessException(ErrorCode.PASSWORD_ERROR,"旧密码不正确"); } } UserModel userModel = new UserModel(); BeanUtil.copyProperties(userModelDTO, userModel); int i = userDao.updateById(userModel); // 返回结果 userDao.selectById(userModel.getId()); UserModelVO userModelVO = BeanUtil.copyProperties(userModel, UserModelVO.class); userModelVO.setNewAccount(false); if (i == 1){ return userModelVO; }else { throw new BusinessException(ErrorCode.SYSTEM_ERROR); } } @Override public UserModelVO addUserWithMail(UserModelDTO userModelDTO) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(UserModel::getEmail,userModelDTO.getEmail()); UserModel userModel = userDao.selectOne(lambdaQueryWrapper); if (userModel != null) { throw new BusinessException(ErrorCode.MAIL_ALREADY_EXIST); } if (userModelDTO.getPassword().length() < 6 ){ throw new BusinessException(ErrorCode.PARAMS_ERROR,"密码长度不能小于 6 位"); } userModelDTO.setPassword(BcryptUtils.encryptPassword(userModelDTO.getPassword())); userModelDTO.setCreateTime(VVTools.currentTimeStamp()); //设置状态为待验证 userModelDTO.setStatus(2); //设置积分为0 userModelDTO.setPoints(0); UserModel userModelEntity = BeanUtil.copyProperties(userModelDTO, UserModel.class); userModelEntity.setMailVerification(1); if ( userDao.insert(userModelEntity) != 1){ throw new BusinessException(ErrorCode.ADD_FAILED,"用户注册失败"); } mailService.sendMail(userModelDTO.getEmail(),userModelEntity.getId()); // 判断用户是否为邀请用户 if (userModelDTO.getInviterId() != null) { UserModel oldUser = userDao.selectById(userModelDTO.getInviterId()); oldUser.setPoints(oldUser.getPoints() + 10); userDao.updateById(oldUser); } UserModelVO userModelVO = BeanUtil.copyProperties(userModelEntity, UserModelVO.class); userModelVO.setNewAccount(true); userModelVO.setChatInfo(wxChatParam); return userModelVO; } @Override public Boolean activateAccount(String token) { Integer userId = SaTempUtil.parseToken(token, Integer.class); UserModel userModel = userDao.selectById(userId); if (userModel == null) { throw new BusinessException(ErrorCode.USER_DOES_NOT_EXIST); } userModel.setStatus(0); userModel.setMailVerification(0); if (userDao.updateById(userModel) == 1){ return true; }else { throw new BusinessException(ErrorCode.UPDATE_FAILED,"激活失败"); } } @Override public Boolean verificationMail(String token) { Integer userId = SaTempUtil.parseToken(token, Integer.class); UserModel userModel = userDao.selectById(userId); userModel.setMailVerification(0); if (userDao.updateById(userModel) == 1){ return true; } throw new BusinessException(ErrorCode.SYSTEM_ERROR,"邮箱验证失败"); } }