|
|
@@ -0,0 +1,186 @@
|
|
|
+package com.tzld.videoVector.api;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import lombok.Data;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import okhttp3.*;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.annotation.PostConstruct;
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.List;
|
|
|
+import java.util.concurrent.TimeUnit;
|
|
|
+
|
|
|
+/**
|
|
|
+ * AIGC API 服务
|
|
|
+ * 封装 AIGC 平台任务输入列表和回调详情两个接口
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+public class AigcApiService {
|
|
|
+
|
|
|
+ private OkHttpClient client;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务输入列表接口
|
|
|
+ */
|
|
|
+ private static final String INPUT_LIST_URL = "http://aigc-api.cybertogether.net/aigc/api/task/input-usage/list";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 任务回调详情接口
|
|
|
+ */
|
|
|
+ private static final String CALLBACK_DETAIL_URL = "http://aigc-api.cybertogether.net/aigc/api/task/callback-data";
|
|
|
+
|
|
|
+ private static final MediaType JSON_MEDIA_TYPE = MediaType.get("application/json; charset=utf-8");
|
|
|
+
|
|
|
+ @Value("${aigc.api.timeout:30}")
|
|
|
+ private int timeout;
|
|
|
+
|
|
|
+ @PostConstruct
|
|
|
+ public void init() {
|
|
|
+ client = new OkHttpClient.Builder()
|
|
|
+ .connectTimeout(timeout, TimeUnit.SECONDS)
|
|
|
+ .readTimeout(timeout, TimeUnit.SECONDS)
|
|
|
+ .writeTimeout(timeout, TimeUnit.SECONDS)
|
|
|
+ .build();
|
|
|
+ log.info("AIGC API 服务初始化完成");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取任务输入列表
|
|
|
+ * 请求 POST /aigc/api/task/input-usage/list,返回 bizUniqueId(视频ID)与 taskInstanceId 的映射关系
|
|
|
+ *
|
|
|
+ * @param id 任务 ID
|
|
|
+ * @return 任务输入列表,失败时返回空列表
|
|
|
+ */
|
|
|
+ public List<AigcTaskInput> getTaskInputList(int id) {
|
|
|
+ try {
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("id", id);
|
|
|
+ JSONObject reqBody = new JSONObject();
|
|
|
+ reqBody.put("params", params);
|
|
|
+
|
|
|
+ RequestBody body = RequestBody.create(JSON_MEDIA_TYPE, reqBody.toJSONString());
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(INPUT_LIST_URL)
|
|
|
+ .post(body)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ log.error("获取任务输入列表请求失败,HTTP状态码: {}, id: {}", response.code(), id);
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ String respStr = response.body().string();
|
|
|
+ JSONObject res = JSON.parseObject(respStr);
|
|
|
+ if (res == null || res.getIntValue("code") != 0) {
|
|
|
+ log.error("获取任务输入列表响应异常,id: {}, resp: {}", id, respStr);
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ JSONArray data = res.getJSONArray("data");
|
|
|
+ if (data == null || data.isEmpty()) {
|
|
|
+ log.info("任务输入列表为空,id: {}", id);
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ List<AigcTaskInput> list = new ArrayList<>();
|
|
|
+ for (int i = 0; i < data.size(); i++) {
|
|
|
+ JSONObject item = data.getJSONObject(i);
|
|
|
+ if (item == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ Long taskInstanceId = item.getLong("taskInstanceId");
|
|
|
+ String bizUniqueId = item.getString("bizUniqueId");
|
|
|
+ if (taskInstanceId == null || bizUniqueId == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ AigcTaskInput input = new AigcTaskInput();
|
|
|
+ input.setTaskInstanceId(taskInstanceId);
|
|
|
+ input.setBizUniqueId(bizUniqueId);
|
|
|
+ list.add(input);
|
|
|
+ }
|
|
|
+ log.info("获取到 {} 条任务输入数据,id: {}", list.size(), id);
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取任务输入列表异常,id: {}, error: {}", id, e.getMessage(), e);
|
|
|
+ return Collections.emptyList();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取任务回调详情,解析并返回 dataContent 字段的 JSONObject
|
|
|
+ * 请求 POST /aigc/api/task/callback-data/detail
|
|
|
+ *
|
|
|
+ * @param taskInstanceId 任务实例 ID
|
|
|
+ * @return dataContent 解析后的 JSONObject,失败时返回 null
|
|
|
+ */
|
|
|
+ public JSONObject getTaskCallbackDetail(long taskInstanceId) {
|
|
|
+ try {
|
|
|
+ JSONObject params = new JSONObject();
|
|
|
+ params.put("id", taskInstanceId);
|
|
|
+ JSONObject reqBody = new JSONObject();
|
|
|
+ reqBody.put("params", params);
|
|
|
+
|
|
|
+ RequestBody body = RequestBody.create(JSON_MEDIA_TYPE, reqBody.toJSONString());
|
|
|
+ Request request = new Request.Builder()
|
|
|
+ .url(CALLBACK_DETAIL_URL)
|
|
|
+ .post(body)
|
|
|
+ .addHeader("Content-Type", "application/json")
|
|
|
+ .build();
|
|
|
+
|
|
|
+ try (Response response = client.newCall(request).execute()) {
|
|
|
+ if (!response.isSuccessful()) {
|
|
|
+ log.error("获取任务回调详情请求失败,HTTP状态码: {}, taskInstanceId: {}", response.code(), taskInstanceId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String respStr = response.body().string();
|
|
|
+ JSONObject res = JSON.parseObject(respStr);
|
|
|
+ if (res == null || res.getIntValue("code") != 0) {
|
|
|
+ log.error("获取任务回调详情响应异常,taskInstanceId: {}, resp: {}", taskInstanceId, respStr);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ JSONArray dataArray = res.getJSONArray("data");
|
|
|
+ if (dataArray == null || dataArray.isEmpty()) {
|
|
|
+ log.warn("任务回调详情 data 为空,taskInstanceId: {}", taskInstanceId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ // 取第一条记录
|
|
|
+ JSONObject data = dataArray.getJSONObject(0);
|
|
|
+ if (data == null) {
|
|
|
+ log.warn("任务回调详情 data[0] 为空,taskInstanceId: {}", taskInstanceId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ String dataContent = data.getString("dataContent");
|
|
|
+ if (dataContent == null || dataContent.trim().isEmpty()) {
|
|
|
+ log.warn("taskInstanceId: {} 的 dataContent 为空", taskInstanceId);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ return JSON.parseObject(dataContent);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("获取任务回调详情异常,taskInstanceId: {}, error: {}", taskInstanceId, e.getMessage(), e);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * AIGC 任务输入数据
|
|
|
+ */
|
|
|
+ @Data
|
|
|
+ public static class AigcTaskInput {
|
|
|
+ /**
|
|
|
+ * 任务实例 ID,用于调用 detail 接口
|
|
|
+ */
|
|
|
+ private Long taskInstanceId;
|
|
|
+ /**
|
|
|
+ * 业务唯一 ID,即视频 ID
|
|
|
+ */
|
|
|
+ private String bizUniqueId;
|
|
|
+ }
|
|
|
+}
|