登陆注册后端接口
Change-Id: Ida548e4f32a505259dd180a5ae1ff8989ccfc076
diff --git a/src/main/java/api/ApiController.java b/src/main/java/api/ApiController.java
index 41d9052..b8ee0c1 100644
--- a/src/main/java/api/ApiController.java
+++ b/src/main/java/api/ApiController.java
@@ -25,6 +25,7 @@
import entity.Seed;
import entity.User;
+import entity.Post;
import java.util.UUID;
@@ -308,4 +309,189 @@
return new ResponseEntity<>("{}", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
+
+ @Override
+ @CrossOrigin(origins = "*", allowedHeaders = "*")
+ public ResponseEntity<String> loginUser(
+ @RequestBody String requestBody
+ ) {
+ try {
+ // 解析前端发送的JSON数据 {email: xxx, password: xxx}
+ com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+
+ // 获取email和password字段
+ com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("email");
+ com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
+
+ if (emailNode == null || passwordNode == null) {
+ com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
+ errorJson.put("message", "缺少必要参数");
+ String jsonError = mapper.writeValueAsString(errorJson);
+ return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
+ }
+
+ String email = emailNode.asText();
+ String password = passwordNode.asText();
+
+ // 参数验证
+ if (email == null || email.trim().isEmpty() ||
+ password == null || password.trim().isEmpty()) {
+ com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
+ errorJson.put("message", "邮箱和密码不能为空");
+ String jsonError = mapper.writeValueAsString(errorJson);
+ return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST);
+ }
+
+ // 创建User对象进行登录验证
+ User user = new User();
+ user.email = email.trim();
+ user.password = password;
+
+ String userid = db1.LoginUser(user);
+ if (userid != null) {
+ com.fasterxml.jackson.databind.node.ObjectNode responseJson = mapper.createObjectNode();
+ responseJson.put("userId", userid);
+ responseJson.put("userid", userid);
+ responseJson.put("message", "登录成功");
+ String jsonResponse = mapper.writeValueAsString(responseJson);
+ return new ResponseEntity<>(jsonResponse, HttpStatus.OK);
+ } else {
+ // 返回JSON格式的错误信息
+ com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
+ errorJson.put("message", "登录失败,请检查账号密码");
+ String jsonError = mapper.writeValueAsString(errorJson);
+ return new ResponseEntity<>(jsonError, HttpStatus.UNAUTHORIZED);
+ }
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
+ try {
+ errorJson.put("message", "服务器内部错误");
+ String jsonError = mapper.writeValueAsString(errorJson);
+ return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (JsonProcessingException ex) {
+ return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode();
+ try {
+ errorJson.put("message", "服务器内部错误");
+ String jsonError = mapper.writeValueAsString(errorJson);
+ return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (JsonProcessingException ex) {
+ return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+ }
+
+ @Override
+ @CrossOrigin(origins = "*", allowedHeaders = "*")
+ public ResponseEntity<Integer> registerUser(
+ @RequestBody String requestBody
+ ) {
+ try {
+ System.out.println("Register request body: " + requestBody);
+ // 解析 JSON 数据
+ com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody);
+
+ // 安全地获取字段
+ com.fasterxml.jackson.databind.JsonNode usernameNode = jsonNode.get("username");
+ com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password");
+ com.fasterxml.jackson.databind.JsonNode inviteEmailNode = jsonNode.get("invite_email");
+
+ if (usernameNode == null || passwordNode == null || inviteEmailNode == null) {
+ return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 参数不完整
+ }
+
+ String username = usernameNode.asText();
+ String password = passwordNode.asText();
+ String inviteEmail = inviteEmailNode.asText();
+
+ // 参数验证
+ if (username == null || username.trim().isEmpty() ||
+ password == null || password.trim().isEmpty() ||
+ inviteEmail == null || inviteEmail.trim().isEmpty()) {
+ return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST);
+ }
+
+ // 创建 User 对象
+ User user = new User();
+ user.userid = java.util.UUID.randomUUID().toString(); // 生成唯一用户ID
+ user.username = username.trim();
+ user.password = password;
+ user.email = inviteEmail.trim(); // 使用邀请邮箱作为用户邮箱
+
+ // 设置默认值
+ user.sex = "m"; // 默认性别
+ user.school = ""; // 默认学校
+ user.pictureurl = ""; // 默认头像URL
+ user.profile = ""; // 默认个人简介
+ user.accountstate = false; // 默认账号状态为正常
+ user.invitetimes = 5; // 默认邀请次数
+
+ // 设置时间字段
+ user.lastDetectedTime = new java.util.Date();
+ user.fakeLastDetectedTime = new java.util.Date();
+
+ // 调用数据库注册方法
+ int ret = db1.RegisterUser(user);
+ System.out.println("Register result: " + ret);
+
+ // // 如果注册成功,还需要创建对应的 UserPT 记录
+ // if (ret == 0) {
+ // try {
+ // entity.UserPT userPT = new entity.UserPT();
+ // userPT.userid = user.userid;
+ // userPT.magic = 100; // 初始魔力值
+ // userPT.upload = 0L; // 初始上传量
+ // userPT.download = 0L; // 初始下载量
+ // userPT.share = 0.0; // 初始分享率
+ // userPT.farmurl = ""; // 默认做种路径
+ // userPT.viptime = 0; // 初始VIP次数
+ // userPT.user = user; // 设置关联
+
+ // int ptRet = db1.RegisterUserPT(userPT);
+ // if (ptRet != 0) {
+ // // 如果 UserPT 创建失败,记录日志但不影响主要注册流程
+ // System.err.println("Warning: Failed to create UserPT for user " + user.userid);
+ // }
+ // } catch (Exception e) {
+ // System.err.println("Warning: Exception creating UserPT: " + e.getMessage());
+ // }
+ // }
+
+ if (ret == 0) {
+ return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示注册成功
+ } else if (ret == 1) {
+ return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示邮箱重复
+ } else {
+ return new ResponseEntity<>(ret, HttpStatus.BAD_REQUEST); // 返回 2 表示未被邀请或其他错误
+ }
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 2 表示处理失败
+ } catch (Exception e) {
+ e.printStackTrace();
+ return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
+
+ @Override
+ public ResponseEntity<String> getForum() {
+ try {
+ Post[] posts = db1.GetPostList();
+ if (posts == null || posts.length == 0) {
+ return new ResponseEntity<>("[]", HttpStatus.OK); // 返回空数组表示没有帖子
+ }
+ String json = mapper.writeValueAsString(posts);
+ return new ResponseEntity<>(json, HttpStatus.OK);
+ } catch (JsonProcessingException e) {
+ e.printStackTrace();
+ return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR);
+ } catch (Exception e) {
+ e.printStackTrace();
+ return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR);
+ }
+ }
}
\ No newline at end of file
diff --git a/src/main/java/api/ApiInterface.java b/src/main/java/api/ApiInterface.java
index 2b4a3a3..4a2c02e 100644
--- a/src/main/java/api/ApiInterface.java
+++ b/src/main/java/api/ApiInterface.java
@@ -62,4 +62,17 @@
ResponseEntity<String> getUserStat(
@RequestParam("userid") String userid
);
+
+ @PostMapping("/login")
+ ResponseEntity<String> loginUser(
+ @RequestBody String requestBody
+ );
+
+ @PostMapping("/register")
+ ResponseEntity<Integer> registerUser(
+ @RequestBody String requestBody
+ );
+
+ @GetMapping("/forum")
+ ResponseEntity<String> getForum();
}
\ No newline at end of file