change package name,fixes error

This commit is contained in:
2025-08-05 14:44:03 +08:00
parent 9832336b89
commit 69700c8fe1
22 changed files with 46 additions and 39 deletions

View File

@@ -0,0 +1,150 @@
package com.onekeycall.videotablet.controller;
import com.onekeycall.videotablet.entity.User;
import com.onekeycall.videotablet.service.UserService;
import com.onekeycall.videotablet.utils.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
import java.util.Map;
import java.util.Objects;
@RestController
public class LoginController {
private final UserService userService;
private final AuthenticationManager authenticationManager;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Autowired
public LoginController(UserService userService, AuthenticationManager authenticationManager) {
this.userService = userService;
this.authenticationManager = authenticationManager;
}
@PostMapping("/public/register")
public ResponseEntity<?> registerUser(@RequestBody RegisterRequest registerRequest) {
try {
userService.registerUser(registerRequest.getUsername(), registerRequest.getPassword());
return new ResponseEntity<>("User registered successfully", HttpStatus.CREATED);
} catch (RuntimeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
@PostMapping("/public/login")
public ResponseEntity<?> login() {
// 登录逻辑由Spring Security自动处理
return ResponseEntity.ok("Login successful");
}
// 注册请求参数类
public static class RegisterRequest {
private String username;
private String password;
// Getters and Setters
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
@PostMapping("/public/registerByPhone")
public ResponseEntity<?> registerByPhone(@RequestBody PhoneRequest request) {
String requestVerifyKey = request.getVerifyKey();
if (TextUtils.isEmpty(requestVerifyKey)) {
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
}
String phone = request.getPhone();
Map<String, Object> map = (Map<String, Object>) redisTemplate.opsForValue().get(phone);
if (map != null) {
String verifyKey = (String) map.get("verifyKey");
if (!Objects.equals(verifyKey, requestVerifyKey)) {
return new ResponseEntity<>("verify key is not same", HttpStatus.BAD_REQUEST);
}
String code = map.get("code").toString();
if (!Objects.equals(code, request.getCode())) {
return new ResponseEntity<>("code is not same", HttpStatus.BAD_REQUEST);
}
try {
User user = userService.registerByPhone(request.getPhone(), request.getCode(), new Date());
return new ResponseEntity<>(user, HttpStatus.CREATED);
} catch (RuntimeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
} else {
return new ResponseEntity<>("verify key is expired", HttpStatus.BAD_REQUEST);
}
}
@PostMapping("/public/loginByPhone")
public ResponseEntity<?> loginByPhone(@RequestBody PhoneRequest request) {
String requestVerifyKey = request.getVerifyKey();
if (TextUtils.isEmpty(requestVerifyKey)) {
return new ResponseEntity<>("verify key is empty", HttpStatus.BAD_REQUEST);
}
try {
User user = userService.loginByPhone(request.getPhone(), request.getCode());
// 生成并返回JWT令牌实际项目中需要实现JWT逻辑
return ResponseEntity.ok("Login successful: " + user.getUsername());
} catch (RuntimeException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
}
}
public static class PhoneRequest {
private String phone;
private String code;
private String verifyKey;
// Getters and Setters
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getVerifyKey() {
return verifyKey;
}
public void setVerifyKey(String verifyKey) {
this.verifyKey = verifyKey;
}
}
}