package com.guwan.backend.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.guwan.backend.dto.user.LoginDto; import com.guwan.backend.dto.user.RegisterDTO; import com.guwan.backend.dto.user.UserDTO; import com.guwan.backend.entity.User; import com.guwan.backend.mapper.UserMapper; import com.guwan.backend.service.EmailService; import com.guwan.backend.service.UserService; import com.guwan.backend.service.VerificationService; import com.guwan.backend.util.JwtUtil; import com.guwan.backend.util.RedisUtil; import com.guwan.backend.util.RedisUtils; import com.guwan.backend.vo.user.EmailRegisterRequest; import com.guwan.backend.vo.user.PhoneRegisterRequest; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.BeanUtils; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import java.time.LocalDateTime; import java.util.concurrent.TimeUnit; @Slf4j @Service @RequiredArgsConstructor public class UserServiceImpl implements UserService { private final UserMapper userMapper; private final PasswordEncoder passwordEncoder; private final JwtUtil jwtUtil; private final RedisUtil redisUtil; private final RedisUtils redisUtils; private final VerificationService verificationService; private final EmailService emailService; private static final String USER_CACHE_KEY = "user:"; private static final long USER_CACHE_DURATION = 3600L; // 1小时 @Override @Transactional public UserDTO register(RegisterDTO request) { // 检查用户名是否已存在 if (findByUsername(request.getUsername()) != null) { throw new IllegalArgumentException("用户名已存在"); } // 检查邮箱是否已存在 if (findByEmail(request.getEmail()) != null) { throw new IllegalArgumentException("邮箱已被注册"); } // 检查手机号是否已存在 if (findByPhone(request.getPhone()) != null) { throw new IllegalArgumentException("手机号已被注册"); } // 校验邮箱验证码 String redisEmailCode = (String) redisUtil.get(request.getEmail()); if (!request.getEmailCode().equals(redisEmailCode)) { throw new IllegalArgumentException("邮箱验证码错误"); } // 校验手机号验证码 String redisPhoneCode = (String) redisUtil.get(request.getPhone()); if (!request.getPhoneCode().equals(redisPhoneCode)) { throw new IllegalArgumentException("手机验证码错误"); } User user = new User(); BeanUtils.copyProperties(request, user); user.setPassword(passwordEncoder.encode(request.getPassword())); user.setPhone(request.getPhone()); user.setEmail(request.getEmail()); user.setStatus(1); userMapper.insert(user); redisUtil.delete(request.getEmail()); redisUtil.delete(request.getPhone()); return convertToDTO(user); } @Override public UserDTO login(LoginDto request) { User user = null; System.out.println("request = " + request); if (request.getActiveTab().equals("account")) { user = userMapper.selectOne(new LambdaQueryWrapper().eq(User::getUsername, request.getUsername())); if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) { throw new IllegalArgumentException("用户名或密码错误"); } } if (request.getActiveTab().equals("phone")) { user = userMapper.selectOne(new LambdaQueryWrapper().eq(User::getPhone, request.getPhone())); String redisPhoneCode = (String) redisUtil.get(request.getPhone()); if (user == null) { throw new IllegalArgumentException("用户未注册"); } if (!request.getCode().equals(redisPhoneCode)){ throw new IllegalArgumentException("验证码错误"); }else { redisUtil.delete(request.getPhone()); } } if (request.getActiveTab().equals("email")) { user = userMapper.selectOne(new LambdaQueryWrapper().eq(User::getEmail, request.getEmail())); String redisEmailCode = (String) redisUtil.get(request.getEmail()); if (user == null) { throw new IllegalArgumentException("用户未注册"); } if (!request.getCode().equals(redisEmailCode)){ throw new IllegalArgumentException("验证码错误"); }else { redisUtil.delete(request.getEmail()); } } if (user.getStatus() != 1) { throw new IllegalArgumentException("账号已被禁用"); } // 更新最后登录时间 user.setLastLoginTime(LocalDateTime.now()); userMapper.updateById(user); UserDTO userDTO = convertToDTO(user); String token = jwtUtil.generateToken(user.getId()); userDTO.setToken(token); // 缓存用户信息 redisUtils.set(USER_CACHE_KEY + user.getId(), userDTO, USER_CACHE_DURATION); return userDTO; } @Override public UserDTO getCurrentUser() { Long userId = getCurrentUserId(); if (userId == null) { return null; } return getUserById(userId); } @Override public Long getCurrentUserId() { // TODO: 从SecurityContext中获取当前用户ID return null; } @Override public UserDTO getUserById(Long id) { // 先从缓存获取 Object cached = redisUtil.get(USER_CACHE_KEY + id); if (cached != null) { return (UserDTO) cached; } User user = userMapper.selectById(id); if (user == null) { return null; } UserDTO userDTO = convertToDTO(user); redisUtil.set(USER_CACHE_KEY + id, userDTO, USER_CACHE_DURATION, TimeUnit.SECONDS); return userDTO; } @Override public UserDTO updateUserInfo(UserDTO userDTO) { return null; } @Override public void changePassword(String oldPassword, String newPassword) { } @Override public void resetPassword(String email) { } @Override public String refreshToken(String token) { return null; } @Override public UserDTO registerByEmail(EmailRegisterRequest request) { return null; } @Override public UserDTO registerByPhone(PhoneRegisterRequest request) { return null; } @Override public void sendEmailCode(String email) { } @Override public void sendPhoneCode(String phone) { } private User findByUsername(String username) { return userMapper.selectOne( new LambdaQueryWrapper() .eq(User::getUsername, username) ); } private User findByEmail(String email) { return userMapper.selectOne( new LambdaQueryWrapper() .eq(User::getEmail, email) ); } private User findByPhone(String phone) { return userMapper.selectOne( new LambdaQueryWrapper() .eq(User::getPhone, phone) ); } private UserDTO convertToDTO(User user) { if (user == null) { return null; } UserDTO dto = new UserDTO(); BeanUtils.copyProperties(user, dto); return dto; } }