feat: 使用banana模型生成分镜图片,修复数据库列类型问题

- 修改RealAIService.submitTextToImageTask使用nano-banana/nano-banana-hd模型
- 支持根据hdMode参数选择模型(标准/高清)
- 修复数据库列类型:将result_url等字段改为TEXT类型以支持Base64图片
- 添加数据库修复SQL脚本(fix_database_columns.sql, update_database_schema.sql)
- 改进StoryboardVideoService的错误处理和空值检查
- 添加GlobalExceptionHandler全局异常处理
- 优化图片URL提取逻辑,支持url和b64_json两种格式
- 改进响应格式验证,确保data字段不为空
This commit is contained in:
AIGC Developer
2025-11-05 18:18:53 +08:00
parent 0b0ad442a0
commit b5820d9be2
63 changed files with 2207 additions and 341 deletions

View File

@@ -22,7 +22,6 @@ import com.example.demo.util.JwtUtils;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
@RestController
@RequestMapping("/api/auth")
@@ -82,47 +81,84 @@ public class AuthApiController {
if (user == null) {
// 自动注册新用户
try {
logger.info("邮箱验证码登录 - 用户不存在,开始自动注册:{}", email);
// 从邮箱生成用户名(去掉@符号及后面的部分)
String username = email.split("@")[0];
logger.info("邮箱验证码登录 - 原始邮箱: '{}', 生成的用户名: '{}'", email, username);
// 清理用户名(移除特殊字符,只保留字母、数字、下划线)
username = username.replaceAll("[^a-zA-Z0-9_]", "_");
// 确保用户名长度不超过50个字符
if (username.length() > 50) {
username = username.substring(0, 50);
logger.info("邮箱验证码登录 - 用户名过长,截断为: '{}'", username);
}
// 确保用户名不为空且至少3个字符
if (username.length() < 3) {
username = username + "_" + System.currentTimeMillis() % 1000;
if (username.length() > 50) {
username = username.substring(0, 50);
}
}
// 确保用户名唯一
String originalUsername = username;
int counter = 1;
while (userService.findByUsernameOrNull(username) != null) {
// 如果用户名过长,需要重新截断
String newUsername = originalUsername + counter;
String baseUsername = originalUsername.length() > 45 ?
originalUsername.substring(0, 45) : originalUsername;
String newUsername = baseUsername + counter;
if (newUsername.length() > 50) {
newUsername = newUsername.substring(0, 50);
}
username = newUsername;
counter++;
// 防止无限循环
if (counter > 1000) {
// 使用时间戳确保唯一性
username = "user_" + System.currentTimeMillis();
break;
}
}
// 创建新用户
logger.info("邮箱验证码登录 - 最终用户名: '{}'", username);
// 直接创建用户对象并设置所有必要字段
user = new User();
user.setUsername(username);
user.setEmail(email);
user.setPasswordHash(""); // 邮箱登录不需要密码
user.setRole("ROLE_USER"); // 默认为普通用户
user.setPoints(50); // 默认积分
user.setPoints(50); // 默认50积分
user.setFrozenPoints(0); // 默认冻结积分为0
user.setNickname(username); // 默认昵称为用户名
user.setIsActive(true);
// 保存用户
// 保存用户@PrePersist 会自动设置 createdAt 等字段)
user = userService.save(user);
logger.info("自动注册新用户:{}", email);
logger.info("✅ 用户已保存到数据库 - ID: {}, 用户名: {}, 邮箱: {}",
user.getId(), user.getUsername(), user.getEmail());
logger.info("✅ 自动注册新用户成功 - 邮箱: {}, 用户名: {}, ID: {}",
email, username, user.getId());
} catch (IllegalArgumentException e) {
logger.error("❌ 自动注册用户失败(参数错误):{} - {}", email, e.getMessage());
return ResponseEntity.badRequest()
.body(createErrorResponse("用户注册失败:" + e.getMessage()));
} catch (Exception e) {
logger.error("自动注册用户失败:{}", email, e);
logger.error("自动注册用户失败:{}", email, e);
return ResponseEntity.badRequest()
.body(createErrorResponse("用户注册失败:" + e.getMessage()));
}
} else {
logger.info("✅ 找到现有用户 - 邮箱: {}, 用户名: {}, ID: {}",
email, user.getUsername(), user.getId());
}
// 生成JWT Token
@@ -153,28 +189,59 @@ public class AuthApiController {
* 用户注册
*/
@PostMapping("/register")
public ResponseEntity<Map<String, Object>> register(@Valid @RequestBody User user) {
public ResponseEntity<Map<String, Object>> register(@RequestBody Map<String, String> requestData) {
try {
if (userService.findByUsername(user.getUsername()) != null) {
String username = requestData.get("username");
String email = requestData.get("email");
String password = requestData.get("password");
// 验证必填字段
if (username == null || username.trim().isEmpty()) {
return ResponseEntity.badRequest()
.body(createErrorResponse("用户名不能为空"));
}
if (email == null || email.trim().isEmpty()) {
return ResponseEntity.badRequest()
.body(createErrorResponse("邮箱不能为空"));
}
if (password == null || password.trim().isEmpty()) {
return ResponseEntity.badRequest()
.body(createErrorResponse("密码不能为空"));
}
// 检查用户名是否已存在
User existingUser = userService.findByUsernameOrNull(username);
if (existingUser != null) {
return ResponseEntity.badRequest()
.body(createErrorResponse("用户名已存在"));
}
if (userService.findByEmail(user.getEmail()) != null) {
// 检查邮箱是否已存在
User existingEmail = userService.findByEmailOrNull(email);
if (existingEmail != null) {
return ResponseEntity.badRequest()
.body(createErrorResponse("邮箱已存在"));
}
User savedUser = userService.save(user);
// 使用 register 方法创建用户(会自动编码密码)
User savedUser = userService.register(username, email, password);
Map<String, Object> response = new HashMap<>();
response.put("success", true);
response.put("message", "注册成功");
// 不返回密码哈希
savedUser.setPasswordHash(null);
response.put("data", savedUser);
logger.info("用户注册成功:{}", user.getUsername());
logger.info("用户注册成功:{}", username);
return ResponseEntity.ok(response);
} catch (IllegalArgumentException e) {
logger.warn("注册失败:{}", e.getMessage());
return ResponseEntity.badRequest()
.body(createErrorResponse(e.getMessage()));
} catch (Exception e) {
logger.error("注册失败:", e);
return ResponseEntity.badRequest()

View File

@@ -0,0 +1,37 @@
package com.example.demo.controller;
import java.util.HashMap;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
@ControllerAdvice
public class GlobalExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, Object>> handleException(Exception e) {
logger.error("全局异常处理器捕获到异常", e);
logger.error("异常类型: {}", e.getClass().getName());
logger.error("异常消息: {}", e.getMessage());
if (e.getCause() != null) {
logger.error("异常原因: {}", e.getCause().getMessage());
}
e.printStackTrace();
Map<String, Object> errorResponse = new HashMap<>();
errorResponse.put("success", false);
errorResponse.put("message", "服务器内部错误: " + e.getMessage());
errorResponse.put("error", e.getClass().getSimpleName());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorResponse);
}
}

View File

@@ -1,5 +1,7 @@
package com.example.demo.controller;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -21,8 +23,11 @@ import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Payment;
import com.example.demo.model.PaymentStatus;
import com.example.demo.model.User;
import com.example.demo.repository.PaymentRepository;
import com.example.demo.service.AlipayService;
import com.example.demo.service.PaymentService;
import com.example.demo.service.UserService;
@RestController
@RequestMapping("/api/payments")
@@ -35,6 +40,12 @@ public class PaymentApiController {
@Autowired
private AlipayService alipayService;
@Autowired
private UserService userService;
@Autowired
private PaymentRepository paymentRepository;
/**
@@ -361,6 +372,109 @@ public class PaymentApiController {
}
}
/**
* 获取用户订阅信息(当前套餐、到期时间等)
*/
@GetMapping("/subscription/info")
public ResponseEntity<Map<String, Object>> getUserSubscriptionInfo(
Authentication authentication) {
try {
logger.info("=== 开始获取用户订阅信息 ===");
if (authentication == null || !authentication.isAuthenticated()) {
logger.warn("用户未认证");
return ResponseEntity.badRequest()
.body(createErrorResponse("请先登录"));
}
String username = authentication.getName();
logger.info("认证用户名: {}", username);
User user = userService.findByUsername(username);
logger.info("查找用户结果: {}", user != null ? "找到用户ID: " + user.getId() : "未找到用户");
if (user == null) {
logger.error("用户不存在: {}", username);
return ResponseEntity.badRequest()
.body(createErrorResponse("用户不存在"));
}
// 获取用户最近一次成功的订阅支付
logger.info("开始查询用户订阅记录用户ID: {}", user.getId());
List<Payment> subscriptions;
try {
subscriptions = paymentRepository.findLatestSuccessfulSubscriptionByUserId(user.getId(), PaymentStatus.SUCCESS);
logger.info("用户 {} (ID: {}) 的订阅记录数量: {}", username, user.getId(), subscriptions.size());
} catch (Exception e) {
logger.error("查询订阅记录失败用户ID: {}", user.getId(), e);
// 如果查询失败,使用空列表
subscriptions = new ArrayList<>();
}
Map<String, Object> subscriptionInfo = new HashMap<>();
// 默认值:免费版
String currentPlan = "免费版";
String expiryTime = "永久";
LocalDateTime paidAt = null;
if (!subscriptions.isEmpty()) {
logger.info("找到订阅记录,第一条描述: {}", subscriptions.get(0).getDescription());
Payment latestSubscription = subscriptions.get(0);
String description = latestSubscription.getDescription();
paidAt = latestSubscription.getPaidAt() != null ?
latestSubscription.getPaidAt() : latestSubscription.getCreatedAt();
// 从描述中识别套餐类型
if (description != null) {
if (description.contains("标准版")) {
currentPlan = "标准版会员";
} else if (description.contains("专业版")) {
currentPlan = "专业版会员";
} else if (description.contains("会员")) {
currentPlan = "会员";
}
}
// 计算到期时间假设订阅有效期为30天
if (paidAt != null) {
LocalDateTime expiryDateTime = paidAt.plusDays(30);
LocalDateTime now = LocalDateTime.now();
if (expiryDateTime.isAfter(now)) {
// 未过期,显示到期时间
expiryTime = expiryDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm"));
} else {
// 已过期,显示已过期
expiryTime = "已过期";
}
}
}
subscriptionInfo.put("currentPlan", currentPlan);
subscriptionInfo.put("expiryTime", expiryTime);
subscriptionInfo.put("paidAt", paidAt != null ? paidAt.toString() : null);
subscriptionInfo.put("points", user.getPoints());
subscriptionInfo.put("username", user.getUsername());
subscriptionInfo.put("userId", user.getId());
subscriptionInfo.put("email", user.getEmail());
subscriptionInfo.put("nickname", user.getNickname());
Map<String, Object> response = new HashMap<>();
response.put("success", true);
response.put("data", subscriptionInfo);
logger.info("=== 用户订阅信息获取成功 ===");
return ResponseEntity.ok(response);
} catch (Exception e) {
logger.error("获取用户订阅信息失败", e);
logger.error("异常堆栈: ", e);
e.printStackTrace();
return ResponseEntity.status(500)
.body(createErrorResponse("获取用户订阅信息失败: " + e.getMessage()));
}
}
/**
* 创建支付宝支付
*/

View File

@@ -100,6 +100,10 @@ public class PaymentController {
@PostMapping("/alipay/notify")
@ResponseBody
public String alipayNotify(HttpServletRequest request) {
logger.info("========== 收到支付宝回调请求 ==========");
logger.info("请求方法: {}", request.getMethod());
logger.info("请求URL: {}", request.getRequestURL());
logger.info("Content-Type: {}", request.getContentType());
try {
// 支付宝异步通知参数获取
// 注意IJPay的AliPayApi.toMap()使用javax.servlet但Spring Boot 3使用jakarta.servlet
@@ -151,11 +155,14 @@ public class PaymentController {
}
}
logger.info("解析到的参数: {}", params);
boolean success = alipayService.handleNotify(params);
logger.info("处理结果: {}", success ? "success" : "fail");
logger.info("========== 支付宝回调处理完成 ==========");
return success ? "success" : "fail";
} catch (Exception e) {
logger.error("处理支付宝异步通知失败:", e);
logger.error("========== 处理支付宝异步通知失败 ==========", e);
return "fail";
}
}

View File

@@ -69,3 +69,6 @@ public class MailMessage {

View File

@@ -53,7 +53,7 @@ public class ImageToVideoTask {
@Column(name = "progress")
private Integer progress = 0;
@Column(name = "result_url")
@Column(name = "result_url", columnDefinition = "TEXT")
private String resultUrl;
@Column(name = "real_task_id")

View File

@@ -203,3 +203,6 @@ public class PointsFreezeRecord {

View File

@@ -31,7 +31,7 @@ public class StoryboardVideoTask {
@Column(columnDefinition = "TEXT")
private String prompt; // 文本描述
@Column(length = 500)
@Column(name = "image_url", columnDefinition = "TEXT")
private String imageUrl; // 上传的参考图片URL可选
@Column(nullable = false, length = 10)
@@ -47,7 +47,7 @@ public class StoryboardVideoTask {
@Column(nullable = false)
private int progress; // 0-100
@Column(length = 500)
@Column(name = "result_url", columnDefinition = "TEXT")
private String resultUrl; // 分镜图URL
@Column(name = "real_task_id")

View File

@@ -271,3 +271,6 @@ public class TaskQueue {

View File

@@ -263,3 +263,6 @@ public class TaskStatus {

View File

@@ -1,8 +1,16 @@
package com.example.demo.model;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
/**
* 文生视频任务实体
*/
@@ -39,7 +47,7 @@ public class TextToVideoTask {
@Column(nullable = false)
private int progress; // 0-100
@Column(length = 500)
@Column(name = "result_url", columnDefinition = "TEXT")
private String resultUrl;
@Column(name = "real_task_id")

View File

@@ -50,7 +50,7 @@ public class User {
@Column(name = "phone", length = 20)
private String phone;
@Column(name = "avatar", length = 500)
@Column(name = "avatar", columnDefinition = "TEXT")
private String avatar;
@Column(name = "nickname", length = 100)
@@ -79,7 +79,24 @@ public class User {
@PrePersist
protected void onCreate() {
createdAt = LocalDateTime.now();
if (createdAt == null) {
createdAt = LocalDateTime.now();
}
if (updatedAt == null) {
updatedAt = LocalDateTime.now();
}
if (points == null) {
points = 50; // 默认50积分
}
if (frozenPoints == null) {
frozenPoints = 0; // 默认冻结积分为0
}
if (role == null || role.isEmpty()) {
role = "ROLE_USER"; // 默认角色
}
if (isActive == null) {
isActive = true; // 默认激活
}
}
public Long getId() {

View File

@@ -1,8 +1,16 @@
package com.example.demo.model;
import jakarta.persistence.*;
import java.time.LocalDateTime;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
/**
* 用户作品实体
* 记录用户生成的视频作品
@@ -37,10 +45,10 @@ public class UserWork {
@Column(name = "prompt", columnDefinition = "TEXT")
private String prompt; // 生成提示词
@Column(name = "result_url", length = 500)
@Column(name = "result_url", columnDefinition = "TEXT")
private String resultUrl; // 结果视频URL
@Column(name = "thumbnail_url", length = 500)
@Column(name = "thumbnail_url", columnDefinition = "TEXT")
private String thumbnailUrl; // 缩略图URL
@Column(name = "duration", length = 10)

View File

@@ -1,14 +1,15 @@
package com.example.demo.repository;
import com.example.demo.model.Payment;
import com.example.demo.model.PaymentStatus;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Optional;
import com.example.demo.model.Payment;
import com.example.demo.model.PaymentStatus;
@Repository
public interface PaymentRepository extends JpaRepository<Payment, Long> {
@@ -62,4 +63,13 @@ public interface PaymentRepository extends JpaRepository<Payment, Long> {
"FROM Payment p WHERE p.status = 'COMPLETED' AND YEAR(p.paidAt) = :year " +
"GROUP BY MONTH(p.paidAt) ORDER BY MONTH(p.paidAt)")
List<java.util.Map<String, Object>> findMonthlyRevenueByYear(@Param("year") int year);
/**
* 获取用户最近一次成功的订阅支付(按支付时间倒序)
*/
@Query("SELECT p FROM Payment p WHERE p.user.id = :userId " +
"AND p.status = :status " +
"AND (p.description LIKE '%标准版%' OR p.description LIKE '%专业版%' OR p.description LIKE '%会员%') " +
"ORDER BY p.paidAt DESC, p.createdAt DESC")
List<Payment> findLatestSuccessfulSubscriptionByUserId(@Param("userId") Long userId, @Param("status") PaymentStatus status);
}

View File

@@ -71,3 +71,6 @@ public interface TaskStatusRepository extends JpaRepository<TaskStatus, Long> {

View File

@@ -38,6 +38,9 @@ public class PlainTextPasswordEncoder implements PasswordEncoder {

View File

@@ -403,30 +403,50 @@ public class PaymentService {
}
/**
* 根据支付金额增加积分
* 根据支付信息增加积分
*/
private void addPointsForPayment(Payment payment) {
try {
BigDecimal amount = payment.getAmount();
String description = payment.getDescription() != null ? payment.getDescription() : "";
Integer pointsToAdd = 0;
// 根据支付金额确定积分奖励
if (amount.compareTo(new BigDecimal("59.00")) >= 0 && amount.compareTo(new BigDecimal("259.00")) < 0) {
// 标准版订阅 (59-258元) - 200积分
// 优先从描述中识别套餐类型
if (description.contains("标准版") || description.contains("standard")) {
// 标准版订阅 - 200积分
pointsToAdd = 200;
} else if (amount.compareTo(new BigDecimal("259.00")) >= 0) {
// 专业版订阅 (259元以上) - 1000积分
logger.info("识别到标准版订阅,奖励 200 积分");
} else if (description.contains("专业版") || description.contains("premium")) {
// 专业版订阅 - 1000积分
pointsToAdd = 1000;
logger.info("识别到专业版订阅,奖励 1000 积分");
} else {
// 如果描述中没有套餐信息,根据金额判断
// 标准版订阅 (59-258元) - 200积分
if (amount.compareTo(new BigDecimal("59.00")) >= 0 && amount.compareTo(new BigDecimal("259.00")) < 0) {
pointsToAdd = 200;
logger.info("根据金额 {} 判断为标准版订阅,奖励 200 积分", amount);
}
// 专业版订阅 (259元以上) - 1000积分
else if (amount.compareTo(new BigDecimal("259.00")) >= 0) {
pointsToAdd = 1000;
logger.info("根据金额 {} 判断为专业版订阅,奖励 1000 积分", amount);
} else {
logger.warn("支付金额 {} 不在已知套餐范围内,不增加积分", amount);
}
}
if (pointsToAdd > 0) {
userService.addPoints(payment.getUser().getId(), pointsToAdd);
logger.info("用户 {} 支付 {} 元,获得 {} 积分",
logger.info("用户 {} 支付 {} 元,成功获得 {} 积分",
payment.getUser().getUsername(), amount, pointsToAdd);
} else {
logger.warn("⚠️ 用户 {} 支付 {} 元,但未获得积分(描述: {}",
payment.getUser().getUsername(), amount, description);
}
} catch (Exception e) {
logger.error("增加积分失败:", e);
logger.error("增加积分失败:", e);
// 不抛出异常,避免影响支付流程
}
}

View File

@@ -86,17 +86,41 @@ public class RealAIService {
.body(requestBody)
.asString();
if (response.getStatus() == 200 && response.getBody() != null) {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
Integer code = (Integer) responseBody.get("code");
// 添加响应调试日志
logger.info("API响应状态: {}", response.getStatus());
String responseBodyStr = response.getBody();
logger.info("API响应内容前500字符: {}", responseBodyStr != null && responseBodyStr.length() > 500 ?
responseBodyStr.substring(0, 500) : responseBodyStr);
if (response.getStatus() == 200 && responseBodyStr != null) {
// 检查响应是否为HTML可能是认证失败或API端点错误
String trimmedResponse = responseBodyStr.trim();
String lowerResponse = trimmedResponse.toLowerCase();
if (lowerResponse.startsWith("<!") || lowerResponse.startsWith("<html") ||
lowerResponse.contains("<!doctype") || (!trimmedResponse.startsWith("{") && !trimmedResponse.startsWith("["))) {
logger.error("API返回HTML页面而不是JSON可能是认证失败或API端点错误");
logger.error("响应前100字符: {}", trimmedResponse.length() > 100 ? trimmedResponse.substring(0, 100) : trimmedResponse);
logger.error("请检查1) API密钥是否正确 2) API端点URL是否正确 3) API服务是否正常运行");
throw new RuntimeException("API返回HTML页面可能是认证失败。请检查API密钥和端点配置");
}
if (code != null && code == 200) {
logger.info("图生视频任务提交成功: {}", responseBody);
return responseBody;
} else {
logger.error("图生视频任务提交失败: {}", responseBody);
throw new RuntimeException("任务提交失败: " + responseBody.get("message"));
try {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(responseBodyStr, Map.class);
Integer code = (Integer) responseBody.get("code");
if (code != null && code == 200) {
logger.info("图生视频任务提交成功: {}", responseBody);
return responseBody;
} else {
logger.error("图生视频任务提交失败: {}", responseBody);
throw new RuntimeException("任务提交失败: " + responseBody.get("message"));
}
} catch (com.fasterxml.jackson.core.JsonParseException e) {
logger.error("解析API响应为JSON失败响应内容可能是HTML或其他格式", e);
logger.error("响应内容前200字符: {}", responseBodyStr.length() > 200 ?
responseBodyStr.substring(0, 200) : responseBodyStr);
throw new RuntimeException("API返回非JSON响应可能是认证失败。请检查API密钥和端点配置");
}
} else {
logger.error("图生视频任务提交失败HTTP状态: {}", response.getStatus());
@@ -120,21 +144,25 @@ public class RealAIService {
try {
// 根据参数选择可用的模型
String modelName = selectAvailableTextToVideoModel(aspectRatio, duration, hdMode);
// 根据分辨率选择size参数
String size = convertAspectRatioToSize(aspectRatio, hdMode);
// 添加调试日志
logger.info("提交文生视频任务请求: model={}, prompt={}, size={}, seconds={}",
modelName, prompt, size, duration);
logger.info("提交文生视频任务请求: model={}, prompt={}, aspectRatio={}, duration={}, hd={}",
modelName, prompt, aspectRatio, duration, hdMode);
logger.info("选择的模型: {}", modelName);
logger.info("API端点: {}", aiApiBaseUrl + "/user/ai/tasks/submit");
logger.info("API端点: {}", aiApiBaseUrl + "/v2/videos/generations");
logger.info("使用API密钥: {}", aiApiKey.substring(0, Math.min(10, aiApiKey.length())) + "...");
String url = aiApiBaseUrl + "/user/ai/tasks/submit";
String requestBody = String.format("{\"modelName\":\"%s\",\"prompt\":\"%s\",\"aspectRatio\":\"%s\",\"imageToVideo\":false}",
modelName, prompt, aspectRatio);
String url = aiApiBaseUrl + "/v2/videos/generations";
// 构建请求体参考Comfly项目的格式
Map<String, Object> requestBodyMap = new HashMap<>();
requestBodyMap.put("prompt", prompt);
requestBodyMap.put("model", modelName);
requestBodyMap.put("aspect_ratio", aspectRatio);
requestBodyMap.put("duration", duration);
requestBodyMap.put("hd", hdMode);
String requestBody = objectMapper.writeValueAsString(requestBodyMap);
logger.info("请求体: {}", requestBody);
HttpResponse<String> response = Unirest.post(url)
@@ -145,19 +173,44 @@ public class RealAIService {
// 添加响应调试日志
logger.info("API响应状态: {}", response.getStatus());
logger.info("API响应内容: {}", response.getBody());
String responseBodyStr = response.getBody();
logger.info("API响应内容前500字符: {}", responseBodyStr != null && responseBodyStr.length() > 500 ?
responseBodyStr.substring(0, 500) : responseBodyStr);
if (response.getStatus() == 200 && response.getBody() != null) {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
Integer code = (Integer) responseBody.get("code");
if (response.getStatus() == 200 && responseBodyStr != null) {
// 检查响应是否为HTML可能是认证失败或API端点错误
String trimmedResponse = responseBodyStr.trim();
String lowerResponse = trimmedResponse.toLowerCase();
if (lowerResponse.startsWith("<!") || lowerResponse.startsWith("<html") ||
lowerResponse.contains("<!doctype") || (!trimmedResponse.startsWith("{") && !trimmedResponse.startsWith("["))) {
logger.error("API返回HTML页面而不是JSON可能是认证失败或API端点错误");
logger.error("响应前100字符: {}", trimmedResponse.length() > 100 ? trimmedResponse.substring(0, 100) : trimmedResponse);
logger.error("请检查1) API密钥是否正确 2) API端点URL是否正确 3) API服务是否正常运行");
throw new RuntimeException("API返回HTML页面可能是认证失败。请检查API密钥和端点配置");
}
if (code != null && code == 200) {
logger.info("文生视频任务提交成功: {}", responseBody);
return responseBody;
} else {
logger.error("文生视频任务提交失败: {}", responseBody);
throw new RuntimeException("任务提交失败: " + responseBody.get("message"));
try {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(responseBodyStr, Map.class);
// 参考Comfly项目响应格式为 {"task_id": "xxx"}
if (responseBody.containsKey("task_id")) {
logger.info("文生视频任务提交成功task_id: {}", responseBody.get("task_id"));
// 转换为统一的响应格式
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("data", responseBody);
result.put("task_id", responseBody.get("task_id"));
return result;
} else {
logger.error("文生视频任务提交失败响应中缺少task_id: {}", responseBody);
throw new RuntimeException("任务提交失败: 响应格式不正确");
}
} catch (com.fasterxml.jackson.core.JsonParseException e) {
logger.error("解析API响应为JSON失败响应内容可能是HTML或其他格式", e);
logger.error("响应内容前200字符: {}", responseBodyStr.length() > 200 ?
responseBodyStr.substring(0, 200) : responseBodyStr);
throw new RuntimeException("API返回非JSON响应可能是认证失败。请检查API密钥和端点配置");
}
} else {
logger.error("文生视频任务提交失败HTTP状态: {}", response.getStatus());
@@ -175,25 +228,34 @@ public class RealAIService {
/**
* 查询任务状态
* 参考Comfly项目使用 /v2/videos/generations/{task_id} 端点
*/
public Map<String, Object> getTaskStatus(String taskId) {
try {
String url = aiApiBaseUrl + "/user/ai/tasks/" + taskId;
String url = aiApiBaseUrl + "/v2/videos/generations/" + taskId;
logger.debug("查询任务状态: {}", url);
HttpResponse<String> response = Unirest.get(url)
.header("Authorization", "Bearer " + aiApiKey)
.header("Content-Type", "application/json")
.asString();
if (response.getStatus() == 200 && response.getBody() != null) {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
Integer code = (Integer) responseBody.get("code");
String responseBodyStr = response.getBody();
logger.info("查询任务状态API响应前500字符: {}",
responseBodyStr != null && responseBodyStr.length() > 500 ?
responseBodyStr.substring(0, 500) : responseBodyStr);
if (code != null && code == 200) {
return responseBody;
} else {
logger.error("查询任务状态失败: {}", responseBody);
throw new RuntimeException("查询任务状态失败: " + responseBody.get("message"));
}
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(responseBodyStr, Map.class);
logger.info("解析后的任务状态响应: {}", responseBody);
// 参考Comfly项目响应格式为 {"status": "SUCCESS", "data": {"output": "video_url"}, ...}
// 转换为统一的响应格式
Map<String, Object> result = new HashMap<>();
result.put("code", 200);
result.put("data", responseBody);
return result;
} else {
logger.error("查询任务状态失败HTTP状态: {}", response.getStatus());
throw new RuntimeException("查询任务状态失败HTTP状态: " + response.getStatus());
@@ -273,41 +335,13 @@ public class RealAIService {
/**
* 获取可用的模型列表
* 注意Comfly项目可能不需要这个端点直接使用默认模型选择逻辑
*/
public Map<String, Object> getAvailableModels() {
try {
String url = aiApiBaseUrl + "/user/ai/models";
logger.info("正在调用外部API获取模型列表: {}", url);
logger.info("使用API密钥: {}", aiApiKey.substring(0, Math.min(10, aiApiKey.length())) + "...");
HttpResponse<String> response = Unirest.get(url)
.header("Authorization", "Bearer " + aiApiKey)
.asString();
logger.info("API响应状态: {}", response.getStatus());
logger.info("API响应内容: {}", response.getBody());
if (response.getStatus() == 200 && response.getBody() != null) {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
Integer code = (Integer) responseBody.get("code");
if (code != null && code == 200) {
logger.info("成功获取模型列表");
return responseBody;
} else {
logger.error("API返回错误代码: {}, 响应: {}", code, responseBody);
}
} else {
logger.error("API调用失败HTTP状态: {}, 响应: {}", response.getStatus(), response.getBody());
}
return null;
} catch (UnirestException e) {
logger.error("获取模型列表失败", e);
return null;
} catch (Exception e) {
logger.error("获取模型列表失败", e);
return null;
}
// 暂时不调用模型列表API直接返回null让调用方使用默认逻辑
// 因为Comfly项目直接使用sora-2或sora-2-pro不需要查询模型列表
logger.debug("跳过模型列表查询,直接使用默认模型选择逻辑");
return null;
}
/**
@@ -362,15 +396,17 @@ public class RealAIService {
/**
* 根据参数选择文生视频模型(默认逻辑)
* 参考Comfly项目使用 sora-2 或 sora-2-pro
*/
private String selectTextToVideoModel(String aspectRatio, String duration, boolean hdMode) {
String size = hdMode ? "large" : "small";
String orientation = "9:16".equals(aspectRatio) || "3:4".equals(aspectRatio) ? "portrait" : "landscape";
// 根据API返回的模型列表只支持10s和15s
String actualDuration = "5".equals(duration) ? "10" : duration;
return String.format("sc_sora2_text_%s_%ss_%s", orientation, actualDuration, size);
// 参考Comfly项目
// - sora-2: 支持10s和15s不支持25s和HD
// - sora-2-pro: 支持10s、15s和25s支持HD
if ("25".equals(duration) || hdMode) {
return "sora-2-pro";
}
// aspectRatio参数未使用但保留以保持方法签名一致
return "sora-2";
}
/**
@@ -397,31 +433,37 @@ public class RealAIService {
* @return API响应包含多张图片的URL
*/
public Map<String, Object> submitTextToImageTask(String prompt, String aspectRatio, int numImages) {
return submitTextToImageTask(prompt, aspectRatio, numImages, false);
}
/**
* 提交文生图任务使用banana模型
* 参考Comfly项目的Comfly_nano_banana_edit节点实现
*/
public Map<String, Object> submitTextToImageTask(String prompt, String aspectRatio, int numImages, boolean hdMode) {
try {
logger.info("提交文生图任务: prompt={}, aspectRatio={}, numImages={}", prompt, aspectRatio, numImages);
logger.info("提交文生图任务banana模型: prompt={}, aspectRatio={}, hdMode={}",
prompt, aspectRatio, hdMode);
// 限制生成图片数量在1-12之间参考Comfly项目的限制
if (numImages < 1) {
numImages = 1;
} else if (numImages > 12) {
numImages = 12;
}
// 根据aspectRatio转换尺寸参考Comfly项目的尺寸映射
String size = convertAspectRatioToImageSize(aspectRatio);
// 注意banana模型一次只生成1张图片numImages参数用于兼容性实际请求中不使用
// 参考Comfly_nano_banana_edit节点每次调用只生成1张图片
// 使用文生图的API端点Comfly API
// 参考Comfly_nano_banana_edit节点使用 /v1/images/generations 端点
String url = aiImageApiBaseUrl + "/v1/images/generations";
// 构建请求体参考Comfly_qwen_image节点的参数设置
// 根据hdMode选择模型参考Comfly_nano_banana_edit节点
// nano-banana: 标准模式
// nano-banana-hd: 高清模式
String model = hdMode ? "nano-banana-hd" : "nano-banana";
// 构建请求体参考Comfly_nano_banana_edit节点的参数设置
// 注意banana模型不需要n参数每次只生成1张图片
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("prompt", prompt);
requestBody.put("size", size);
requestBody.put("model", "qwen-image");
requestBody.put("n", numImages); // 支持生成多张图片
requestBody.put("response_format", "url");
// 添加guidance_scale参数以提高图片质量参考Comfly项目
requestBody.put("guidance_scale", 2.5);
requestBody.put("model", model);
requestBody.put("aspect_ratio", aspectRatio); // 直接使用aspect_ratio不需要转换为size
requestBody.put("response_format", "url"); // 可选url 或 b64_json
String requestBodyJson = objectMapper.writeValueAsString(requestBody);
@@ -436,19 +478,54 @@ public class RealAIService {
.asString();
logger.info("文生图API响应状态: {}", response.getStatus());
logger.info("文生图API响应内容: {}", response.getBody());
String responseBodyStr = response.getBody();
logger.info("文生图API响应内容前500字符: {}", responseBodyStr != null && responseBodyStr.length() > 500 ?
responseBodyStr.substring(0, 500) : responseBodyStr);
if (response.getStatus() == 200 && response.getBody() != null) {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(response.getBody(), Map.class);
if (response.getStatus() == 200 && responseBodyStr != null) {
// 检查响应是否为HTML可能是认证失败或API端点错误
String trimmedResponse = responseBodyStr.trim();
String lowerResponse = trimmedResponse.toLowerCase();
if (lowerResponse.startsWith("<!") || lowerResponse.startsWith("<html") ||
lowerResponse.contains("<!doctype") || (!trimmedResponse.startsWith("{") && !trimmedResponse.startsWith("["))) {
logger.error("API返回HTML页面而不是JSON可能是认证失败或API端点错误");
logger.error("响应前100字符: {}", trimmedResponse.length() > 100 ? trimmedResponse.substring(0, 100) : trimmedResponse);
logger.error("请检查1) API密钥是否正确 2) API端点URL是否正确 3) API服务是否正常运行");
throw new RuntimeException("API返回HTML页面可能是认证失败。请检查API密钥和端点配置");
}
// 检查是否有data字段
if (responseBody.get("data") != null) {
logger.info("文生图任务提交成功: {}", responseBody);
return responseBody;
} else {
logger.error("文生图任务提交失败: {}", responseBody);
throw new RuntimeException("任务提交失败: " + responseBody.get("message"));
try {
@SuppressWarnings("unchecked")
Map<String, Object> responseBody = objectMapper.readValue(responseBodyStr, Map.class);
// 检查是否有data字段且不为空参考Comfly_nano_banana_edit节点的响应处理
Object dataObj = responseBody.get("data");
if (dataObj != null) {
// 检查data是否为列表且不为空
if (dataObj instanceof List) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> dataList = (List<Map<String, Object>>) dataObj;
if (dataList.isEmpty()) {
logger.error("文生图任务提交失败: data字段为空列表");
throw new RuntimeException("任务提交失败: API返回空的data列表");
}
}
logger.info("文生图任务提交成功: {}", responseBody);
return responseBody;
} else {
logger.error("文生图任务提交失败: 响应中没有data字段响应内容: {}", responseBody);
Object errorMessage = responseBody.get("message");
if (errorMessage != null) {
throw new RuntimeException("任务提交失败: " + errorMessage);
} else {
throw new RuntimeException("任务提交失败: API响应中没有data字段");
}
}
} catch (com.fasterxml.jackson.core.JsonParseException e) {
logger.error("解析API响应为JSON失败响应内容可能是HTML或其他格式", e);
logger.error("响应内容前200字符: {}", responseBodyStr.length() > 200 ?
responseBodyStr.substring(0, 200) : responseBodyStr);
throw new RuntimeException("API返回非JSON响应可能是认证失败。请检查API密钥和端点配置");
}
} else {
logger.error("文生图任务提交失败HTTP状态: {}", response.getStatus());

View File

@@ -100,60 +100,113 @@ public class StoryboardVideoService {
taskRepository.flush(); // 强制刷新到数据库
// 调用真实文生图API生成多张分镜图
// 参考Comfly项目如果API不支持一次生成多张图片则多次调用生成多张
logger.info("分镜视频任务已提交正在调用文生图API生成{}张分镜图...", DEFAULT_STORYBOARD_IMAGES);
Map<String, Object> apiResponse = realAIService.submitTextToImageTask(
task.getPrompt(),
task.getAspectRatio(),
DEFAULT_STORYBOARD_IMAGES // 生成多张图片用于分镜图
);
// 收集所有图片URL
List<String> imageUrls = new ArrayList<>();
// 从API响应中提取所有图片URL
@SuppressWarnings("unchecked")
List<Map<String, Object>> data = (List<Map<String, Object>>) apiResponse.get("data");
if (data != null && !data.isEmpty()) {
// 收集所有图片URL
List<String> imageUrls = new ArrayList<>();
for (Map<String, Object> imageData : data) {
String imageUrl = null;
if (imageData.get("url") != null) {
imageUrl = (String) imageData.get("url");
} else if (imageData.get("b64_json") != null) {
// base64编码的图片
String base64Data = (String) imageData.get("b64_json");
imageUrl = "data:image/png;base64," + base64Data;
// 参考Comfly项目多次调用API生成多张图片因为Comfly API可能不支持一次生成多张
for (int i = 0; i < DEFAULT_STORYBOARD_IMAGES; i++) {
try {
logger.info("生成第{}张分镜图(共{}张)...", i + 1, DEFAULT_STORYBOARD_IMAGES);
// 每次调用生成1张图片使用banana模型
Map<String, Object> apiResponse = realAIService.submitTextToImageTask(
task.getPrompt(),
task.getAspectRatio(),
1, // 每次生成1张图片
task.isHdMode() // 使用任务的hdMode参数选择模型
);
// 检查API响应是否为空
if (apiResponse == null) {
logger.warn("第{}张图片API响应为null跳过", i + 1);
continue;
}
if (imageUrl != null) {
imageUrls.add(imageUrl);
// 从API响应中提取图片URL
// 参考Comfly_nano_banana_edit节点响应格式为 {"data": [{"url": "...", "b64_json": "..."}]}
Object dataObj = apiResponse.get("data");
if (dataObj instanceof List) {
@SuppressWarnings("unchecked")
List<Map<String, Object>> data = (List<Map<String, Object>>) dataObj;
if (!data.isEmpty()) {
// 提取第一张图片的URL因为每次只生成1张
Map<String, Object> imageData = data.get(0);
if (imageData == null) {
logger.warn("第{}张图片data第一个元素为null跳过", i + 1);
continue;
}
String imageUrl = null;
Object urlObj = imageData.get("url");
Object b64JsonObj = imageData.get("b64_json");
if (urlObj != null) {
imageUrl = urlObj.toString();
} else if (b64JsonObj != null) {
// base64编码的图片
String base64Data = b64JsonObj.toString();
imageUrl = "data:image/png;base64," + base64Data;
}
if (imageUrl != null && !imageUrl.isEmpty()) {
imageUrls.add(imageUrl);
logger.info("成功获取第{}张分镜图", i + 1);
} else {
logger.warn("第{}张图片URL为空跳过", i + 1);
}
} else {
logger.warn("第{}张图片API响应data为空列表跳过", i + 1);
}
} else {
logger.warn("第{}张图片API响应data格式不正确不是列表跳过", i + 1);
}
// 在多次调用之间添加短暂延迟避免API限流
if (i < DEFAULT_STORYBOARD_IMAGES - 1) {
Thread.sleep(500); // 延迟500ms
}
} catch (Exception e) {
logger.error("生成第{}张分镜图失败: {}", i + 1, e.getMessage());
// 继续生成其他图片,不因单张失败而终止整个流程
}
if (imageUrls.isEmpty()) {
throw new RuntimeException("未能从API响应中提取任何图片URL");
}
logger.info("成功获取{}张图片,开始拼接成分镜图网格...", imageUrls.size());
// 拼接多张图片成网格
String mergedImageUrl = imageGridService.mergeImagesToGrid(imageUrls, 0); // 0表示自动计算列数
// 重新加载任务因为之前的flush可能使实体detached
task = taskRepository.findByTaskId(taskId)
.orElseThrow(() -> new RuntimeException("任务未找到: " + taskId));
// 设置拼接后的结果图片URL
task.setResultUrl(mergedImageUrl);
task.setRealTaskId(taskId + "_image");
task.updateStatus(StoryboardVideoTask.TaskStatus.COMPLETED);
task.updateProgress(100);
taskRepository.save(task);
logger.info("分镜图生成并拼接完成任务ID: {}, 共生成{}张图片", taskId, imageUrls.size());
} else {
throw new RuntimeException("API返回的图片数据为空");
}
if (imageUrls.isEmpty()) {
throw new RuntimeException("未能从API响应中提取任何图片URL");
}
if (imageUrls.size() < DEFAULT_STORYBOARD_IMAGES) {
logger.warn("只生成了{}张图片,少于预期的{}张", imageUrls.size(), DEFAULT_STORYBOARD_IMAGES);
}
logger.info("成功获取{}张图片,开始拼接成分镜图网格...", imageUrls.size());
// 拼接多张图片成网格
String mergedImageUrl = imageGridService.mergeImagesToGrid(imageUrls, 0); // 0表示自动计算列数
// 检查拼接后的图片URL是否有效
if (mergedImageUrl == null || mergedImageUrl.isEmpty()) {
throw new RuntimeException("图片拼接失败: 返回的图片URL为空");
}
// 重新加载任务因为之前的flush可能使实体detached
task = taskRepository.findByTaskId(taskId)
.orElseThrow(() -> new RuntimeException("任务未找到: " + taskId));
// 设置拼接后的结果图片URL
task.setResultUrl(mergedImageUrl);
task.setRealTaskId(taskId + "_image");
task.updateStatus(StoryboardVideoTask.TaskStatus.COMPLETED);
task.updateProgress(100);
taskRepository.save(task);
logger.info("分镜图生成并拼接完成任务ID: {}, 共生成{}张图片", taskId, imageUrls.size());
} catch (Exception e) {
logger.error("处理分镜视频任务失败: {}", taskId, e);
try {

View File

@@ -171,6 +171,8 @@ public class TaskQueueService {
apiResponse = processImageToVideoTask(taskQueue);
}
logger.info("收到API响应准备提取任务ID: taskId={}, response={}", taskQueue.getTaskId(), apiResponse);
// 提取真实任务ID
String realTaskId = extractRealTaskId(apiResponse);
if (realTaskId != null) {
@@ -178,6 +180,7 @@ public class TaskQueueService {
taskQueueRepository.save(taskQueue);
logger.info("任务 {} 已提交到外部API真实任务ID: {}", taskQueue.getTaskId(), realTaskId);
} else {
logger.error("无法提取任务IDAPI响应: {}", apiResponse);
throw new RuntimeException("API未返回有效的任务ID");
}
@@ -289,32 +292,89 @@ public class TaskQueueService {
* 从API响应中提取真实任务ID
*/
private String extractRealTaskId(Map<String, Object> apiResponse) {
if (apiResponse == null || !apiResponse.containsKey("data")) {
if (apiResponse == null) {
logger.warn("API响应为null无法提取任务ID");
return null;
}
logger.debug("提取任务IDAPI响应键: {}", apiResponse.keySet());
// 首先检查顶层是否有task_id新API格式
if (apiResponse.containsKey("task_id")) {
Object taskIdObj = apiResponse.get("task_id");
logger.debug("找到顶层task_id: {} (类型: {})", taskIdObj, taskIdObj != null ? taskIdObj.getClass().getName() : "null");
if (taskIdObj != null) {
String taskId = taskIdObj.toString();
logger.info("提取到任务ID: {}", taskId);
return taskId;
}
}
// 然后检查data字段
if (!apiResponse.containsKey("data")) {
logger.warn("API响应中没有data字段也无法找到顶层task_id");
return null;
}
Object data = apiResponse.get("data");
logger.debug("data字段类型: {}", data != null ? data.getClass().getName() : "null");
if (data instanceof Map) {
Map<?, ?> dataMap = (Map<?, ?>) data;
String taskNo = (String) dataMap.get("taskNo");
if (taskNo != null) {
logger.debug("data字段是Map键: {}", dataMap.keySet());
// 检查新格式的task_id
Object taskIdObj = dataMap.get("task_id");
if (taskIdObj != null) {
String taskId = taskIdObj.toString();
logger.info("从data中提取到任务ID: {}", taskId);
return taskId;
}
// 检查旧格式的taskNo
Object taskNoObj = dataMap.get("taskNo");
if (taskNoObj != null) {
String taskNo = taskNoObj.toString();
logger.info("从data中提取到任务编号: {}", taskNo);
return taskNo;
}
return (String) dataMap.get("taskId");
// 检查旧格式的taskId
Object taskIdObj2 = dataMap.get("taskId");
if (taskIdObj2 != null) {
String taskId = taskIdObj2.toString();
logger.info("从data中提取到任务ID(旧格式): {}", taskId);
return taskId;
}
} else if (data instanceof List) {
List<?> dataList = (List<?>) data;
if (!dataList.isEmpty()) {
Object firstElement = dataList.get(0);
if (firstElement instanceof Map) {
Map<?, ?> firstMap = (Map<?, ?>) firstElement;
String taskNo = (String) firstMap.get("taskNo");
if (taskNo != null) {
// 检查新格式的task_id
Object taskIdObj = firstMap.get("task_id");
if (taskIdObj != null) {
String taskId = taskIdObj.toString();
logger.info("从data列表第一个元素提取到任务ID: {}", taskId);
return taskId;
}
// 检查旧格式的taskNo
Object taskNoObj = firstMap.get("taskNo");
if (taskNoObj != null) {
String taskNo = taskNoObj.toString();
logger.info("从data列表第一个元素提取到任务编号: {}", taskNo);
return taskNo;
}
return (String) firstMap.get("taskId");
// 检查旧格式的taskId
Object taskIdObj2 = firstMap.get("taskId");
if (taskIdObj2 != null) {
String taskId = taskIdObj2.toString();
logger.info("从data列表第一个元素提取到任务ID(旧格式): {}", taskId);
return taskId;
}
}
}
}
logger.warn("无法从API响应中提取任务ID响应内容: {}", apiResponse);
return null;
}
@@ -380,45 +440,138 @@ public class TaskQueueService {
if (statusResponse != null && statusResponse.containsKey("data")) {
Object data = statusResponse.get("data");
logger.debug("data字段类型: {}, 值: {}", data != null ? data.getClass().getName() : "null", data);
Map<?, ?> taskData = null;
// 处理不同的响应格式
if (data instanceof Map) {
taskData = (Map<?, ?>) data;
logger.debug("taskData是Map键: {}", taskData.keySet());
} else if (data instanceof List) {
List<?> dataList = (List<?>) data;
if (!dataList.isEmpty()) {
Object firstElement = dataList.get(0);
if (firstElement instanceof Map) {
taskData = (Map<?, ?>) firstElement;
logger.debug("taskData是List的第一个元素键: {}", taskData.keySet());
}
}
}
if (taskData != null) {
String status = (String) taskData.get("status");
String resultUrl = (String) taskData.get("resultUrl");
// 支持大小写不敏感的状态检查
if (status != null) {
status = status.toUpperCase();
}
logger.info("提取到的任务状态: {}", status);
// 提取结果URL - 支持多种格式
String resultUrl = null;
// 1. 检查嵌套的data.data.output最深层嵌套格式
Object nestedData = taskData.get("data");
if (nestedData instanceof Map) {
Map<?, ?> nestedDataMap = (Map<?, ?>) nestedData;
logger.debug("找到嵌套的data字段键: {}", nestedDataMap.keySet());
Object innerData = nestedDataMap.get("data");
if (innerData instanceof Map) {
Map<?, ?> innerDataMap = (Map<?, ?>) innerData;
logger.debug("找到深层嵌套的data字段键: {}", innerDataMap.keySet());
Object output = innerDataMap.get("output");
if (output != null) {
resultUrl = output.toString();
logger.info("从data.data.output提取到resultUrl: {}", resultUrl);
}
}
// 如果深层嵌套没有找到检查当前层的output
if (resultUrl == null) {
Object output = nestedDataMap.get("output");
if (output != null) {
resultUrl = output.toString();
logger.info("从data.output提取到resultUrl: {}", resultUrl);
}
}
}
// 2. 检查直接的resultUrl字段旧格式
if (resultUrl == null) {
Object resultUrlObj = taskData.get("resultUrl");
if (resultUrlObj != null) {
resultUrl = resultUrlObj.toString();
logger.info("从resultUrl字段提取到resultUrl: {}", resultUrl);
}
}
// 3. 检查output字段另一种格式
if (resultUrl == null) {
Object output = taskData.get("output");
if (output != null) {
resultUrl = output.toString();
logger.info("从output字段提取到resultUrl: {}", resultUrl);
}
}
// 4. 检查其他可能的字段名
if (resultUrl == null) {
Object videoUrl = taskData.get("video_url");
if (videoUrl != null) {
resultUrl = videoUrl.toString();
logger.info("从video_url字段提取到resultUrl: {}", resultUrl);
}
}
if (resultUrl == null) {
Object url = taskData.get("url");
if (url != null) {
resultUrl = url.toString();
logger.info("从url字段提取到resultUrl: {}", resultUrl);
}
}
if (resultUrl == null) {
Object result = taskData.get("result");
if (result != null) {
resultUrl = result.toString();
logger.info("从result字段提取到resultUrl: {}", resultUrl);
}
}
// 提取错误消息 - 支持多种字段名
String errorMessage = (String) taskData.get("errorMessage");
if (errorMessage == null) {
errorMessage = (String) taskData.get("fail_reason");
}
if (errorMessage == null) {
errorMessage = (String) taskData.get("error");
}
logger.info("任务状态更新: taskId={}, status={}, resultUrl={}, errorMessage={}",
taskQueue.getTaskId(), status, resultUrl, errorMessage);
// 更新任务状态
if ("completed".equals(status) || "success".equals(status)) {
logger.info("任务完成: {}", taskQueue.getTaskId());
// 更新任务状态 - 支持多种状态值
if ("COMPLETED".equals(status) || "SUCCESS".equals(status)) {
logger.info("任务完成: {}, resultUrl: {}", taskQueue.getTaskId(), resultUrl);
updateTaskAsCompleted(taskQueue, resultUrl);
} else if ("failed".equals(status) || "error".equals(status)) {
} else if ("FAILED".equals(status) || "ERROR".equals(status)) {
logger.warn("任务失败: {}, 错误: {}", taskQueue.getTaskId(), errorMessage);
updateTaskAsFailed(taskQueue, errorMessage);
} else {
logger.info("任务继续处理中: {}, 状态: {}", taskQueue.getTaskId(), status);
// IN_PROGRESS, PROCESSING, PENDING 等状态继续处理
logger.info("任务继续处理中: {}, 状态: {}, resultUrl: {}", taskQueue.getTaskId(), status, resultUrl);
// 即使任务还在处理中如果有结果URL也更新任务记录用于显示进度
if (resultUrl != null && !resultUrl.isEmpty()) {
logger.info("任务处理中但已有resultUrl更新任务记录: {}", resultUrl);
updateOriginalTaskStatus(taskQueue, "PROCESSING", resultUrl, null);
}
}
} else {
logger.warn("无法解析任务数据: taskId={}", taskQueue.getTaskId());
logger.warn("无法解析任务数据: taskId={}, data类型: {}",
taskQueue.getTaskId(), data != null ? data.getClass().getName() : "null");
}
} else {
logger.warn("外部API响应格式异常: taskId={}, response={}",
taskQueue.getTaskId(), statusResponse);
logger.warn("外部API响应格式异常: taskId={}, response={}, hasData={}",
taskQueue.getTaskId(), statusResponse,
statusResponse != null && statusResponse.containsKey("data"));
}
// 检查是否超时
@@ -437,6 +590,12 @@ public class TaskQueueService {
*/
private void updateTaskAsCompleted(TaskQueue taskQueue, String resultUrl) {
try {
if (resultUrl == null || resultUrl.isEmpty()) {
logger.warn("任务 {} 标记为完成但resultUrl为空可能无法正常显示视频", taskQueue.getTaskId());
} else {
logger.info("任务 {} 已完成resultUrl: {}", taskQueue.getTaskId(), resultUrl);
}
taskQueue.updateStatus(TaskQueue.QueueStatus.COMPLETED);
taskQueueRepository.save(taskQueue);
@@ -446,15 +605,19 @@ public class TaskQueueService {
// 更新原始任务状态
updateOriginalTaskStatus(taskQueue, "COMPLETED", resultUrl, null);
logger.info("任务 {} 完成", taskQueue.getTaskId());
logger.info("任务 {} 状态更新完成", taskQueue.getTaskId());
// 创建用户作品 - 在最后执行,避免影响主要流程
try {
UserWork work = userWorkService.createWorkFromTask(taskQueue.getTaskId(), resultUrl);
logger.info("创建用户作品成功: {}, 任务ID: {}", work.getId(), taskQueue.getTaskId());
} catch (Exception workException) {
logger.error("创建用户作品失败: {}, 但不影响任务完成状态", taskQueue.getTaskId(), workException);
// 作品创建失败不影响任务完成状态
if (resultUrl != null && !resultUrl.isEmpty()) {
try {
UserWork work = userWorkService.createWorkFromTask(taskQueue.getTaskId(), resultUrl);
logger.info("创建用户作品成功: {}, 任务ID: {}", work.getId(), taskQueue.getTaskId());
} catch (Exception workException) {
logger.error("创建用户作品失败: {}, 但不影响任务完成状态", taskQueue.getTaskId(), workException);
// 作品创建失败不影响任务完成状态
}
} else {
logger.warn("resultUrl为空跳过创建用户作品: {}", taskQueue.getTaskId());
}
} catch (Exception e) {
@@ -517,14 +680,26 @@ public class TaskQueueService {
if (taskOpt.isPresent()) {
TextToVideoTask task = taskOpt.get();
if ("COMPLETED".equals(status)) {
logger.info("更新文生视频任务为完成状态: taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
task.setResultUrl(resultUrl);
task.updateStatus(TextToVideoTask.TaskStatus.COMPLETED);
task.updateProgress(100);
textToVideoTaskRepository.save(task);
logger.info("文生视频任务resultUrl已更新: taskId={}, resultUrl={}", taskQueue.getTaskId(), task.getResultUrl());
} else if ("FAILED".equals(status) || "CANCELLED".equals(status)) {
logger.info("更新文生视频任务为失败状态: taskId={}, errorMessage={}", taskQueue.getTaskId(), errorMessage);
task.updateStatus(TextToVideoTask.TaskStatus.FAILED);
task.setErrorMessage(errorMessage);
textToVideoTaskRepository.save(task);
} else if ("PROCESSING".equals(status)) {
// 处理中状态更新resultUrl以显示进度
logger.info("更新文生视频任务处理中状态: taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
if (resultUrl != null && !resultUrl.isEmpty()) {
task.setResultUrl(resultUrl);
textToVideoTaskRepository.save(task);
logger.info("文生视频任务resultUrl已更新(处理中): taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
}
}
textToVideoTaskRepository.save(task);
logger.info("原始文生视频任务状态已更新: {} -> {}", taskQueue.getTaskId(), status);
} else {
logger.warn("找不到原始文生视频任务: {}", taskQueue.getTaskId());
@@ -534,14 +709,26 @@ public class TaskQueueService {
if (taskOpt.isPresent()) {
ImageToVideoTask task = taskOpt.get();
if ("COMPLETED".equals(status)) {
logger.info("更新图生视频任务为完成状态: taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
task.setResultUrl(resultUrl);
task.updateStatus(ImageToVideoTask.TaskStatus.COMPLETED);
task.updateProgress(100);
imageToVideoTaskRepository.save(task);
logger.info("图生视频任务resultUrl已更新: taskId={}, resultUrl={}", taskQueue.getTaskId(), task.getResultUrl());
} else if ("FAILED".equals(status) || "CANCELLED".equals(status)) {
logger.info("更新图生视频任务为失败状态: taskId={}, errorMessage={}", taskQueue.getTaskId(), errorMessage);
task.updateStatus(ImageToVideoTask.TaskStatus.FAILED);
task.setErrorMessage(errorMessage);
imageToVideoTaskRepository.save(task);
} else if ("PROCESSING".equals(status)) {
// 处理中状态更新resultUrl以显示进度
logger.info("更新图生视频任务处理中状态: taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
if (resultUrl != null && !resultUrl.isEmpty()) {
task.setResultUrl(resultUrl);
imageToVideoTaskRepository.save(task);
logger.info("图生视频任务resultUrl已更新(处理中): taskId={}, resultUrl={}", taskQueue.getTaskId(), resultUrl);
}
}
imageToVideoTaskRepository.save(task);
logger.info("原始图生视频任务状态已更新: {} -> {}", taskQueue.getTaskId(), status);
} else {
logger.warn("找不到原始图生视频任务: {}", taskQueue.getTaskId());

View File

@@ -113,6 +113,11 @@ public class UserService {
return userRepository.findByEmail(email).orElse(null);
}
@Transactional(readOnly = true)
public User findByEmailOrNull(String email) {
return userRepository.findByEmail(email).orElse(null);
}
/**
* 根据手机号查找用户
*/

View File

@@ -36,10 +36,10 @@ tencent.ses.template-id=154360
# AI API配置
# 文生视频、图生视频、分镜视频都使用Comfly API
ai.api.base-url=https://ai.comfly.chat
ai.api.key=sk-jp1O4h5lfN3WWZReF48SDa2osm0o9alC9qetkgq3M7XUjJ4R
ai.api.key=sk-xCX1X12e8Dpj4mRJKFMxFUnV29pzJQpPeuZFGqTwYOorjvOQ
# 文生图使用Comfly API (在代码中单独配置)
ai.image.api.base-url=https://ai.comfly.chat
ai.image.api.key=sk-jp1O4h5lfN3WWZReF48SDa2osm0o9alC9qetkgq3M7XUjJ4R
ai.image.api.key=sk-xCX1X12e8Dpj4mRJKFMxFUnV29pzJQpPeuZFGqTwYOorjvOQ
# 支付宝配置 (开发环境 - 沙箱测试)
# 请替换为您的实际配置

View File

@@ -30,3 +30,6 @@ CREATE TABLE IF NOT EXISTS task_queue (

View File

@@ -29,3 +29,6 @@ CREATE TABLE IF NOT EXISTS points_freeze_records (

View File

@@ -32,3 +32,6 @@ CREATE TABLE task_status (

View File

@@ -574,6 +574,9 @@

View File

@@ -490,6 +490,9 @@

View File

@@ -529,6 +529,9 @@