- 新增 CategoryController、CollectionController、CommentController 等多个控制器类 - 实现了分类、收藏、评论、消息等功能的接口 - 优化了部分方法名称,提高了代码可读性
224 lines
8.3 KiB
Java
224 lines
8.3 KiB
Java
package com.example.copykamanotes.service.impl;
|
|
|
|
import com.example.copykamanotes.annotation.NeedLogin;
|
|
import com.example.copykamanotes.mapper.UserMapper;
|
|
import com.example.copykamanotes.model.base.ApiResponse;
|
|
import com.example.copykamanotes.model.base.Pagination;
|
|
import com.example.copykamanotes.model.dto.user.LoginRequest;
|
|
import com.example.copykamanotes.model.dto.user.RegisterRequest;
|
|
import com.example.copykamanotes.model.dto.user.UpdateUserRequest;
|
|
import com.example.copykamanotes.model.dto.user.UserQueryParam;
|
|
import com.example.copykamanotes.model.entity.User;
|
|
import com.example.copykamanotes.model.vo.user.AvatarVO;
|
|
import com.example.copykamanotes.model.vo.user.LoginUserVO;
|
|
import com.example.copykamanotes.model.vo.user.RegisterVO;
|
|
import com.example.copykamanotes.model.vo.user.UserVO;
|
|
import com.example.copykamanotes.scope.RequestScopeData;
|
|
import com.example.copykamanotes.service.EmailService;
|
|
import com.example.copykamanotes.service.FileService;
|
|
import com.example.copykamanotes.service.UserService;
|
|
import com.example.copykamanotes.utils.ApiResponseUtils;
|
|
import com.example.copykamanotes.utils.JwtUtil;
|
|
import com.example.copykamanotes.utils.PaginationUtils;
|
|
import lombok.RequiredArgsConstructor;
|
|
import lombok.extern.log4j.Log4j2;
|
|
import org.springframework.beans.BeanUtils;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
import java.util.Collections;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Log4j2
|
|
@Service
|
|
@RequiredArgsConstructor
|
|
public class UserServiceImpl implements UserService {
|
|
|
|
private final UserMapper userMapper;
|
|
private final PasswordEncoder passwordEncoder;
|
|
private final JwtUtil jwtUtil;
|
|
private final RequestScopeData requestScopeData;
|
|
private final FileService fileService;
|
|
private final EmailService emailService;
|
|
|
|
@Override
|
|
@Transactional(rollbackFor = Exception.class)
|
|
public ApiResponse<RegisterVO> register(RegisterRequest request) {
|
|
|
|
// 判断账号
|
|
User existingUser = userMapper.findByAccount(request.getAccount());
|
|
if (existingUser != null) {
|
|
return ApiResponseUtils.error("账号重复");
|
|
}
|
|
|
|
// 判断邮箱
|
|
if (request.getEmail() != null && !request.getEmail().isEmpty()) {
|
|
existingUser = userMapper.findByEmail(request.getEmail());
|
|
if (existingUser != null) {
|
|
return ApiResponseUtils.error("邮箱重复");
|
|
}
|
|
// if (registerRequest.getVerifyCode() == null || registerRequest.getVerifyCode().isEmpty()) {
|
|
// return ApiResponseUtil.error("验证码不能为空");
|
|
// }
|
|
//
|
|
// if (!emailService.verifyCode(registerRequest.getEmail(), "register", registerRequest.getVerifyCode())) {
|
|
// return ApiResponseUtil.error("验证码错误");
|
|
// }
|
|
}
|
|
|
|
User user = new User();
|
|
BeanUtils.copyProperties(request, user);
|
|
user.setPassword(passwordEncoder.encode(request.getPassword()));
|
|
// user.setEmailVerified(request.getEmail() != null && !request.getEmail().isEmpty());
|
|
|
|
try {
|
|
userMapper.insert(user);
|
|
String token = jwtUtil.generateToken(user.getUserId());
|
|
RegisterVO registerVO = new RegisterVO();
|
|
BeanUtils.copyProperties(user, registerVO);
|
|
userMapper.updateLastLoginAt(user.getUserId());
|
|
return ApiResponseUtils.success("注册成功", registerVO, token);
|
|
} catch (Exception e) {
|
|
log.error("注册失败", e);
|
|
return ApiResponseUtils.error("注册失败,请稍后再试");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<LoginUserVO> login(LoginRequest loginRequest) {
|
|
User user = null;
|
|
|
|
if (loginRequest.getAccount() != null && !loginRequest.getAccount().isEmpty()) {
|
|
user = userMapper.findByAccount(loginRequest.getAccount());
|
|
} else if (loginRequest.getEmail() != null && !loginRequest.getEmail().isEmpty()) {
|
|
user = userMapper.findByEmail(loginRequest.getEmail());
|
|
} else {
|
|
return ApiResponseUtils.error("账号或邮箱不能为空");
|
|
}
|
|
|
|
if (user == null) {
|
|
return ApiResponseUtils.error("账号或邮箱不存在");
|
|
}
|
|
|
|
if (!passwordEncoder.matches(loginRequest.getPassword(), user.getPassword())) {
|
|
return ApiResponseUtils.error("密码错误");
|
|
}
|
|
|
|
String token = jwtUtil.generateToken(user.getUserId());
|
|
|
|
LoginUserVO loginUserVO = new LoginUserVO();
|
|
BeanUtils.copyProperties(user, loginUserVO);
|
|
|
|
userMapper.updateLastLoginAt(user.getUserId());
|
|
|
|
return ApiResponseUtils.success("登录成功", loginUserVO, token);
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<LoginUserVO> whoami() {
|
|
Long userId = requestScopeData.getUserId();
|
|
|
|
if (userId == null) {
|
|
return ApiResponseUtils.error("未登录");
|
|
}
|
|
|
|
try {
|
|
User user = userMapper.findById(userId);
|
|
if (user == null) {
|
|
return ApiResponseUtils.error("用户不存在");
|
|
}
|
|
|
|
String newToken = jwtUtil.generateToken(userId);
|
|
if (newToken == null) {
|
|
return ApiResponseUtils.error("token生成失败");
|
|
}
|
|
|
|
LoginUserVO loginUserVO = new LoginUserVO();
|
|
BeanUtils.copyProperties(user, loginUserVO);
|
|
return ApiResponseUtils.success("获取用户信息成功", loginUserVO, newToken);
|
|
} catch (Exception e) {
|
|
log.error("获取用户信息失败", e);
|
|
return ApiResponseUtils.error("获取用户信息失败");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<UserVO> getUserInfo(Long userId) {
|
|
User user = userMapper.findById(userId);
|
|
|
|
if (user == null) {
|
|
return ApiResponseUtils.error("用户不存在");
|
|
}
|
|
|
|
UserVO userVO = new UserVO();
|
|
BeanUtils.copyProperties(user, userVO);
|
|
return ApiResponseUtils.success("获取用户信息成功", userVO);
|
|
}
|
|
|
|
@Override
|
|
@Transactional
|
|
@NeedLogin
|
|
public ApiResponse<LoginUserVO> updateUserInfo(UpdateUserRequest updateUserRequest) {
|
|
Long userId = requestScopeData.getUserId();
|
|
|
|
if (userId == null) {
|
|
return ApiResponseUtils.error("未登录");
|
|
}
|
|
|
|
User user = new User();
|
|
BeanUtils.copyProperties(updateUserRequest, user);
|
|
user.setUserId(userId);
|
|
|
|
try {
|
|
userMapper.update(user);
|
|
return ApiResponseUtils.success("更新用户信息成功");
|
|
} catch (Exception e) {
|
|
log.error("更新用户信息失败", e);
|
|
return ApiResponseUtils.error("更新用户信息失败");
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Map<Long, User> getUserMapByIds(List<Long> authorIds) {
|
|
if (authorIds.isEmpty()) return Collections.emptyMap();
|
|
|
|
List<User> users = userMapper.findByIdBatch(authorIds);
|
|
|
|
return users.stream().collect(Collectors.toMap(User::getUserId, user -> user));
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<List<User>> getUserList(UserQueryParam userQueryParam) {
|
|
|
|
// 分页数据
|
|
int total = userMapper.countByQueryParam(userQueryParam);
|
|
int offset = PaginationUtils.calculateOffset(userQueryParam.getPage(), userQueryParam.getPageSize());
|
|
Pagination pagination = new Pagination(userQueryParam.getPage(), userQueryParam.getPageSize(), total);
|
|
|
|
try {
|
|
List<User> users = userMapper.findByQueryParam(userQueryParam, userQueryParam.getPageSize(), offset);
|
|
|
|
return ApiResponseUtils.success("获取用户列表成功", users, pagination);
|
|
} catch (Exception e) {
|
|
return ApiResponseUtils.error(e.getMessage());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public ApiResponse<AvatarVO> uploadAvatar(MultipartFile file) {
|
|
try {
|
|
String url = fileService.uploadImage(file);
|
|
AvatarVO avatarVO = new AvatarVO();
|
|
avatarVO.setAvatarUrl(url);
|
|
return ApiResponseUtils.success("上传成功", avatarVO);
|
|
} catch (Exception e) {
|
|
log.error("上传失败", e);
|
|
return ApiResponseUtils.error("上传失败");
|
|
}
|
|
}
|
|
}
|