优化用户登录逻辑

This commit is contained in:
2025-08-15 09:24:01 +08:00
parent 37bf419e08
commit 8277e54662
3 changed files with 5 additions and 4 deletions

View File

@@ -37,9 +37,9 @@ public class LoginController {
@RequestParam(value = "user_id") String userId, @RequestParam String password) {
try {
userService.registerUser(userId, password);
return Result.ok().message("User registered successfully");
return Result.ok().message("User registered successfully");
} catch (RuntimeException e) {
return Result.error().message(e.getMessage());
return Result.error().message(e.getMessage());
}
}
@@ -67,7 +67,7 @@ public class LoginController {
@RequestParam String phone, @RequestParam String password) {
User user = userService.getUserByPhone(phone);
if (user == null) {
return Result.error().message("User not found with phone: " + phone);
return Result.error().message("手机号未注册");
}
String userId = user.getUserId();

View File

@@ -168,6 +168,7 @@ public class TencentSmsController {
map.put("expireAt", expireAt);
Map<String, Object> codeMap = new HashMap<>(map);
codeMap.put("code", code);
System.out.println(codeMap);
if (!sent) {
//4.保存验证码到Redis,并且设置有效期5分钟
redisTemplate.opsForValue().set(phone, codeMap, Duration.ofMinutes(5));

View File

@@ -31,7 +31,7 @@ public class UserService implements UserDetailsService {
}
public User getUserByPhone(String phone) {
return userRepository.findUserByPhone(phone).orElseThrow(() -> new RuntimeException("User not found with phone: " + phone));
return userRepository.findUserByPhone(phone).orElse(null);
}
public User registerUser(String userId, String password) {