package com.guwan.backend.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; 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.vo.user.*; 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 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("手机号已被注册"); } User user = new User(); BeanUtils.copyProperties(request, user); user.setPassword(passwordEncoder.encode(request.getPassword())); user.setCreatedTime(LocalDateTime.now()); user.setStatus(1); userMapper.insert(user); return convertToDTO(user); } @Override public UserDTO login(LoginRequest request) { User user = findByUsername(request.getUsername()); if (user == null || !passwordEncoder.matches(request.getPassword(), user.getPassword())) { throw new IllegalArgumentException("用户名或密码错误"); } 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); // 缓存用户信息 redisUtil.set(USER_CACHE_KEY + user.getId(), userDTO, USER_CACHE_DURATION, TimeUnit.SECONDS); 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; } }