fix: 修复Spring Boot兼容性问题并添加链接删除功能

主要修改:
1. 降级Spring Boot版本到2.7.18以兼容MyBatis-Plus
2. 修复所有validation包导入路径 (jakarta -> javax)
3. 修复ResponseStatusException API调用
4. 降级Swagger版本以兼容Spring Boot 2.x
5. 添加单个和批量删除链接功能
6. 修复JWT认证中的Claims获取方式
7. 优化代码格式和日志输出

技术细节:
- Spring Boot: 3.3.3 -> 2.7.18
- Swagger: springdoc-openapi-starter-webflux-ui:2.3.0 -> springdoc-openapi-webflux-ui:1.7.0
- 所有javax.validation包路径修复
- 新增BatchDeleteRequest和BatchDeleteResponse DTO类
- LinkController中添加DELETE和POST批量删除接口
This commit is contained in:
zyh
2025-08-26 16:43:53 +08:00
parent 833159d1f1
commit e9858bfec1
7 changed files with 403 additions and 4 deletions

View File

@@ -1,5 +1,7 @@
package com.gameplatform.server.controller.link;
import com.gameplatform.server.model.dto.link.BatchDeleteRequest;
import com.gameplatform.server.model.dto.link.BatchDeleteResponse;
import com.gameplatform.server.model.dto.link.LinkGenerateRequest;
import com.gameplatform.server.model.dto.link.LinkGenerateResponse;
import com.gameplatform.server.model.dto.link.LinkListRequest;
@@ -14,12 +16,9 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.ReactiveSecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@RestController
@@ -56,7 +55,13 @@ public class LinkController {
log.info("认证用户: {}", authentication.getName());
// 获取用户ID
Claims claims = (Claims) authentication.getCredentials();
Claims claims = (Claims) authentication.getDetails();
if (claims == null) {
log.error("=== 认证失败Claims为空 ===");
log.error("Authentication details: {}", authentication.getDetails());
return Mono.error(new IllegalArgumentException("用户未认证Claims为空"));
}
Long agentId = claims.get("userId", Long.class);
String userType = claims.get("userType", String.class);
@@ -189,6 +194,95 @@ public class LinkController {
log.debug("检查链接是否有效: codeNo={}", codeNo);
return linkStatusService.isLinkValid(codeNo);
}
@DeleteMapping("/{codeNo}")
@Operation(summary = "删除链接", description = "删除指定的链接,用户只能删除自己创建的链接")
public Mono<Boolean> deleteLink(@PathVariable("codeNo") String codeNo, Authentication authentication) {
log.info("=== 开始删除链接 ===");
log.info("链接编号: {}", codeNo);
if (authentication == null) {
log.error("=== 认证失败Authentication为空 ===");
return Mono.error(new IllegalArgumentException("用户未认证Authentication为空"));
}
// 获取用户ID
Claims claims = (Claims) authentication.getDetails();
if (claims == null) {
log.error("=== 认证失败Claims为空 ===");
log.error("Authentication details: {}", authentication.getDetails());
return Mono.error(new IllegalArgumentException("用户未认证Claims为空"));
}
Long agentId = claims.get("userId", Long.class);
String userType = claims.get("userType", String.class);
log.info("用户信息: agentId={}, userType={}", agentId, userType);
if (agentId == null) {
log.error("=== 无法获取用户ID ===");
return Mono.error(new IllegalArgumentException("无法获取用户ID"));
}
return linkStatusService.deleteLink(codeNo, agentId)
.doOnSuccess(success -> {
if (success) {
log.info("链接删除成功: codeNo={}, agentId={}", codeNo, agentId);
} else {
log.warn("链接删除失败: codeNo={}, agentId={}", codeNo, agentId);
}
})
.doOnError(error -> {
log.error("删除链接时发生错误: codeNo={}, agentId={}, error={}",
codeNo, agentId, error.getMessage(), error);
});
}
@PostMapping("/batch-delete")
@Operation(summary = "批量删除链接", description = "批量删除指定的链接用户只能删除自己创建的链接最多一次删除100个")
public Mono<BatchDeleteResponse> batchDeleteLinks(@RequestBody BatchDeleteRequest request, Authentication authentication) {
log.info("=== 开始批量删除链接 ===");
log.info("要删除的链接数量: {}", request.getCodeNos().size());
log.info("链接编号列表: {}", request.getCodeNos());
if (authentication == null) {
log.error("=== 认证失败Authentication为空 ===");
return Mono.error(new IllegalArgumentException("用户未认证Authentication为空"));
}
// 获取用户ID
Claims claims = (Claims) authentication.getDetails();
if (claims == null) {
log.error("=== 认证失败Claims为空 ===");
log.error("Authentication details: {}", authentication.getDetails());
return Mono.error(new IllegalArgumentException("用户未认证Claims为空"));
}
Long agentId = claims.get("userId", Long.class);
String userType = claims.get("userType", String.class);
log.info("用户信息: agentId={}, userType={}", agentId, userType);
if (agentId == null) {
log.error("=== 无法获取用户ID ===");
return Mono.error(new IllegalArgumentException("无法获取用户ID"));
}
return linkStatusService.batchDeleteLinks(request.getCodeNos(), agentId)
.doOnSuccess(response -> {
log.info("批量删除链接完成: 总数={}, 成功={}, 失败={}, agentId={}",
response.getTotalCount(), response.getSuccessCount(),
response.getFailedCount(), agentId);
if (!response.isAllSuccess()) {
log.warn("部分链接删除失败: 失败的链接={}, 失败原因={}",
response.getFailedCodeNos(), response.getFailedReasons());
}
})
.doOnError(error -> {
log.error("批量删除链接时发生错误: agentId={}, codeNos={}, error={}",
agentId, request.getCodeNos(), error.getMessage(), error);
});
}
}