by-crm/backend/src/main/java/com/bycrm/service/impl/UserServiceImpl.java
2026-01-26 16:01:15 +08:00

125 lines
4.2 KiB
Java

package com.bycrm.service.impl;
import com.bycrm.common.Constants;
import com.bycrm.dto.ChangePasswordDTO;
import com.bycrm.dto.ResetPasswordDTO;
import com.bycrm.entity.User;
import com.bycrm.exception.BusinessException;
import com.bycrm.mapper.UserMapper;
import com.bycrm.service.UserService;
import com.bycrm.util.JwtUtil;
import com.bycrm.vo.UserInfoVO;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
/**
* 用户服务实现
*/
@Service
public class UserServiceImpl implements UserService {
private final UserMapper userMapper;
private final JwtUtil jwtUtil;
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
public UserServiceImpl(UserMapper userMapper, JwtUtil jwtUtil) {
this.userMapper = userMapper;
this.jwtUtil = jwtUtil;
}
@Override
public String login(com.bycrm.dto.LoginDTO loginDTO) {
User user = userMapper.selectByUsername(loginDTO.getUsername());
if (user == null) {
throw new BusinessException("用户名或密码错误");
}
if (user.getStatus() == Constants.USER_STATUS_DISABLED) {
throw new BusinessException("用户已被禁用");
}
if (!passwordEncoder.matches(loginDTO.getPassword(), user.getPassword())) {
throw new BusinessException("用户名或密码错误");
}
return jwtUtil.generateToken(user.getId(), user.getUsername(), user.getRole(), user.getDealerId());
}
@Override
public User getUserById(Long id) {
return userMapper.selectById(id);
}
@Override
public User getUserByUsername(String username) {
return userMapper.selectByUsername(username);
}
@Override
public UserInfoVO getCurrentUser(String token) {
Long userId = jwtUtil.getUserIdFromToken(token);
User user = userMapper.selectById(userId);
if (user == null) {
throw new BusinessException("用户不存在");
}
UserInfoVO vo = new UserInfoVO();
vo.setUserId(user.getId());
vo.setUsername(user.getUsername());
vo.setRealName(user.getRealName());
vo.setDealerId(user.getDealerId());
vo.setDealerName(user.getDealerName());
vo.setRole(user.getRole());
vo.setRoleDesc(user.getRole() == Constants.USER_ROLE_ADMIN ? "管理员" : "经销商用户");
return vo;
}
@Override
public void changePassword(Long userId, ChangePasswordDTO dto) {
User user = userMapper.selectById(userId);
if (user == null) {
throw new BusinessException("用户不存在");
}
// 验证原密码
if (!passwordEncoder.matches(dto.getOldPassword(), user.getPassword())) {
throw new BusinessException("原密码错误");
}
// 新密码不能与原密码相同
if (dto.getOldPassword().equals(dto.getNewPassword())) {
throw new BusinessException("新密码不能与原密码相同");
}
// 加密新密码并更新
String encodedPassword = passwordEncoder.encode(dto.getNewPassword());
user.setPassword(encodedPassword);
userMapper.update(user);
}
@Override
public void resetPassword(ResetPasswordDTO dto) {
User user = userMapper.selectById(dto.getUserId());
if (user == null) {
throw new BusinessException("用户不存在");
}
// 管理员不能重置自己的密码(应使用修改密码功能)
if (user.getRole() == Constants.USER_ROLE_ADMIN) {
throw new BusinessException("不能重置管理员密码");
}
// 加密新密码并更新
String encodedPassword = passwordEncoder.encode(dto.getNewPassword());
user.setPassword(encodedPassword);
userMapper.update(user);
}
public static void main(String[] args) {
BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
String rawPassword = "59cce3254e6d76ec6bad2d84ae56b5da3e118b2f22d0d2b2d780356cbe3c0881";
String encodedPassword = passwordEncoder.encode(rawPassword);
System.out.println("Encoded Password: " + encodedPassword);
}
}