119 lines
4.9 KiB
Java
119 lines
4.9 KiB
Java
package com.onekeycall.videotablet.service;
|
|
|
|
import com.onekeycall.videotablet.entity.User;
|
|
import com.onekeycall.videotablet.repository.UserRepository;
|
|
import com.onekeycall.videotablet.result.Result;
|
|
import com.onekeycall.videotablet.utils.AESUtil;
|
|
import com.onekeycall.videotablet.utils.SecureIdGenerator;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.data.redis.core.RedisTemplate;
|
|
import org.springframework.security.core.userdetails.UserDetails;
|
|
import org.springframework.security.core.userdetails.UserDetailsService;
|
|
import org.springframework.security.core.userdetails.UsernameNotFoundException;
|
|
import org.springframework.security.crypto.password.PasswordEncoder;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.Date;
|
|
import java.util.Map;
|
|
|
|
@Service
|
|
public class UserService implements UserDetailsService {
|
|
|
|
private final UserRepository userRepository;
|
|
private final PasswordEncoder passwordEncoder;
|
|
private final RedisTemplate<String, Object> redisTemplate;
|
|
|
|
@Autowired
|
|
public UserService(UserRepository userRepository, PasswordEncoder passwordEncoder, RedisTemplate<String, Object> redisTemplate) {
|
|
this.userRepository = userRepository;
|
|
this.passwordEncoder = passwordEncoder;
|
|
this.redisTemplate = redisTemplate;
|
|
}
|
|
|
|
public User getUserByPhone(String phone) {
|
|
return userRepository.findUserByPhone(phone).orElse(null);
|
|
}
|
|
|
|
public User registerUser(String userId, String password) {
|
|
if (userRepository.existsByUserId(userId)) {
|
|
throw new RuntimeException("Username already exists");
|
|
}
|
|
|
|
User user = new User();
|
|
user.setUsername(userId);
|
|
user.setPassword(passwordEncoder.encode(password));
|
|
|
|
return userRepository.save(user);
|
|
}
|
|
|
|
public User registerByPhone(String phone, String code, String deviceId, Date createTime) {
|
|
// 1. 验证验证码
|
|
Map<String, Object> codeMap = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
|
if (codeMap == null || !code.equals(codeMap.get("code").toString())) {
|
|
throw new RuntimeException("Invalid verification code");
|
|
}
|
|
|
|
// 2. 检查手机号是否已注册
|
|
if (userRepository.existsByPhone(phone)) {
|
|
User user = userRepository.findByPhone(phone).get();
|
|
user.setUserId(deviceId);
|
|
user.setNewUser(false);
|
|
return user;
|
|
} else {
|
|
// 3. 创建新用户
|
|
User user = new User();
|
|
user.setNewUser(true);
|
|
user.setPhone(phone);
|
|
user.setCreatTime(createTime);
|
|
user.setLastLoginTime(createTime);
|
|
user.setUpdateTime(createTime);
|
|
user.setUserId(SecureIdGenerator.generateSecureId(12));
|
|
user.setUsername(SecureIdGenerator.generateSecureUserName(8));
|
|
user.setDeviceId(deviceId);
|
|
|
|
return userRepository.save(user);
|
|
}
|
|
}
|
|
|
|
public User loginByPhone(String phone, String code) {
|
|
// 1. 验证验证码
|
|
Map<String, Object> codeMap = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
|
|
if (codeMap == null || !code.equals(codeMap.get("code").toString())) {
|
|
throw new RuntimeException("Invalid verification code");
|
|
}
|
|
|
|
// 2. 查询用户
|
|
return userRepository.findByPhone(phone)
|
|
.orElseThrow(() -> new RuntimeException("User not found with phone: " + phone));
|
|
}
|
|
|
|
@Override
|
|
public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
|
|
return userRepository.findByUserId(userId)
|
|
.orElseThrow(() -> new UsernameNotFoundException("User not found with userId: " + userId));
|
|
}
|
|
|
|
public void setPasswordByUserId(String userId, String password) {
|
|
User user = userRepository.findByUserId(userId)
|
|
.orElseThrow(() -> new UsernameNotFoundException("User not found with userId: " + userId));
|
|
user.setPassword(passwordEncoder.encode(password));
|
|
userRepository.save(user);
|
|
}
|
|
|
|
public Result changePassword(String userId, String oldPassword, String newPassword) {
|
|
User user = userRepository.findByUserId(userId)
|
|
.orElseThrow(() -> new UsernameNotFoundException("User not found with userId: " + userId));
|
|
if (!passwordEncoder.matches(oldPassword, user.getPassword())) {
|
|
return Result.error().message("Old password is incorrect");
|
|
} else {
|
|
user.setPassword(passwordEncoder.encode(newPassword));
|
|
userRepository.save(user);
|
|
return Result.ok().message("change password success");
|
|
}
|
|
}
|
|
|
|
public User getUserByUserId(String userId) {
|
|
return userRepository.findUserByUserId(userId).
|
|
orElseThrow(() -> new UsernameNotFoundException("User not found with userId: " + userId));
|
|
}
|
|
} |