Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 1 | package api; |
| 2 | |
| 3 | import java.io.File; |
| 4 | |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 5 | import javax.annotation.PostConstruct; |
| 6 | |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 7 | import org.springframework.core.io.FileSystemResource; |
| 8 | import org.springframework.core.io.Resource; |
| 9 | import org.springframework.http.HttpHeaders; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 10 | import org.springframework.http.HttpStatus; |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 11 | import org.springframework.http.MediaType; |
| 12 | import org.springframework.http.ResponseEntity; |
| 13 | import org.springframework.web.bind.annotation.RequestParam; |
| 14 | import org.springframework.web.bind.annotation.RestController; |
| 15 | import org.springframework.web.multipart.MultipartFile; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 16 | import org.springframework.web.bind.annotation.CrossOrigin; |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 17 | import org.springframework.web.bind.annotation.RequestBody; |
| 18 | import org.springframework.web.bind.annotation.PostMapping; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 19 | |
| 20 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 21 | import com.fasterxml.jackson.databind.ObjectMapper; |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 22 | |
| 23 | import database.Database1; |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 24 | import tracker.Tracker; |
| 25 | |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 26 | import entity.Seed; |
| 27 | import entity.User; |
rhj | 46f62c4 | 2025-06-06 23:24:10 +0800 | [diff] [blame] | 28 | import entity.Post; |
rhj | 5b69b7e | 2025-06-07 01:28:08 +0800 | [diff] [blame] | 29 | import entity.PostReply; |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 30 | |
| 31 | import java.util.UUID; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 32 | |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 33 | @RestController |
| 34 | public class ApiController implements ApiInterface { |
| 35 | |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 36 | private static Database1 db1; |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 37 | private static Tracker tracker; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 38 | private static ObjectMapper mapper; |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 39 | private static HttpHeaders headers; |
| 40 | private static HttpHeaders errorHeaders; |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 41 | |
| 42 | @PostConstruct |
| 43 | public void init() { |
| 44 | db1 = new Database1(); |
| 45 | tracker = new Tracker(); |
| 46 | mapper = new ObjectMapper(); |
| 47 | mapper.configure(com.fasterxml.jackson.databind.SerializationFeature.FAIL_ON_EMPTY_BEANS, false); |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 48 | headers = new HttpHeaders(); |
| 49 | headers.add("Access-Control-Allow-Origin", "*"); |
| 50 | headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); |
| 51 | headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization"); |
| 52 | errorHeaders = new HttpHeaders(); |
| 53 | errorHeaders.add("Access-Control-Allow-Origin", "*"); |
| 54 | errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); |
| 55 | errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization"); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 56 | } |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 57 | |
| 58 | @Override |
| 59 | public ResponseEntity<Integer> saveTorrent( |
| 60 | @RequestParam("userid") String userid, |
| 61 | @RequestParam("title") String title, |
| 62 | @RequestParam("tag") String tag, |
| 63 | @RequestParam("file") MultipartFile file |
| 64 | ) { |
| 65 | try { |
| 66 | Seed seed = new Seed(); |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 67 | seed.seedid = UUID.randomUUID().toString(); // 生成唯一的种子ID |
| 68 | seed.seeduserid = userid; |
| 69 | seed.title = title; |
| 70 | seed.seedsize = "1GB"; |
| 71 | seed.seedtag = tag; |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 72 | seed.url = "http://example.com/torrent"; // 示例URL |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 73 | System.out.println("seed is null? " + (seed == null)); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 74 | int ret = db1.RegisterSeed(seed); |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 75 | if (ret != 0) { |
| 76 | // 如果注册种子失败,返回错误状态 |
| 77 | return new ResponseEntity<>(ret, headers, HttpStatus.INTERNAL_SERVER_ERROR); |
| 78 | } |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 79 | File tempFile = File.createTempFile(seed.seedid, file.getOriginalFilename()); |
| 80 | file.transferTo(tempFile); |
| 81 | tracker.SaveTorrent(seed.seedid, tempFile); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 82 | return new ResponseEntity<>(0, headers, HttpStatus.OK); // 返回 0 表示成功 |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 83 | } catch (Exception e) { |
| 84 | e.printStackTrace(); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 85 | return new ResponseEntity<>(1, errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示失败 |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 86 | } |
| 87 | } |
| 88 | |
| 89 | @Override |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 90 | @CrossOrigin(origins = "*", allowedHeaders = "*") // 允许所有来源和头部 |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 91 | public ResponseEntity<Resource> getTorrent( |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 92 | @RequestParam("torrentId") String seedid, |
| 93 | @RequestParam("userId") String userid |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 94 | ) { |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 95 | File file = tracker.GetTTorent(seedid, userid); |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 96 | if (file != null) { |
| 97 | FileSystemResource resource = new FileSystemResource(file); |
| 98 | return ResponseEntity.ok() |
| 99 | .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"") |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 100 | .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) // 关键:允许前端访问Content-Disposition |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 101 | .contentType(MediaType.APPLICATION_OCTET_STREAM) |
| 102 | .body(resource); |
| 103 | } else { |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 104 | return ResponseEntity.notFound().build(); |
| 105 | } |
| 106 | } |
| 107 | |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 108 | @Override |
| 109 | public ResponseEntity<String> getSeedListByTag( |
| 110 | @RequestParam("tag") String tag |
| 111 | ) { |
| 112 | try { |
| 113 | Seed[] seeds = db1.GetSeedListByTag(tag); |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 114 | if (seeds == null || seeds.length == 0) { |
| 115 | return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子 |
| 116 | } |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 117 | String json = mapper.writeValueAsString(seeds); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 118 | return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 119 | } catch (JsonProcessingException e) { |
| 120 | e.printStackTrace(); |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 121 | return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | @Override |
rhj | c6a4ee0 | 2025-06-06 00:45:18 +0800 | [diff] [blame] | 126 | public ResponseEntity<String> getUserProfile( |
| 127 | @RequestParam("userid") String userid |
| 128 | ) { |
| 129 | try { |
| 130 | User user = db1.GetInformation(userid); |
| 131 | if (user == null) { |
| 132 | return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 133 | } |
| 134 | String json = mapper.writeValueAsString(user); |
| 135 | return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 136 | } catch (JsonProcessingException e) { |
| 137 | e.printStackTrace(); |
| 138 | return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 144 | public ResponseEntity<Integer> changeProfile( |
| 145 | @RequestBody String requestBody |
| 146 | ) { |
| 147 | try { |
| 148 | // 解析 JSON 数据 |
| 149 | com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody); |
| 150 | |
| 151 | // 安全地获取 userid 字段 |
| 152 | com.fasterxml.jackson.databind.JsonNode useridNode = jsonNode.get("userid"); |
| 153 | if (useridNode == null) { |
| 154 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); |
| 155 | } |
| 156 | String userid = useridNode.asText(); |
| 157 | |
| 158 | // 添加参数验证 |
| 159 | if (userid == null || userid.trim().isEmpty()) { |
| 160 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); |
| 161 | } |
| 162 | |
| 163 | // 手动映射前端字段到 User 对象,处理类型转换 |
| 164 | User user = new User(); |
| 165 | user.userid = userid; |
| 166 | |
| 167 | // 安全地获取其他字段并进行类型转换 |
| 168 | if (jsonNode.has("username") && !jsonNode.get("username").isNull()) { |
| 169 | user.username = jsonNode.get("username").asText(); |
| 170 | } |
| 171 | if (jsonNode.has("school") && !jsonNode.get("school").isNull()) { |
| 172 | user.school = jsonNode.get("school").asText(); |
| 173 | } |
| 174 | if (jsonNode.has("gender") && !jsonNode.get("gender").isNull()) { |
| 175 | user.sex = jsonNode.get("gender").asText(); |
| 176 | } |
| 177 | if (jsonNode.has("avatar_url") && !jsonNode.get("avatar_url").isNull()) { |
| 178 | user.pictureurl = jsonNode.get("avatar_url").asText(); |
| 179 | } |
| 180 | |
| 181 | // 处理 account_status 的类型转换(字符串/数字 -> 布尔值) |
| 182 | if (jsonNode.has("account_status") && !jsonNode.get("account_status").isNull()) { |
| 183 | com.fasterxml.jackson.databind.JsonNode statusNode = jsonNode.get("account_status"); |
| 184 | if (statusNode.isTextual()) { |
| 185 | String statusStr = statusNode.asText(); |
| 186 | user.accountstate = "1".equals(statusStr) || "封禁".equals(statusStr); |
| 187 | } else if (statusNode.isNumber()) { |
| 188 | user.accountstate = statusNode.asInt() == 1; |
| 189 | } else if (statusNode.isBoolean()) { |
| 190 | user.accountstate = statusNode.asBoolean(); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | // 处理 invite_left 的类型转换(字符串 -> 整数) |
| 195 | if (jsonNode.has("invite_left") && !jsonNode.get("invite_left").isNull()) { |
| 196 | com.fasterxml.jackson.databind.JsonNode inviteNode = jsonNode.get("invite_left"); |
| 197 | if (inviteNode.isTextual()) { |
| 198 | try { |
| 199 | user.invitetimes = Integer.parseInt(inviteNode.asText()); |
| 200 | } catch (NumberFormatException e) { |
| 201 | user.invitetimes = 0; // 默认值 |
| 202 | } |
| 203 | } else if (inviteNode.isNumber()) { |
| 204 | user.invitetimes = inviteNode.asInt(); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | int ret = db1.UpdateInformation(user); |
| 209 | if (ret == 0) { |
| 210 | return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功 |
| 211 | } else { |
| 212 | return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败 |
| 213 | } |
| 214 | } catch (JsonProcessingException e) { |
| 215 | e.printStackTrace(); |
| 216 | return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败 |
| 217 | } catch (Exception e) { |
| 218 | e.printStackTrace(); |
| 219 | return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | @Override |
| 224 | public ResponseEntity<String> getUserSeeds( |
| 225 | @RequestParam("userid") String userid |
| 226 | ) { |
| 227 | try { |
| 228 | Seed[] seeds = db1.GetSeedListByUser(userid); |
| 229 | if (seeds == null || seeds.length == 0) { |
| 230 | return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子 |
| 231 | } |
| 232 | String json = mapper.writeValueAsString(seeds); |
| 233 | return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 234 | } catch (JsonProcessingException e) { |
| 235 | e.printStackTrace(); |
| 236 | return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | @Override |
| 241 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 242 | public ResponseEntity<Integer> deleteSeed( |
| 243 | @RequestBody String requestBody |
| 244 | ) { |
| 245 | try { |
| 246 | // 解析 JSON 数据 |
| 247 | com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody); |
| 248 | com.fasterxml.jackson.databind.JsonNode seedidNode = jsonNode.get("seedid"); |
| 249 | if (seedidNode == null) { |
| 250 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); |
| 251 | } |
| 252 | String seedid = seedidNode.asText(); |
| 253 | |
| 254 | // 添加参数验证 |
| 255 | if (seedid == null || seedid.trim().isEmpty()) { |
| 256 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); |
| 257 | } |
| 258 | |
| 259 | int ret = db1.DeleteSeed(seedid); |
| 260 | if (ret == 0) { |
| 261 | return new ResponseEntity<>(0, HttpStatus.OK); |
| 262 | } else { |
| 263 | return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); |
| 264 | } |
| 265 | } catch (Exception e) { |
| 266 | e.printStackTrace(); |
| 267 | return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public ResponseEntity<String> getUserStat( |
| 273 | @RequestParam("userid") String userid |
| 274 | ) { |
| 275 | try { |
| 276 | User user = db1.GetInformation(userid); |
| 277 | if (user == null) { |
| 278 | return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 279 | } |
| 280 | String json = mapper.writeValueAsString(user); |
| 281 | return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 282 | } catch (JsonProcessingException e) { |
| 283 | e.printStackTrace(); |
| 284 | return new ResponseEntity<>("", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | @Override |
Raver | aae0612 | 2025-06-05 08:13:35 +0000 | [diff] [blame] | 289 | public ResponseEntity<String> getTorrentDetail( |
| 290 | @RequestParam("id") String seedid |
| 291 | ) { |
| 292 | try { |
| 293 | Seed seed = db1.GetSeedInformation(seedid); |
| 294 | if (seed != null) { |
| 295 | String json = mapper.writeValueAsString(seed); |
| 296 | HttpHeaders headers = new HttpHeaders(); |
| 297 | headers.add("Access-Control-Allow-Origin", "*"); |
| 298 | headers.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); |
| 299 | headers.add("Access-Control-Allow-Headers", "Content-Type, Authorization"); |
| 300 | return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 301 | } else { |
| 302 | return ResponseEntity.notFound().build(); // 返回 404 表示种子未找到 |
| 303 | } |
| 304 | } catch (JsonProcessingException e) { |
| 305 | e.printStackTrace(); |
| 306 | HttpHeaders errorHeaders = new HttpHeaders(); |
| 307 | errorHeaders.add("Access-Control-Allow-Origin", "*"); |
| 308 | errorHeaders.add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS"); |
| 309 | errorHeaders.add("Access-Control-Allow-Headers", "Content-Type, Authorization"); |
| 310 | return new ResponseEntity<>("{}", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 311 | } |
| 312 | } |
rhj | 46f62c4 | 2025-06-06 23:24:10 +0800 | [diff] [blame] | 313 | |
| 314 | @Override |
| 315 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 316 | public ResponseEntity<String> loginUser( |
| 317 | @RequestBody String requestBody |
| 318 | ) { |
| 319 | try { |
| 320 | // 解析前端发送的JSON数据 {email: xxx, password: xxx} |
| 321 | com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody); |
| 322 | |
| 323 | // 获取email和password字段 |
| 324 | com.fasterxml.jackson.databind.JsonNode emailNode = jsonNode.get("email"); |
| 325 | com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password"); |
| 326 | |
| 327 | if (emailNode == null || passwordNode == null) { |
| 328 | com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode(); |
| 329 | errorJson.put("message", "缺少必要参数"); |
| 330 | String jsonError = mapper.writeValueAsString(errorJson); |
| 331 | return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST); |
| 332 | } |
| 333 | |
| 334 | String email = emailNode.asText(); |
| 335 | String password = passwordNode.asText(); |
| 336 | |
| 337 | // 参数验证 |
| 338 | if (email == null || email.trim().isEmpty() || |
| 339 | password == null || password.trim().isEmpty()) { |
| 340 | com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode(); |
| 341 | errorJson.put("message", "邮箱和密码不能为空"); |
| 342 | String jsonError = mapper.writeValueAsString(errorJson); |
| 343 | return new ResponseEntity<>(jsonError, HttpStatus.BAD_REQUEST); |
| 344 | } |
| 345 | |
| 346 | // 创建User对象进行登录验证 |
| 347 | User user = new User(); |
| 348 | user.email = email.trim(); |
| 349 | user.password = password; |
| 350 | |
| 351 | String userid = db1.LoginUser(user); |
| 352 | if (userid != null) { |
| 353 | com.fasterxml.jackson.databind.node.ObjectNode responseJson = mapper.createObjectNode(); |
| 354 | responseJson.put("userId", userid); |
| 355 | responseJson.put("userid", userid); |
| 356 | responseJson.put("message", "登录成功"); |
| 357 | String jsonResponse = mapper.writeValueAsString(responseJson); |
| 358 | return new ResponseEntity<>(jsonResponse, HttpStatus.OK); |
| 359 | } else { |
| 360 | // 返回JSON格式的错误信息 |
| 361 | com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode(); |
| 362 | errorJson.put("message", "登录失败,请检查账号密码"); |
| 363 | String jsonError = mapper.writeValueAsString(errorJson); |
| 364 | return new ResponseEntity<>(jsonError, HttpStatus.UNAUTHORIZED); |
| 365 | } |
| 366 | } catch (JsonProcessingException e) { |
| 367 | e.printStackTrace(); |
| 368 | com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode(); |
| 369 | try { |
| 370 | errorJson.put("message", "服务器内部错误"); |
| 371 | String jsonError = mapper.writeValueAsString(errorJson); |
| 372 | return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR); |
| 373 | } catch (JsonProcessingException ex) { |
| 374 | return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR); |
| 375 | } |
| 376 | } catch (Exception e) { |
| 377 | e.printStackTrace(); |
| 378 | com.fasterxml.jackson.databind.node.ObjectNode errorJson = mapper.createObjectNode(); |
| 379 | try { |
| 380 | errorJson.put("message", "服务器内部错误"); |
| 381 | String jsonError = mapper.writeValueAsString(errorJson); |
| 382 | return new ResponseEntity<>(jsonError, HttpStatus.INTERNAL_SERVER_ERROR); |
| 383 | } catch (JsonProcessingException ex) { |
| 384 | return new ResponseEntity<>("{\"message\":\"服务器内部错误\"}", HttpStatus.INTERNAL_SERVER_ERROR); |
| 385 | } |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | @Override |
| 390 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 391 | public ResponseEntity<Integer> registerUser( |
| 392 | @RequestBody String requestBody |
| 393 | ) { |
| 394 | try { |
| 395 | System.out.println("Register request body: " + requestBody); |
| 396 | // 解析 JSON 数据 |
| 397 | com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody); |
| 398 | |
| 399 | // 安全地获取字段 |
| 400 | com.fasterxml.jackson.databind.JsonNode usernameNode = jsonNode.get("username"); |
| 401 | com.fasterxml.jackson.databind.JsonNode passwordNode = jsonNode.get("password"); |
| 402 | com.fasterxml.jackson.databind.JsonNode inviteEmailNode = jsonNode.get("invite_email"); |
| 403 | |
| 404 | if (usernameNode == null || passwordNode == null || inviteEmailNode == null) { |
| 405 | return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); // 参数不完整 |
| 406 | } |
| 407 | |
| 408 | String username = usernameNode.asText(); |
| 409 | String password = passwordNode.asText(); |
| 410 | String inviteEmail = inviteEmailNode.asText(); |
| 411 | |
| 412 | // 参数验证 |
| 413 | if (username == null || username.trim().isEmpty() || |
| 414 | password == null || password.trim().isEmpty() || |
| 415 | inviteEmail == null || inviteEmail.trim().isEmpty()) { |
| 416 | return new ResponseEntity<>(2, HttpStatus.BAD_REQUEST); |
| 417 | } |
| 418 | |
| 419 | // 创建 User 对象 |
| 420 | User user = new User(); |
| 421 | user.userid = java.util.UUID.randomUUID().toString(); // 生成唯一用户ID |
| 422 | user.username = username.trim(); |
| 423 | user.password = password; |
| 424 | user.email = inviteEmail.trim(); // 使用邀请邮箱作为用户邮箱 |
| 425 | |
| 426 | // 设置默认值 |
| 427 | user.sex = "m"; // 默认性别 |
| 428 | user.school = ""; // 默认学校 |
| 429 | user.pictureurl = ""; // 默认头像URL |
| 430 | user.profile = ""; // 默认个人简介 |
| 431 | user.accountstate = false; // 默认账号状态为正常 |
| 432 | user.invitetimes = 5; // 默认邀请次数 |
| 433 | |
| 434 | // 设置时间字段 |
| 435 | user.lastDetectedTime = new java.util.Date(); |
| 436 | user.fakeLastDetectedTime = new java.util.Date(); |
| 437 | |
| 438 | // 调用数据库注册方法 |
| 439 | int ret = db1.RegisterUser(user); |
| 440 | System.out.println("Register result: " + ret); |
| 441 | |
| 442 | // // 如果注册成功,还需要创建对应的 UserPT 记录 |
| 443 | // if (ret == 0) { |
| 444 | // try { |
| 445 | // entity.UserPT userPT = new entity.UserPT(); |
| 446 | // userPT.userid = user.userid; |
| 447 | // userPT.magic = 100; // 初始魔力值 |
| 448 | // userPT.upload = 0L; // 初始上传量 |
| 449 | // userPT.download = 0L; // 初始下载量 |
| 450 | // userPT.share = 0.0; // 初始分享率 |
| 451 | // userPT.farmurl = ""; // 默认做种路径 |
| 452 | // userPT.viptime = 0; // 初始VIP次数 |
| 453 | // userPT.user = user; // 设置关联 |
| 454 | |
| 455 | // int ptRet = db1.RegisterUserPT(userPT); |
| 456 | // if (ptRet != 0) { |
| 457 | // // 如果 UserPT 创建失败,记录日志但不影响主要注册流程 |
| 458 | // System.err.println("Warning: Failed to create UserPT for user " + user.userid); |
| 459 | // } |
| 460 | // } catch (Exception e) { |
| 461 | // System.err.println("Warning: Exception creating UserPT: " + e.getMessage()); |
| 462 | // } |
| 463 | // } |
| 464 | |
| 465 | if (ret == 0) { |
| 466 | return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示注册成功 |
| 467 | } else if (ret == 1) { |
| 468 | return new ResponseEntity<>(1, HttpStatus.CONFLICT); // 返回 1 表示邮箱重复 |
| 469 | } else { |
| 470 | return new ResponseEntity<>(ret, HttpStatus.BAD_REQUEST); // 返回 2 表示未被邀请或其他错误 |
| 471 | } |
| 472 | } catch (JsonProcessingException e) { |
| 473 | e.printStackTrace(); |
| 474 | return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 2 表示处理失败 |
| 475 | } catch (Exception e) { |
| 476 | e.printStackTrace(); |
| 477 | return new ResponseEntity<>(2, HttpStatus.INTERNAL_SERVER_ERROR); |
| 478 | } |
| 479 | } |
| 480 | |
| 481 | @Override |
rhj | 5b69b7e | 2025-06-07 01:28:08 +0800 | [diff] [blame] | 482 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
rhj | 46f62c4 | 2025-06-06 23:24:10 +0800 | [diff] [blame] | 483 | public ResponseEntity<String> getForum() { |
| 484 | try { |
| 485 | Post[] posts = db1.GetPostList(); |
| 486 | if (posts == null || posts.length == 0) { |
| 487 | return new ResponseEntity<>("[]", HttpStatus.OK); // 返回空数组表示没有帖子 |
| 488 | } |
| 489 | String json = mapper.writeValueAsString(posts); |
| 490 | return new ResponseEntity<>(json, HttpStatus.OK); |
| 491 | } catch (JsonProcessingException e) { |
| 492 | e.printStackTrace(); |
| 493 | return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR); |
| 494 | } catch (Exception e) { |
| 495 | e.printStackTrace(); |
| 496 | return new ResponseEntity<>("[]", HttpStatus.INTERNAL_SERVER_ERROR); |
| 497 | } |
| 498 | } |
rhj | 5b69b7e | 2025-06-07 01:28:08 +0800 | [diff] [blame] | 499 | |
| 500 | @Override |
| 501 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 502 | public ResponseEntity<String> getPostById( |
| 503 | @RequestParam("postid") String postid |
| 504 | ) { |
| 505 | try { |
| 506 | Post post = db1.GetPost(postid); |
| 507 | PostReply[] replies = db1.GetPostReplyList(postid); |
| 508 | if (post == null) { |
| 509 | return new ResponseEntity<>("", HttpStatus.NOT_FOUND); // 返回 404 表示帖子未找到 |
| 510 | } |
| 511 | |
| 512 | // 创建合并的 JSON 对象 |
| 513 | com.fasterxml.jackson.databind.node.ObjectNode resultJson = mapper.createObjectNode(); |
| 514 | |
| 515 | // 添加 post 的所有字段 |
| 516 | resultJson.put("postid", post.postid); |
| 517 | resultJson.put("posttitle", post.posttitle); |
| 518 | resultJson.put("postcontent", post.postcontent); |
| 519 | resultJson.put("postuserid", post.postuserid); |
| 520 | resultJson.put("replytime", post.replytime); |
| 521 | resultJson.put("readtime", post.readtime); |
| 522 | |
| 523 | if (post.posttime != null) { |
| 524 | resultJson.put("posttime", post.posttime.getTime()); |
| 525 | } |
| 526 | |
| 527 | // 添加作者信息 |
| 528 | if (post.author != null) { |
| 529 | com.fasterxml.jackson.databind.node.ObjectNode authorJson = mapper.createObjectNode(); |
| 530 | authorJson.put("userid", post.author.userid); |
| 531 | authorJson.put("username", post.author.username); |
| 532 | authorJson.put("email", post.author.email); |
| 533 | authorJson.put("sex", post.author.sex); |
| 534 | authorJson.put("school", post.author.school); |
| 535 | authorJson.put("pictureurl", post.author.pictureurl); |
| 536 | authorJson.put("profile", post.author.profile); |
| 537 | authorJson.put("accountstate", post.author.accountstate); |
| 538 | authorJson.put("invitetimes", post.author.invitetimes); |
| 539 | if (post.author.lastDetectedTime != null) { |
| 540 | authorJson.put("lastDetectedTime", post.author.lastDetectedTime.getTime()); |
| 541 | } |
| 542 | if (post.author.fakeLastDetectedTime != null) { |
| 543 | authorJson.put("fakeLastDetectedTime", post.author.fakeLastDetectedTime.getTime()); |
| 544 | } |
| 545 | resultJson.set("author", authorJson); |
| 546 | } |
| 547 | |
| 548 | // 添加 replies 数组 |
| 549 | com.fasterxml.jackson.databind.node.ArrayNode repliesArray = mapper.createArrayNode(); |
| 550 | if (replies != null) { |
| 551 | System.out.println("Replies count: " + replies.length); |
| 552 | for (PostReply reply : replies) { |
| 553 | com.fasterxml.jackson.databind.node.ObjectNode replyJson = mapper.createObjectNode(); |
| 554 | replyJson.put("replyid", reply.replyid); |
| 555 | replyJson.put("postid", reply.postid); |
| 556 | replyJson.put("authorid", reply.authorid); |
| 557 | replyJson.put("content", reply.content); |
| 558 | if (reply.createdAt != null) { |
| 559 | replyJson.put("createdAt", reply.createdAt.getTime()); |
| 560 | } |
| 561 | |
| 562 | // 添加回复作者信息 |
| 563 | if (reply.author != null) { |
| 564 | com.fasterxml.jackson.databind.node.ObjectNode replyAuthorJson = mapper.createObjectNode(); |
| 565 | replyAuthorJson.put("userid", reply.author.userid); |
| 566 | replyAuthorJson.put("username", reply.author.username); |
| 567 | replyAuthorJson.put("email", reply.author.email); |
| 568 | replyAuthorJson.put("sex", reply.author.sex); |
| 569 | replyAuthorJson.put("school", reply.author.school); |
| 570 | replyAuthorJson.put("pictureurl", reply.author.pictureurl); |
| 571 | replyAuthorJson.put("profile", reply.author.profile); |
| 572 | replyAuthorJson.put("accountstate", reply.author.accountstate); |
| 573 | replyAuthorJson.put("invitetimes", reply.author.invitetimes); |
| 574 | if (reply.author.lastDetectedTime != null) { |
| 575 | replyAuthorJson.put("lastDetectedTime", reply.author.lastDetectedTime.getTime()); |
| 576 | } |
| 577 | if (reply.author.fakeLastDetectedTime != null) { |
| 578 | replyAuthorJson.put("fakeLastDetectedTime", reply.author.fakeLastDetectedTime.getTime()); |
| 579 | } |
| 580 | replyJson.set("author", replyAuthorJson); |
| 581 | } |
| 582 | |
| 583 | repliesArray.add(replyJson); |
| 584 | } |
| 585 | } |
| 586 | resultJson.set("replies", repliesArray); |
| 587 | |
| 588 | String json = mapper.writeValueAsString(resultJson); |
| 589 | return new ResponseEntity<>(json, HttpStatus.OK); |
| 590 | } catch (JsonProcessingException e) { |
| 591 | // e.printStackTrace(); |
| 592 | return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR); |
| 593 | } catch (Exception e) { |
| 594 | // e.printStackTrace(); |
| 595 | return new ResponseEntity<>("", HttpStatus.INTERNAL_SERVER_ERROR); |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | @Override |
| 600 | @CrossOrigin(origins = "*", allowedHeaders = "*") |
| 601 | public ResponseEntity<Integer> addPostReply( |
| 602 | @RequestBody String requestBody |
| 603 | ) { |
| 604 | try { |
| 605 | System.out.println("Add reply request body: " + requestBody); |
| 606 | // 解析 JSON 数据 |
| 607 | com.fasterxml.jackson.databind.JsonNode jsonNode = mapper.readTree(requestBody); |
| 608 | com.fasterxml.jackson.databind.JsonNode postidNode = jsonNode.get("postid"); |
| 609 | com.fasterxml.jackson.databind.JsonNode authoridNode = jsonNode.get("replyuserid"); |
| 610 | com.fasterxml.jackson.databind.JsonNode contentNode = jsonNode.get("replycontent"); |
| 611 | |
| 612 | if (postidNode == null || authoridNode == null || contentNode == null) { |
| 613 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); // 参数不完整 |
| 614 | } |
| 615 | |
| 616 | String postid = postidNode.asText(); |
| 617 | String authorid = authoridNode.asText(); |
| 618 | String content = contentNode.asText(); |
| 619 | |
| 620 | System.out.println("Post ID: " + postid); |
| 621 | System.out.println("Author ID: " + authorid); |
| 622 | System.out.println("Content: " + content); |
| 623 | |
| 624 | // 参数验证 |
| 625 | if (postid == null || postid.trim().isEmpty() || |
| 626 | authorid == null || authorid.trim().isEmpty() || |
| 627 | content == null || content.trim().isEmpty()) { |
| 628 | return new ResponseEntity<>(1, HttpStatus.BAD_REQUEST); |
| 629 | } |
| 630 | |
| 631 | int ret = db1.AddComment(postid, authorid, content); |
| 632 | System.out.println("Add comment result: " + ret); |
| 633 | if (ret == 0) { |
| 634 | return new ResponseEntity<>(0, HttpStatus.OK); // 返回 0 表示成功 |
| 635 | } else { |
| 636 | return new ResponseEntity<>(ret, HttpStatus.INTERNAL_SERVER_ERROR); // 返回其他状态表示失败 |
| 637 | } |
| 638 | } catch (JsonProcessingException e) { |
| 639 | e.printStackTrace(); |
| 640 | return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 1 表示处理失败 |
| 641 | } catch (Exception e) { |
| 642 | e.printStackTrace(); |
| 643 | return new ResponseEntity<>(1, HttpStatus.INTERNAL_SERVER_ERROR); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | @Override |
| 648 | public ResponseEntity<String> searchSeeds( |
| 649 | @RequestParam("tag") String tag, |
| 650 | @RequestParam("keyword") String query |
| 651 | ) { |
| 652 | return null; |
| 653 | // try { |
| 654 | // Seed[] seeds = db1.SearchSeeds(query); |
| 655 | // if (seeds == null || seeds.length == 0) { |
| 656 | // return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); // 返回 404 表示未找到种子 |
| 657 | // } |
| 658 | // String json = mapper.writeValueAsString(seeds); |
| 659 | // return new ResponseEntity<>(json, headers, HttpStatus.OK); |
| 660 | // } catch (JsonProcessingException e) { |
| 661 | // e.printStackTrace(); |
| 662 | // return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 663 | // } catch (Exception e) { |
| 664 | // e.printStackTrace(); |
| 665 | // return new ResponseEntity<>("[]", errorHeaders, HttpStatus.INTERNAL_SERVER_ERROR); |
| 666 | // } |
| 667 | } |
Raver | f79fdb6 | 2025-06-03 06:02:49 +0000 | [diff] [blame] | 668 | } |