基本实现手机验证码,账号密码登录。腾讯云短信后台显示成功,但是收不到,也没有报错

This commit is contained in:
2025-08-06 01:31:16 +08:00
parent 66480121b4
commit 940f1d7bac
8 changed files with 314 additions and 89 deletions

View File

@@ -2,6 +2,7 @@ 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.SecureIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
@@ -49,19 +50,23 @@ public class UserService implements UserDetailsService {
// 2. 检查手机号是否已注册
if (userRepository.existsByPhone(phone)) {
throw new RuntimeException("Phone number already registered");
}
User user = userRepository.findByPhone(phone).get();
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);
// 3. 创建新用户
User user = new User();
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);
return userRepository.save(user);
}
}
public User loginByPhone(String phone, String code) {
@@ -81,4 +86,23 @@ public class UserService implements UserDetailsService {
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");
}
}
}