lh 1 ヶ月 前
コミット
9c7dca17f1
1 ファイル変更67 行追加8 行削除
  1. 67 8
      app/Services/AIGeneration/AIVideoGenerationService.php

+ 67 - 8
app/Services/AIGeneration/AIVideoGenerationService.php

@@ -1044,13 +1044,32 @@ class AIVideoGenerationService
 
             $groupId = $this->ensureSeedance20AssetGroup();
 
-            $responseData = $this->requestSeedance20Gateway('CreateAsset', [
+            $createAssetBody = [
                 'ProjectName' => 'default',
                 'GroupId' => $groupId,
                 'AssetType' => $assetType,
                 'Name' => $name,
                 'URL' => $url,
-            ]);
+            ];
+
+            try {
+                $responseData = $this->requestSeedance20Gateway('CreateAsset', $createAssetBody);
+            } catch (\GuzzleHttp\Exception\RequestException $e) {
+                $errorBody = $e->hasResponse() ? (string)$e->getResponse()->getBody() : '';
+                if (strpos($errorBody, 'asset_space_mismatch') === false) {
+                    throw $e;
+                }
+
+                // 素材组空间失效(网关资产空间迁移),创建新素材组并重试一次
+                dLog('generate')->warning('Seedance 2.0 素材组空间失效,创建新素材组重试: ' . $groupId);
+                logDB('generate', 'warning', 'Seedance2.0素材组空间失效', [
+                    'group_id' => $groupId,
+                    'error' => $errorBody
+                ]);
+                $groupId = $this->createSeedance20AssetGroup();
+                $createAssetBody['GroupId'] = $groupId;
+                $responseData = $this->requestSeedance20Gateway('CreateAsset', $createAssetBody);
+            }
 
             $result = $responseData['Result'] ?? [];
             $assetId = $result['Id'] ?? $result['AssetId'] ?? '';
@@ -1196,18 +1215,38 @@ class AIVideoGenerationService
     }
 
     /**
-     * 获取可用的Seedance 2.0素材组ID(不存在则创建默认素材组)
+     * 获取可用的Seedance 2.0素材组ID
+     * 优先复用本地已有素材组,避免依赖网关ListAssetGroups接口(该接口近期返回400)
      *
      * @return string
      */
     private function ensureSeedance20AssetGroup(): string
     {
-        $groups = $this->listSeedance20AssetGroups();
-        if (!empty($groups)) {
-            $groupId = $groups[0]['Id'] ?? $groups[0]['GroupId'] ?? '';
-            if (!empty($groupId)) {
-                return $groupId;
+        // 优先复用本地已有的百度素材组
+        $localGroup = DB::table('mp_assets')
+            ->where('source_type', 'baidu')
+            ->whereNotNull('group_id')
+            ->where('group_id', '!=', '')
+            ->orderBy('id', 'desc')
+            ->select('group_id')
+            ->first();
+        if ($localGroup && !empty($localGroup->group_id)) {
+            dLog('generate')->info('Seedance 2.0 复用本地素材组: ' . $localGroup->group_id);
+            return $localGroup->group_id;
+        }
+
+        // 本地无记录时再查询网关素材组
+        try {
+            $groups = $this->listSeedance20AssetGroups();
+            if (!empty($groups)) {
+                $groupId = $groups[0]['Id'] ?? $groups[0]['GroupId'] ?? '';
+                if (!empty($groupId)) {
+                    return $groupId;
+                }
             }
+        } catch (\Exception $e) {
+            dLog('generate')->warning('Seedance 2.0 ListAssetGroups查询失败,尝试直接创建素材组: ' . $e->getMessage());
+            logDB('generate', 'warning', 'Seedance2.0 ListAssetGroups失败', ['error' => $e->getMessage()]);
         }
 
         return $this->createSeedance20AssetGroup();
@@ -1249,6 +1288,26 @@ class AIVideoGenerationService
             throw new \Exception('创建Seedance 2.0素材组失败: ' . json_encode($responseData, JSON_UNESCAPED_UNICODE));
         }
 
+        // 持久化素材组记录(占位),避免后续每次都调用网关查询素材组
+        $groupRecordExists = DB::table('mp_assets')
+            ->where('group_id', $groupId)
+            ->where('source_type', 'baidu')
+            ->where('asset_id', 'like', 'group-%')
+            ->exists();
+        if (!$groupRecordExists) {
+            MpAsset::create([
+                'asset_id' => 'group-' . $groupId,
+                'group_id' => $groupId,
+                'group_name' => 'default',
+                'name' => $result['Name'] ?? 'default',
+                'url' => 'baidu_group://' . $groupId,
+                'asset_type' => MpAsset::TYPE_IMAGE,
+                'source_type' => 'baidu',
+                'status' => MpAsset::STATUS_ACTIVE,
+                'extra_params' => $responseData,
+            ]);
+        }
+
         logDB('generate', 'info', 'Seedance2.0素材组创建成功', ['group_id' => $groupId]);
         return $groupId;
     }