更新带货视频工作流接口和页面

This commit is contained in:
super
2026-07-09 00:27:49 +08:00
parent f3a25b5b16
commit ced59b5787
50 changed files with 4772 additions and 815 deletions
@@ -0,0 +1,100 @@
package com.nanri.aiimage.modules.imagevideo.controller;
import com.nanri.aiimage.common.api.ApiResponse;
import com.nanri.aiimage.modules.imagevideo.model.dto.DouyinCopyRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoSecretSaveRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoVoiceCloneRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoVoiceDeleteRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoVoiceListRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoVoiceSynthesisRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoWorkflowResultRequest;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoWorkflowRunRequest;
import com.nanri.aiimage.modules.imagevideo.model.vo.DouyinCopyVo;
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoMediaUploadVo;
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoSecretStatusVo;
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoCozeService;
import com.nanri.aiimage.modules.imagevideo.service.ImageVideoSecretService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.Map;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/image-video")
@Tag(name = "视频复刻/图生视频", description = "视频复刻和图生视频相关 AI 接口")
public class ImageVideoController {
private final ImageVideoCozeService imageVideoCozeService;
private final ImageVideoSecretService imageVideoSecretService;
@GetMapping("/secrets")
@Operation(summary = "查询视频密钥配置状态")
public ApiResponse<ImageVideoSecretStatusVo> secretStatus(@RequestParam("user_id") Long userId) {
return ApiResponse.success(imageVideoSecretService.status(userId));
}
@PutMapping("/secrets")
@Operation(summary = "保存视频密钥配置")
public ApiResponse<ImageVideoSecretStatusVo> saveSecrets(@Valid @RequestBody ImageVideoSecretSaveRequest request) {
return ApiResponse.success(imageVideoSecretService.save(request));
}
@PostMapping("/douyin-copy")
@Operation(summary = "抖音文案识别和仿写")
public ApiResponse<DouyinCopyVo> douyinCopy(@Valid @RequestBody DouyinCopyRequest request) {
return ApiResponse.success(imageVideoCozeService.runDouyinCopy(request.getUserId(), request.getUrl()));
}
@PostMapping("/workflow/run")
@Operation(summary = "提交图生/复刻视频工作流")
public ApiResponse<Map<String, Object>> runWorkflow(@Valid @RequestBody ImageVideoWorkflowRunRequest request) {
return ApiResponse.success(imageVideoCozeService.runImageVideoWorkflow(request.getUserId(), request.getParameters()));
}
@PostMapping("/workflow/result")
@Operation(summary = "查询图生/复刻视频工作流异步结果")
public ApiResponse<Map<String, Object>> workflowResult(@Valid @RequestBody ImageVideoWorkflowResultRequest request) {
return ApiResponse.success(imageVideoCozeService.getImageVideoWorkflowResult(request.getUserId(), request.getExecuteId()));
}
@PostMapping("/media/upload")
@Operation(summary = "上传图生/复刻视频媒体素材")
public ApiResponse<ImageVideoMediaUploadVo> uploadMedia(@RequestParam("file") MultipartFile file) {
return ApiResponse.success(imageVideoCozeService.uploadMedia(file));
}
@PostMapping("/voice/list")
@Operation(summary = "查看音色")
public ApiResponse<Map<String, Object>> listVoices(@Valid @RequestBody ImageVideoVoiceListRequest request) {
return ApiResponse.success(imageVideoCozeService.runVoiceList(request.getUserId()));
}
@PostMapping("/voice/delete")
@Operation(summary = "删除音色")
public ApiResponse<Map<String, Object>> deleteVoice(@Valid @RequestBody ImageVideoVoiceDeleteRequest request) {
return ApiResponse.success(imageVideoCozeService.runVoiceDelete(request.getUserId(), request.getVoiceId()));
}
@PostMapping("/voice/clone")
@Operation(summary = "音色克隆")
public ApiResponse<Map<String, Object>> cloneVoice(@Valid @RequestBody ImageVideoVoiceCloneRequest request) {
return ApiResponse.success(imageVideoCozeService.runVoiceClone(request.getUserId(), request.getAudioUrl(), request.getVideoUrl()));
}
@PostMapping("/voice/synthesis")
@Operation(summary = "语音合成")
public ApiResponse<Map<String, Object>> synthesizeVoice(@Valid @RequestBody ImageVideoVoiceSynthesisRequest request) {
return ApiResponse.success(imageVideoCozeService.runVoiceSynthesis(request.getUserId(), request.getText(), request.getVoiceId()));
}
}
@@ -0,0 +1,9 @@
package com.nanri.aiimage.modules.imagevideo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoSecretEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ImageVideoSecretMapper extends BaseMapper<ImageVideoSecretEntity> {
}
@@ -0,0 +1,9 @@
package com.nanri.aiimage.modules.imagevideo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoWorkflowConfigEntity;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface ImageVideoWorkflowConfigMapper extends BaseMapper<ImageVideoWorkflowConfigEntity> {
}
@@ -0,0 +1,19 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
@Schema(description = "抖音文案解析和仿写请求")
public class DouyinCopyRequest {
@NotNull(message = "userId 不能为空")
@Schema(description = "用户ID", example = "1")
private Long userId;
@NotBlank(message = "url 不能为空")
@Schema(description = "抖音分享口令、短链或视频链接", example = "2.38 复制打开抖音,看看")
private String url;
}
@@ -0,0 +1,29 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
@Schema(description = "视频复刻/图生视频密钥保存请求")
public class ImageVideoSecretSaveRequest {
@NotNull(message = "userId 不能为空")
@Schema(description = "用户ID", example = "1")
private Long userId;
@Schema(description = "文案工作流 api_key;首次配置或过期后必填", example = "sk-...")
private String copyApiKey;
private String t8Key;
@Schema(description = "音色工作流 api_key;首次配置或过期后必填", example = "sk-api-...")
private String voiceApiKey;
@Schema(description = "音色 group_id;首次配置或过期后必填", example = "2030928714635682107")
private String voiceGroupId;
@NotNull(message = "有效期不能为空")
@Schema(description = "有效期天数,只允许 1、7、30", example = "7")
private Integer expireDays;
}
@@ -0,0 +1,12 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ImageVideoVoiceCloneRequest {
@NotNull(message = "userId 不能为空")
private Long userId;
private String audioUrl;
private String videoUrl;
}
@@ -0,0 +1,14 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ImageVideoVoiceDeleteRequest {
@NotNull(message = "userId 不能为空")
private Long userId;
@NotBlank(message = "voice_id 不能为空")
private String voiceId;
}
@@ -0,0 +1,10 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ImageVideoVoiceListRequest {
@NotNull(message = "userId 不能为空")
private Long userId;
}
@@ -0,0 +1,17 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ImageVideoVoiceSynthesisRequest {
@NotNull(message = "userId 不能为空")
private Long userId;
@NotBlank(message = "text 不能为空")
private String text;
@NotBlank(message = "voice_id 不能为空")
private String voiceId;
}
@@ -0,0 +1,14 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
@Data
public class ImageVideoWorkflowResultRequest {
@NotNull(message = "userId cannot be null")
private Long userId;
@NotBlank(message = "executeId cannot be blank")
private String executeId;
}
@@ -0,0 +1,16 @@
package com.nanri.aiimage.modules.imagevideo.model.dto;
import jakarta.validation.constraints.NotNull;
import lombok.Data;
import java.util.LinkedHashMap;
import java.util.Map;
@Data
public class ImageVideoWorkflowRunRequest {
@NotNull(message = "用户ID不能为空")
private Long userId;
@NotNull(message = "工作流参数不能为空")
private Map<String, Object> parameters = new LinkedHashMap<>();
}
@@ -0,0 +1,24 @@
package com.nanri.aiimage.modules.imagevideo.model.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName("biz_image_video_secret")
public class ImageVideoSecretEntity {
@TableId(type = IdType.AUTO)
private Long id;
private Long userId;
private String copyApiKey;
private String t8Key;
private String voiceApiKey;
private String voiceGroupId;
private LocalDateTime expiresAt;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
@@ -0,0 +1,18 @@
package com.nanri.aiimage.modules.imagevideo.model.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@TableName("biz_image_video_workflow_config")
public class ImageVideoWorkflowConfigEntity {
@TableId
private String configKey;
private String workflowId;
private LocalDateTime createdAt;
private LocalDateTime updatedAt;
}
@@ -0,0 +1,21 @@
package com.nanri.aiimage.modules.imagevideo.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
@Schema(description = "抖音文案解析和仿写结果")
public class DouyinCopyVo {
@Schema(description = "识别出的原始文案")
private String recognizedContent;
@Schema(description = "仿写后的文案")
private String scriptDraft;
@Schema(description = "Coze 执行 ID")
private String executeId;
@Schema(description = "Coze 调试链接")
private String debugUrl;
}
@@ -0,0 +1,11 @@
package com.nanri.aiimage.modules.imagevideo.model.vo;
import lombok.Data;
@Data
public class ImageVideoMediaUploadVo {
private String url;
private String objectKey;
private String originalFilename;
private String mediaType;
}
@@ -0,0 +1,51 @@
package com.nanri.aiimage.modules.imagevideo.model.vo;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.time.LocalDateTime;
@Data
@Schema(description = "视频复刻/图生视频密钥配置状态")
public class ImageVideoSecretStatusVo {
@Schema(description = "用户ID")
private Long userId;
@Schema(description = "是否已保存完整密钥")
private Boolean configured;
@Schema(description = "是否在有效期内")
private Boolean valid;
@Schema(description = "是否已经过期")
private Boolean expired;
@Schema(description = "文案 key 是否已保存")
private Boolean hasCopyApiKey;
private Boolean hasT8Key;
@Schema(description = "音色 key 是否已保存")
private Boolean hasVoiceApiKey;
@Schema(description = "音色 group_id 是否已保存")
private Boolean hasVoiceGroupId;
@Schema(description = "脱敏后的文案 key")
private String copyApiKeyMasked;
private String t8KeyMasked;
@Schema(description = "脱敏后的音色 key")
private String voiceApiKeyMasked;
@Schema(description = "脱敏后的音色 group_id")
private String voiceGroupIdMasked;
@Schema(description = "用户选择的有效期天数,支持 1、7、30")
private Integer expireDays;
@Schema(description = "过期时间")
private LocalDateTime expiresAt;
}
@@ -0,0 +1,663 @@
package com.nanri.aiimage.modules.imagevideo.service;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.type.TypeReference;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.config.ImageVideoProperties;
import com.nanri.aiimage.modules.file.service.oss.OssStorageService;
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoMediaUploadVo;
import com.nanri.aiimage.modules.imagevideo.model.vo.DouyinCopyVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
@Slf4j
@Service
@RequiredArgsConstructor
public class ImageVideoCozeService {
private static final List<String> ORIGINAL_FIELDS = List.of(
"recognizedContent", "recognized_content", "originalContent", "original_content",
"originContent", "origin_content", "rawContent", "raw_content", "description", "content"
);
private static final List<String> SCRIPT_FIELDS = List.of(
"scriptDraft", "script_draft", "rewrittenContent", "rewritten_content",
"fullContent", "full_content", "copywriting", "script", "output", "text", "result"
);
private final ImageVideoProperties properties;
private final ImageVideoSecretService secretService;
private final ImageVideoWorkflowConfigService workflowConfigService;
private final OssStorageService ossStorageService;
private final ObjectMapper objectMapper;
private volatile HttpClient sharedHttpClient;
public DouyinCopyVo runDouyinCopy(Long userId, String url) {
String normalizedUrl = url == null ? "" : url.trim();
if (normalizedUrl.isBlank()) {
throw new BusinessException("抖音口令或链接不能为空");
}
if (looksLikeJsonObject(normalizedUrl)) {
throw new BusinessException("请粘贴抖音分享口令或链接,不要粘贴接口返回内容");
}
if (!isLikelyDouyinSource(normalizedUrl)) {
throw new BusinessException("请粘贴包含抖音链接的分享口令");
}
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
String responseText = postWorkflow(normalizedUrl, secret.copyApiKey(), secret.t8Key(), secret.cozeToken(),
workflowConfigService.douyinCopyWorkflowId());
return parseDouyinCopyResponse(responseText);
}
public Map<String, Object> runImageVideoWorkflow(Long userId, Map<String, Object> parameters) {
if (parameters == null || parameters.isEmpty()) {
throw new BusinessException("工作流参数不能为空");
}
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
String cozeToken = firstNonBlank(secret.cozeToken());
if (!hasText(cozeToken)) {
throw new BusinessException("图生视频 Coze token 未配置");
}
Map<String, Object> resolvedParameters = new LinkedHashMap<>(parameters);
Map<String, Object> apiKeyInfo = new LinkedHashMap<>();
Object incomingApiKeyInfo = resolvedParameters.get("api_key_info");
if (incomingApiKeyInfo instanceof Map<?, ?>) {
Map<?, ?> incomingMap = (Map<?, ?>) incomingApiKeyInfo;
incomingMap.forEach((key, value) -> apiKeyInfo.put(String.valueOf(key), value));
}
String aiConductorKey = firstNonBlank(asText(apiKeyInfo.get("ai_conductor_key")),
asText(resolvedParameters.get("api_key")), secret.copyApiKey());
String t8starKey = firstNonBlank(asText(apiKeyInfo.get("t8star_key")), secret.t8Key());
if (!hasText(aiConductorKey)) {
throw new BusinessException("图生视频 ai_conductor_key 未配置");
}
if (!hasText(t8starKey)) {
throw new BusinessException("图生视频 t8star_key 未配置");
}
apiKeyInfo.put("t8star_key", t8starKey);
apiKeyInfo.put("ai_conductor_key", aiConductorKey);
resolvedParameters.put("api_key_info", apiKeyInfo);
resolvedParameters.remove("api_key");
normalizeImageVideoParameters(resolvedParameters);
String workflowId = workflowConfigService.imageVideoWorkflowId();
return postWorkflowParameters("image video", workflowId, resolvedParameters, cozeToken, true);
}
@SuppressWarnings("unchecked")
private void normalizeImageVideoParameters(Map<String, Object> parameters) {
Object faceInfo = parameters.get("face_info");
if (faceInfo instanceof Map<?, ?>) {
Map<String, Object> normalizedFaceInfo = new LinkedHashMap<>();
((Map<?, ?>) faceInfo).forEach((key, value) -> normalizedFaceInfo.put(String.valueOf(key), value));
normalizedFaceInfo.put("model_image", normalizeStringList(normalizedFaceInfo.get("model_image")));
parameters.put("face_info", normalizedFaceInfo);
}
Object procInfo = parameters.get("proc_info");
if (procInfo instanceof Map<?, ?>) {
Map<String, Object> normalizedProcInfo = new LinkedHashMap<>();
((Map<?, ?>) procInfo).forEach((key, value) -> normalizedProcInfo.put(String.valueOf(key), value));
normalizedProcInfo.put("proc_image", normalizeStringList(normalizedProcInfo.get("proc_image")));
parameters.put("proc_info", normalizedProcInfo);
}
}
private List<String> normalizeStringList(Object value) {
List<String> urls = new ArrayList<>();
if (value instanceof List<?>) {
for (Object item : (List<?>) value) {
String text = firstNonBlank(asText(item));
if (hasText(text)) {
urls.add(text);
}
}
} else {
String text = firstNonBlank(asText(value));
if (hasText(text)) {
urls.add(text);
}
}
return urls;
}
private boolean looksLikeJsonObject(String value) {
String text = firstNonBlank(value);
return text.startsWith("{") && text.endsWith("}");
}
private boolean isLikelyDouyinSource(String value) {
String text = firstNonBlank(value).toLowerCase();
return text.contains("douyin.com")
|| text.contains("v.douyin")
|| text.contains("iesdouyin")
|| text.contains("dou音")
|| text.contains("抖音");
}
public Map<String, Object> getImageVideoWorkflowResult(Long userId, String executeId) {
String resolvedExecuteId = firstNonBlank(executeId);
if (!hasText(resolvedExecuteId)) {
throw new BusinessException("execute_id cannot be blank");
}
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
String cozeToken = firstNonBlank(secret.cozeToken());
if (!hasText(cozeToken)) {
throw new BusinessException("图生视频 Coze token 未配置");
}
String workflowId = workflowConfigService.imageVideoWorkflowId();
if (!hasText(workflowId)) {
throw new BusinessException("image video workflow_id 未配置");
}
String path = firstNonBlank(properties.getCozeWorkflowHistoryPath(), "/v1/workflows/{workflow_id}/run_histories/{execute_id}")
.replace("{workflow_id}", workflowId)
.replace("{execute_id}", resolvedExecuteId);
String endpoint = joinUrl(properties.getCozeBaseUrl(), path);
String responseText = getCozeJson("image video history", endpoint, cozeToken);
return parseCozeMap("image video history", responseText);
}
public ImageVideoMediaUploadVo uploadMedia(MultipartFile file) {
if (file == null || file.isEmpty()) {
throw new BusinessException("请上传媒体文件");
}
String originalFilename = firstNonBlank(file.getOriginalFilename(), "media");
String mediaType = resolveMediaType(file.getContentType(), originalFilename);
if (!List.of("image", "video", "audio").contains(mediaType)) {
throw new BusinessException("仅支持图片、视频或音频文件");
}
File tempFile = null;
try {
String suffix = "";
int dotIndex = originalFilename.lastIndexOf('.');
if (dotIndex >= 0 && dotIndex + 1 < originalFilename.length()) {
suffix = originalFilename.substring(dotIndex);
}
tempFile = Files.createTempFile("image-video-", suffix).toFile();
file.transferTo(tempFile);
OssStorageService.UploadedResult uploaded = ossStorageService.uploadResultFileWithFreshDownloadUrl(tempFile, "IMAGE_VIDEO");
ImageVideoMediaUploadVo vo = new ImageVideoMediaUploadVo();
vo.setUrl(uploaded.downloadUrl());
vo.setObjectKey(uploaded.objectKey());
vo.setOriginalFilename(originalFilename);
vo.setMediaType(mediaType);
return vo;
} catch (Exception ex) {
throw new BusinessException("媒体上传失败: " + ex.getMessage(), ex);
} finally {
if (tempFile != null && tempFile.exists() && !tempFile.delete()) {
log.warn("[image-video] temp media delete failed path={}", tempFile.getAbsolutePath());
}
}
}
public Map<String, Object> runVoiceList(Long userId) {
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("api_key", secret.voiceApiKey());
return postWorkflowParameters("voice list", workflowConfigService.voiceListWorkflowId(), parameters, secret.cozeToken(), false);
}
public Map<String, Object> runVoiceDelete(Long userId, String voiceId) {
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("api_key", secret.voiceApiKey());
parameters.put("group_id", secret.voiceGroupId());
parameters.put("voice_id", firstNonBlank(voiceId));
return postWorkflowParameters("voice delete", workflowConfigService.voiceDeleteWorkflowId(), parameters, secret.cozeToken(), false);
}
public Map<String, Object> runVoiceClone(Long userId, String audioUrl, String videoUrl) {
String resolvedAudioUrl = firstNonBlank(audioUrl);
String resolvedVideoUrl = firstNonBlank(videoUrl);
if (!hasText(resolvedAudioUrl) && !hasText(resolvedVideoUrl)) {
throw new BusinessException("请上传音频或填写视频链接");
}
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("api_key", secret.voiceApiKey());
parameters.put("audio_url", resolvedAudioUrl);
parameters.put("video_url", resolvedVideoUrl);
return postWorkflowParameters("voice clone", workflowConfigService.voiceCloneWorkflowId(), parameters, secret.cozeToken(), false);
}
public Map<String, Object> runVoiceSynthesis(Long userId, String text, String voiceId) {
ImageVideoSecretService.ResolvedSecret secret = secretService.requireValid(userId);
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("api_key", secret.voiceApiKey());
parameters.put("group_id", secret.voiceGroupId());
parameters.put("text", firstNonBlank(text));
parameters.put("voice_id", firstNonBlank(voiceId));
return postWorkflowParameters("voice synthesis", workflowConfigService.voiceSynthesisWorkflowId(), parameters, secret.cozeToken(), false);
}
private String postWorkflow(String url, String apiKey, String t8Key, String token, String workflowId) {
String resolvedApiKey = firstNonBlank(apiKey);
String resolvedT8Key = firstNonBlank(t8Key);
String cozeToken = firstNonBlank(token);
if (!hasText(resolvedT8Key)) {
throw new BusinessException("image-video douyin copy t8_key not configured");
}
if (!hasText(resolvedApiKey)) {
throw new BusinessException("图生视频抖音文案 api_key 未配置");
}
if (!hasText(cozeToken)) {
throw new BusinessException("图生视频 Coze token 未配置");
}
Map<String, Object> parameters = new LinkedHashMap<>();
parameters.put("api_key", resolvedApiKey);
parameters.put("t8_key", resolvedT8Key);
parameters.put("url", url);
Map<String, Object> body = new LinkedHashMap<>();
String resolvedWorkflowId = firstNonBlank(workflowId, properties.getDouyinCopyWorkflowId());
body.put("workflow_id", resolvedWorkflowId);
body.put("parameters", parameters);
String endpoint = joinUrl(properties.getCozeBaseUrl(), properties.getCozeWorkflowPath());
log.info("[image-video] douyin copy coze request url={} workflowId={} inputLength={}",
endpoint, resolvedWorkflowId, url.length());
log.info("[image-video] douyin copy coze request detail endpoint={} authorization=Bearer {} body={}",
endpoint, maskForLog(cozeToken), toJsonForLog(body));
return postCozeJson("douyin copy", endpoint, body, cozeToken);
}
private Map<String, Object> postWorkflowParameters(String label, String workflowId, Map<String, Object> parameters,
String token, boolean async) {
String cozeToken = firstNonBlank(token);
if (!hasText(cozeToken)) {
throw new BusinessException("图生视频 Coze token 未配置");
}
String resolvedWorkflowId = firstNonBlank(workflowId);
if (!hasText(resolvedWorkflowId)) {
throw new BusinessException(label + " workflow_id 未配置");
}
Map<String, Object> body = new LinkedHashMap<>();
body.put("workflow_id", resolvedWorkflowId);
body.put("parameters", parameters == null ? Map.of() : parameters);
if (async) {
body.put("is_async", true);
}
String endpoint = joinUrl(properties.getCozeBaseUrl(), properties.getCozeWorkflowPath());
log.info("[image-video] {} coze request url={} workflowId={}", label, endpoint, resolvedWorkflowId);
log.info("[image-video] {} coze request detail endpoint={} authorization=Bearer {} body={}",
label, endpoint, maskForLog(cozeToken), toJsonForLog(body));
String responseText = postCozeJson(label, endpoint, body, cozeToken);
return parseCozeMap(label, responseText);
}
private Map<String, Object> parseCozeMap(String label, String responseText) {
try {
Map<String, Object> result = objectMapper.readValue(responseText, new TypeReference<>() {});
Object code = result.get("code");
if (code instanceof Number number && number.intValue() != 0) {
throw new BusinessException(firstNonBlank(asText(result.get("msg")), asText(result.get("message")), "Coze 工作流提交失败"));
}
return result;
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
throw new BusinessException("Coze 返回解析失败: " + ex.getMessage());
}
}
private DouyinCopyVo parseDouyinCopyResponse(String responseText) {
try {
JsonNode root = objectMapper.readTree(responseText);
if (root.has("code") && root.path("code").asInt(0) != 0) {
throw new BusinessException(firstNonBlank(root.path("msg").asText(null), root.path("message").asText(null), "Coze 工作流执行失败"));
}
JsonNode data = root.path("data");
JsonNode payload = unwrapPossiblyJsonText(data);
String recognized = firstNonBlank(findText(payload, ORIGINAL_FIELDS), findText(root, ORIGINAL_FIELDS));
String script = firstNonBlank(findText(payload, SCRIPT_FIELDS), findText(root, SCRIPT_FIELDS));
if (!hasText(recognized) && !hasText(script)) {
String fallback = flattenText(payload);
recognized = fallback;
script = fallback;
} else if (!hasText(script)) {
script = recognized;
} else if (!hasText(recognized)) {
recognized = script;
}
if (!hasText(recognized) && !hasText(script)) {
throw new BusinessException("Coze 返回为空,未识别到文案内容");
}
DouyinCopyVo vo = new DouyinCopyVo();
vo.setRecognizedContent(cleanCopyText(recognized));
vo.setScriptDraft(cleanCopyText(script));
vo.setExecuteId(root.path("execute_id").asText(""));
vo.setDebugUrl(root.path("debug_url").asText(""));
return vo;
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
log.warn("[image-video] parse douyin copy response failed body={}", responseText, ex);
throw new BusinessException("Coze 返回解析失败: " + ex.getMessage());
}
}
private String cleanCopyText(String value) {
String text = firstNonBlank(value);
if (!hasText(text)) {
return "";
}
return text
.replaceAll("(?m)^\\s*https?://\\S+\\s*$", "")
.replaceAll("https?://\\S+", "")
.replaceAll("(?m)^[ \\t]+|[ \\t]+$", "")
.replaceAll("\\n{3,}", "\n\n")
.trim();
}
private JsonNode unwrapPossiblyJsonText(JsonNode node) {
JsonNode current = node;
for (int i = 0; i < 3; i++) {
if (current == null || current.isMissingNode() || !current.isTextual()) {
return current;
}
String text = current.asText("").trim();
if (!(text.startsWith("{") || text.startsWith("["))) {
return current;
}
try {
current = objectMapper.readTree(text);
} catch (Exception ignored) {
return current;
}
}
return current;
}
private String findText(JsonNode node, List<String> fieldNames) {
if (node == null || node.isMissingNode() || node.isNull()) {
return "";
}
for (String fieldName : fieldNames) {
JsonNode value = node.path(fieldName);
String text = normalizeNodeText(value);
if (hasText(text)) {
return text;
}
}
if (node.isObject()) {
for (JsonNode child : node) {
String text = findText(unwrapPossiblyJsonText(child), fieldNames);
if (hasText(text)) {
return text;
}
}
}
if (node.isArray()) {
for (JsonNode child : node) {
String text = findText(unwrapPossiblyJsonText(child), fieldNames);
if (hasText(text)) {
return text;
}
}
}
return "";
}
private String normalizeNodeText(JsonNode node) {
if (node == null || node.isMissingNode() || node.isNull()) {
return "";
}
JsonNode unwrapped = unwrapPossiblyJsonText(node);
if (unwrapped != node) {
return flattenText(unwrapped);
}
if (node.isTextual()) {
return node.asText("");
}
if (node.isNumber() || node.isBoolean()) {
return node.asText("");
}
return flattenText(node);
}
private String flattenText(JsonNode node) {
if (node == null || node.isMissingNode() || node.isNull()) {
return "";
}
if (node.isTextual() || node.isNumber() || node.isBoolean()) {
return node.asText("").trim();
}
if (node.isArray()) {
StringBuilder out = new StringBuilder();
for (JsonNode child : node) {
String text = flattenText(unwrapPossiblyJsonText(child));
if (hasText(text)) {
if (!out.isEmpty()) {
out.append('\n');
}
out.append(text.trim());
}
}
return out.toString();
}
if (node.isObject()) {
StringBuilder out = new StringBuilder();
for (JsonNode child : node) {
String text = flattenText(unwrapPossiblyJsonText(child));
if (hasText(text)) {
if (!out.isEmpty()) {
out.append('\n');
}
out.append(text.trim());
}
}
return out.toString();
}
return "";
}
private String postCozeJson(String label, String endpoint, Map<String, Object> body, String cozeToken) {
try {
String requestBody = objectMapper.writeValueAsString(body == null ? Map.of() : body);
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint))
.timeout(Duration.ofMillis(Math.max(1000, properties.getCozeReadTimeoutMillis())))
.header("Authorization", "Bearer " + stripBearer(cozeToken))
.header("Content-Type", "application/json; charset=UTF-8")
.header("Accept-Charset", StandardCharsets.UTF_8.name())
.POST(HttpRequest.BodyPublishers.ofString(requestBody, StandardCharsets.UTF_8))
.build();
HttpResponse<String> response = httpClient().send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
String responseBody = response.body() == null ? "" : response.body();
log.info("[image-video] {} coze response status={} bodyLength={}",
label, response.statusCode(), responseBody.length());
log.info("[image-video] {} coze response detail status={} body={}",
label, response.statusCode(), responseBody);
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new BusinessException("Coze 调用失败: HTTP " + response.statusCode());
}
return responseBody;
} catch (BusinessException ex) {
throw ex;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new BusinessException("Coze 调用被中断", ex);
} catch (Exception ex) {
throw new BusinessException("Coze 调用失败: " + ex.getMessage(), ex);
}
}
private String getCozeJson(String label, String endpoint, String cozeToken) {
try {
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(endpoint))
.timeout(Duration.ofMillis(Math.max(1000, properties.getCozeReadTimeoutMillis())))
.header("Authorization", "Bearer " + stripBearer(cozeToken))
.header("Content-Type", "application/json; charset=UTF-8")
.header("Accept-Charset", StandardCharsets.UTF_8.name())
.GET()
.build();
HttpResponse<String> response = httpClient().send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
String responseBody = response.body() == null ? "" : response.body();
log.info("[image-video] {} coze response status={} bodyLength={}",
label, response.statusCode(), responseBody.length());
log.info("[image-video] {} coze response detail status={} body={}",
label, response.statusCode(), responseBody);
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new BusinessException("Coze 调用失败: HTTP " + response.statusCode());
}
return responseBody;
} catch (BusinessException ex) {
throw ex;
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new BusinessException("Coze 调用被中断", ex);
} catch (Exception ex) {
throw new BusinessException("Coze 调用失败: " + ex.getMessage(), ex);
}
}
private HttpClient httpClient() {
HttpClient client = sharedHttpClient;
if (client == null) {
synchronized (this) {
if (sharedHttpClient == null) {
sharedHttpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofMillis(Math.max(1000, properties.getCozeConnectTimeoutMillis())))
.build();
}
client = sharedHttpClient;
}
}
return client;
}
private String joinUrl(String baseUrl, String path) {
String base = firstNonBlank(baseUrl, "https://api.coze.cn").trim();
String suffix = firstNonBlank(path, "/v1/workflow/run").trim();
if (base.endsWith("/") && suffix.startsWith("/")) {
return base.substring(0, base.length() - 1) + suffix;
}
if (!base.endsWith("/") && !suffix.startsWith("/")) {
return base + "/" + suffix;
}
return base + suffix;
}
private String stripBearer(String token) {
String normalized = token == null ? "" : token.trim();
if (normalized.regionMatches(true, 0, "Bearer ", 0, 7)) {
return normalized.substring(7).trim();
}
return normalized;
}
private String firstNonBlank(String... values) {
for (String value : values) {
if (hasText(value)) {
return value;
}
}
return "";
}
private boolean hasText(String value) {
return value != null && !value.trim().isEmpty();
}
private String asText(Object value) {
return value == null ? "" : String.valueOf(value).trim();
}
private String resolveMediaType(String contentType, String filename) {
String type = contentType == null ? "" : contentType.toLowerCase();
String name = filename == null ? "" : filename.toLowerCase();
if (type.startsWith("image/") || name.matches(".*\\.(png|jpe?g|webp|gif|bmp)$")) {
return "image";
}
if (type.startsWith("video/") || name.matches(".*\\.(mp4|mov|webm|m4v|avi|mkv)$")) {
return "video";
}
if (type.startsWith("audio/") || name.matches(".*\\.(mp3|wav|m4a|aac|flac|ogg)$")) {
return "audio";
}
return "file";
}
private String toJsonForLog(Object value) {
try {
return objectMapper.writeValueAsString(maskSecretsForLog(value));
} catch (Exception ex) {
return String.valueOf(value);
}
}
private Object maskSecretsForLog(Object value) {
if (value instanceof Map<?, ?> source) {
Map<String, Object> masked = new LinkedHashMap<>();
for (Map.Entry<?, ?> entry : source.entrySet()) {
String key = String.valueOf(entry.getKey());
Object rawValue = entry.getValue();
if (isSensitiveLogKey(key)) {
masked.put(key, maskForLog(asText(rawValue)));
} else {
masked.put(key, maskSecretsForLog(rawValue));
}
}
return masked;
}
if (value instanceof List<?> source) {
List<Object> masked = new ArrayList<>(source.size());
for (Object item : source) {
masked.add(maskSecretsForLog(item));
}
return masked;
}
return value;
}
private boolean isSensitiveLogKey(String key) {
String normalized = key == null ? "" : key.toLowerCase();
return normalized.contains("token")
|| normalized.contains("secret")
|| normalized.contains("authorization")
|| normalized.endsWith("_key")
|| normalized.equals("key")
|| normalized.equals("api_key")
|| normalized.equals("t8_key");
}
private String maskForLog(String value) {
if (!hasText(value)) {
return "";
}
String text = value.trim();
if (text.length() <= 10) {
return "***";
}
return text.substring(0, 6) + "***" + text.substring(text.length() - 4);
}
}
@@ -0,0 +1,211 @@
package com.nanri.aiimage.modules.imagevideo.service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.common.security.ShopCredentialCryptoService;
import com.nanri.aiimage.config.ImageVideoProperties;
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoSecretMapper;
import com.nanri.aiimage.modules.imagevideo.model.dto.ImageVideoSecretSaveRequest;
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoSecretEntity;
import com.nanri.aiimage.modules.imagevideo.model.vo.ImageVideoSecretStatusVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime;
import java.util.Set;
@Service
@RequiredArgsConstructor
public class ImageVideoSecretService {
private static final Set<Integer> ALLOWED_EXPIRE_DAYS = Set.of(1, 7, 30);
private final ImageVideoSecretMapper secretMapper;
private final ShopCredentialCryptoService cryptoService;
private final ImageVideoProperties properties;
public ImageVideoSecretStatusVo status(Long userId) {
validateUserId(userId);
return toStatus(userId, selectByUserId(userId));
}
@Transactional
public ImageVideoSecretStatusVo save(ImageVideoSecretSaveRequest request) {
validateUserId(request.getUserId());
Integer expireDays = request.getExpireDays();
if (expireDays == null || !ALLOWED_EXPIRE_DAYS.contains(expireDays)) {
throw new BusinessException("有效期只支持 1 天、7 天、30 天");
}
LocalDateTime now = LocalDateTime.now();
ImageVideoSecretEntity existing = selectByUserId(request.getUserId());
boolean mustInputAll = existing == null || existing.getExpiresAt() == null || !existing.getExpiresAt().isAfter(now);
String copyApiKey = normalize(request.getCopyApiKey());
String t8Key = normalize(request.getT8Key());
String voiceApiKey = normalize(request.getVoiceApiKey());
String voiceGroupId = normalize(request.getVoiceGroupId());
if (mustInputAll) {
requireText(copyApiKey, "文案 key 不能为空");
requireText(t8Key, "t8_key 不能为空");
requireText(voiceApiKey, "音色 key 不能为空");
requireText(voiceGroupId, "音色 groupId 不能为空");
}
ImageVideoSecretEntity row = existing == null ? new ImageVideoSecretEntity() : existing;
row.setUserId(request.getUserId());
if (hasText(copyApiKey)) {
row.setCopyApiKey(cryptoService.encrypt(copyApiKey));
}
if (hasText(t8Key)) {
row.setT8Key(cryptoService.encrypt(t8Key));
}
if (hasText(voiceApiKey)) {
row.setVoiceApiKey(cryptoService.encrypt(voiceApiKey));
}
if (hasText(voiceGroupId)) {
row.setVoiceGroupId(cryptoService.encrypt(voiceGroupId));
}
row.setExpiresAt(now.plusDays(expireDays));
row.setUpdatedAt(now);
if (row.getId() == null) {
row.setCreatedAt(now);
secretMapper.insert(row);
} else {
secretMapper.updateById(row);
}
return toStatus(request.getUserId(), row);
}
public ResolvedSecret requireValid(Long userId) {
validateUserId(userId);
ImageVideoSecretEntity row = selectByUserId(userId);
LocalDateTime now = LocalDateTime.now();
if (row == null) {
throw new BusinessException("请先配置视频密钥");
}
if (row.getExpiresAt() == null || !row.getExpiresAt().isAfter(now)) {
throw new BusinessException("视频密钥已过期,请重新配置");
}
String cozeToken = normalize(properties.getCozeToken());
String copyApiKey = decrypt(row.getCopyApiKey());
String t8Key = decrypt(row.getT8Key());
String voiceApiKey = decrypt(row.getVoiceApiKey());
String voiceGroupId = decrypt(row.getVoiceGroupId());
if (!hasText(cozeToken)) {
throw new BusinessException("图生视频 Coze 访问令牌未配置");
}
if (!hasText(copyApiKey) || !hasText(t8Key) || !hasText(voiceApiKey) || !hasText(voiceGroupId)) {
throw new BusinessException("视频密钥未配置完整,请重新配置");
}
return new ResolvedSecret(cozeToken, copyApiKey, t8Key, voiceApiKey, voiceGroupId);
}
private ImageVideoSecretEntity selectByUserId(Long userId) {
return secretMapper.selectOne(new LambdaQueryWrapper<ImageVideoSecretEntity>()
.eq(ImageVideoSecretEntity::getUserId, userId)
.last("limit 1"));
}
private ImageVideoSecretStatusVo toStatus(Long userId, ImageVideoSecretEntity row) {
ImageVideoSecretStatusVo vo = new ImageVideoSecretStatusVo();
vo.setUserId(userId);
if (row == null) {
vo.setConfigured(false);
vo.setValid(false);
vo.setExpired(false);
vo.setHasCopyApiKey(false);
vo.setHasT8Key(false);
vo.setHasVoiceApiKey(false);
vo.setHasVoiceGroupId(false);
return vo;
}
String copyApiKey = decrypt(row.getCopyApiKey());
String t8Key = decrypt(row.getT8Key());
String voiceApiKey = decrypt(row.getVoiceApiKey());
String voiceGroupId = decrypt(row.getVoiceGroupId());
boolean hasCopy = hasText(copyApiKey);
boolean hasT8 = hasText(t8Key);
boolean hasVoice = hasText(voiceApiKey);
boolean hasGroup = hasText(voiceGroupId);
boolean expired = row.getExpiresAt() != null && !row.getExpiresAt().isAfter(LocalDateTime.now());
boolean configured = hasCopy && hasT8 && hasVoice && hasGroup;
vo.setConfigured(configured);
vo.setValid(configured && !expired && row.getExpiresAt() != null);
vo.setExpired(expired);
vo.setHasCopyApiKey(hasCopy);
vo.setHasT8Key(hasT8);
vo.setHasVoiceApiKey(hasVoice);
vo.setHasVoiceGroupId(hasGroup);
vo.setCopyApiKeyMasked(mask(copyApiKey));
vo.setT8KeyMasked(mask(t8Key));
vo.setVoiceApiKeyMasked(mask(voiceApiKey));
vo.setVoiceGroupIdMasked(mask(voiceGroupId));
vo.setExpireDays(resolveExpireDays(row));
vo.setExpiresAt(row.getExpiresAt());
return vo;
}
private Integer resolveExpireDays(ImageVideoSecretEntity row) {
if (row.getExpiresAt() == null) {
return 7;
}
LocalDateTime baseTime = row.getUpdatedAt() != null ? row.getUpdatedAt() : row.getCreatedAt();
if (baseTime == null) {
return 7;
}
long days = java.time.Duration.between(baseTime, row.getExpiresAt()).toDays();
if (days >= 25) {
return 30;
}
if (days >= 5) {
return 7;
}
return 1;
}
private String decrypt(String cipherText) {
if (!hasText(cipherText)) {
return "";
}
return cryptoService.decrypt(cipherText);
}
private String mask(String value) {
if (!hasText(value)) {
return "";
}
String text = value.trim();
if (text.length() <= 8) {
return "****";
}
return text.substring(0, 4) + "****" + text.substring(text.length() - 4);
}
private void validateUserId(Long userId) {
if (userId == null || userId <= 0) {
throw new BusinessException("user_id 不合法");
}
}
private void requireText(String value, String message) {
if (!hasText(value)) {
throw new BusinessException(message);
}
}
private String normalize(String value) {
return value == null ? "" : value.trim();
}
private boolean hasText(String value) {
return value != null && !value.trim().isEmpty();
}
public record ResolvedSecret(String cozeToken, String copyApiKey, String t8Key, String voiceApiKey, String voiceGroupId) {
}
}
@@ -0,0 +1,60 @@
package com.nanri.aiimage.modules.imagevideo.service;
import com.nanri.aiimage.config.ImageVideoProperties;
import com.nanri.aiimage.modules.imagevideo.mapper.ImageVideoWorkflowConfigMapper;
import com.nanri.aiimage.modules.imagevideo.model.entity.ImageVideoWorkflowConfigEntity;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@Service
@RequiredArgsConstructor
public class ImageVideoWorkflowConfigService {
public static final String DOUYIN_COPY = "douyin_copy";
public static final String IMAGE_VIDEO = "image_video";
public static final String VOICE_DELETE = "voice_delete";
public static final String VOICE_LIST = "voice_list";
public static final String VOICE_CLONE = "voice_clone";
public static final String VOICE_SYNTHESIS = "voice_synthesis";
private final ImageVideoWorkflowConfigMapper configMapper;
private final ImageVideoProperties properties;
public String douyinCopyWorkflowId() {
return resolve(DOUYIN_COPY, properties.getDouyinCopyWorkflowId());
}
public String imageVideoWorkflowId() {
return resolve(IMAGE_VIDEO, properties.getImageVideoWorkflowId());
}
public String voiceDeleteWorkflowId() {
return resolve(VOICE_DELETE, properties.getVoiceDeleteWorkflowId());
}
public String voiceListWorkflowId() {
return resolve(VOICE_LIST, properties.getVoiceListWorkflowId());
}
public String voiceCloneWorkflowId() {
return resolve(VOICE_CLONE, properties.getVoiceCloneWorkflowId());
}
public String voiceSynthesisWorkflowId() {
return resolve(VOICE_SYNTHESIS, properties.getVoiceSynthesisWorkflowId());
}
private String resolve(String configKey, String fallback) {
ImageVideoWorkflowConfigEntity row = configMapper.selectById(configKey);
String configured = row == null ? "" : normalize(row.getWorkflowId());
return hasText(configured) ? configured : normalize(fallback);
}
private String normalize(String value) {
return value == null ? "" : value.trim();
}
private boolean hasText(String value) {
return value != null && !value.trim().isEmpty();
}
}