|
|
@@ -2577,168 +2577,68 @@ class AnimeService
|
|
|
];
|
|
|
}
|
|
|
|
|
|
- public function setRoleOrScene($data) {
|
|
|
- $anime_id = getProp($data, 'anime_id');
|
|
|
- $episode_id = getProp($data, 'episode_id');
|
|
|
- $json_data = getProp($data, 'json_data');
|
|
|
+ public function generateImg($data) {
|
|
|
+ $prompt = getProp($data, 'prompt');
|
|
|
|
|
|
// 参数验证
|
|
|
- if (!$anime_id) Utils::throwError('20003:请提供动漫ID');
|
|
|
- if (!$episode_id) Utils::throwError('20003:请提供剧集ID');
|
|
|
- if (!$json_data) Utils::throwError('20003:请提供json_data数据');
|
|
|
-
|
|
|
- // 验证剧集是否存在
|
|
|
- $episode = DB::table('mp_anime_episodes')
|
|
|
- ->where('anime_id', $anime_id)
|
|
|
- ->where('id', $episode_id)
|
|
|
- ->first();
|
|
|
-
|
|
|
- if (!$episode) Utils::throwError('20003:该剧集不存在');
|
|
|
-
|
|
|
- // 判断是 role 还是 scene
|
|
|
- $isRole = isset($json_data['role']);
|
|
|
- $isScene = isset($json_data['scene']);
|
|
|
-
|
|
|
- if (!$isRole && !$isScene) {
|
|
|
- Utils::throwError('20003:json_data必须包含role或scene字段');
|
|
|
- }
|
|
|
-
|
|
|
- if ($isRole && $isScene) {
|
|
|
- Utils::throwError('20003:json_data只能包含role或scene其中一个');
|
|
|
- }
|
|
|
+ if (!$prompt) Utils::throwError('20003:请提供提示词');
|
|
|
|
|
|
try {
|
|
|
- // 获取当前的 roles 或 scenes
|
|
|
- $fieldName = $isRole ? 'roles' : 'scenes';
|
|
|
- $currentData = json_decode($episode->$fieldName, true) ?: [];
|
|
|
+ // 创建图片生成任务
|
|
|
+ $params = [
|
|
|
+ 'prompt' => $prompt,
|
|
|
+ 'ref_img_urls' => [],
|
|
|
+ 'width' => 2048,
|
|
|
+ 'height' => 2048
|
|
|
+ ];
|
|
|
|
|
|
- // 查找匹配的项
|
|
|
- $targetName = $isRole ? $json_data['role'] : $json_data['scene'];
|
|
|
- $foundIndex = -1;
|
|
|
+ $task = $this->aiImageGenerationService->createImageGenerationTask($params);
|
|
|
+ $task_id = $task->id;
|
|
|
|
|
|
- foreach ($currentData as $index => $item) {
|
|
|
- $itemName = $isRole ? ($item['role'] ?? '') : ($item['scene'] ?? '');
|
|
|
- if ($itemName === $targetName) {
|
|
|
- $foundIndex = $index;
|
|
|
- break;
|
|
|
- }
|
|
|
+ if (!$task_id) {
|
|
|
+ Utils::throwError('20003:创建图片生成任务失败');
|
|
|
}
|
|
|
|
|
|
- if ($foundIndex === -1) {
|
|
|
- Utils::throwError('20003:未找到匹配的' . ($isRole ? '角色' : '场景'));
|
|
|
- }
|
|
|
-
|
|
|
- // 检查是否有 url
|
|
|
- $url = getProp($json_data, 'url', '');
|
|
|
+ // 轮询获取结果(3分钟超时,每3秒查询一次)
|
|
|
+ $start_time = time();
|
|
|
+ $timeout = 180; // 3分钟
|
|
|
+ $img_url = '';
|
|
|
|
|
|
- if (empty($url)) {
|
|
|
- // 没有 url,需要生成图片
|
|
|
- $description = getProp($json_data, 'description', '');
|
|
|
- if (empty($description)) {
|
|
|
- Utils::throwError('20003:缺少description字段,无法生成图片');
|
|
|
- }
|
|
|
+ while (time() - $start_time < $timeout) {
|
|
|
+ sleep(3);
|
|
|
|
|
|
- // 获取艺术风格
|
|
|
- $art_style = $episode->art_style ?? '';
|
|
|
+ $task = MpGeneratePicTask::where('id', $task_id)->first();
|
|
|
+ $statusInfo = $this->aiImageGenerationService->queryTaskStatus($task);
|
|
|
|
|
|
- // 构建提示词
|
|
|
- $prompt = $description;
|
|
|
- if ($art_style) {
|
|
|
- $prompt = $art_style . ',' . $prompt;
|
|
|
- }
|
|
|
-
|
|
|
- // 创建图片生成任务
|
|
|
- $params = [
|
|
|
- 'prompt' => $prompt,
|
|
|
- 'ref_img_urls' => [],
|
|
|
- 'width' => 2048,
|
|
|
- 'height' => 2048
|
|
|
- ];
|
|
|
-
|
|
|
- $task = $this->aiImageGenerationService->createImageGenerationTask($params);
|
|
|
- $task_id = $task->id;
|
|
|
-
|
|
|
- if (!$task_id) {
|
|
|
- Utils::throwError('20003:创建图片生成任务失败');
|
|
|
- }
|
|
|
-
|
|
|
- // 轮询获取结果(3分钟超时,每3秒查询一次)
|
|
|
- $start_time = time();
|
|
|
- $timeout = 180; // 3分钟
|
|
|
- $img_url = '';
|
|
|
-
|
|
|
- while (time() - $start_time < $timeout) {
|
|
|
- sleep(3);
|
|
|
+ if ($statusInfo['status'] === 'success') {
|
|
|
+ $task->updateStatus('success', [
|
|
|
+ 'result_url' => $statusInfo['result_url'],
|
|
|
+ 'result_json' => $statusInfo['result_json'] ?? []
|
|
|
+ ]);
|
|
|
|
|
|
- $task = MpGeneratePicTask::where('id', $task_id)->first();
|
|
|
- $statusInfo = $this->aiImageGenerationService->queryTaskStatus($task);
|
|
|
+ $img_url = $statusInfo['result_url'][0];
|
|
|
+ break;
|
|
|
+ } elseif ($statusInfo['status'] === 'failed') {
|
|
|
+ $task->updateStatus('failed', [
|
|
|
+ 'error_message' => $statusInfo['error_message'],
|
|
|
+ 'result_json' => $statusInfo['result_json'] ?? []
|
|
|
+ ]);
|
|
|
|
|
|
- if ($statusInfo['status'] === 'success') {
|
|
|
- $task->updateStatus('success', [
|
|
|
- 'result_url' => $statusInfo['result_url'],
|
|
|
- 'result_json' => $statusInfo['result_json'] ?? []
|
|
|
- ]);
|
|
|
-
|
|
|
- $img_url = $statusInfo['result_url'][0];
|
|
|
- break;
|
|
|
- } elseif ($statusInfo['status'] === 'failed') {
|
|
|
- $task->updateStatus('failed', [
|
|
|
- 'error_message' => $statusInfo['error_message'],
|
|
|
- 'result_json' => $statusInfo['result_json'] ?? []
|
|
|
- ]);
|
|
|
-
|
|
|
- Utils::throwError('20003:图片生成失败: ' . ($statusInfo['error_message'] ?? '未知错误'));
|
|
|
- }
|
|
|
+ Utils::throwError('20003:图片生成失败: ' . ($statusInfo['error_message'] ?? '未知错误'));
|
|
|
}
|
|
|
-
|
|
|
- if (empty($img_url)) {
|
|
|
- Utils::throwError('20003:图片生成超时,请稍后重试');
|
|
|
- }
|
|
|
-
|
|
|
- $url = $img_url;
|
|
|
}
|
|
|
|
|
|
- // 更新数据
|
|
|
- $json_data['url'] = $url;
|
|
|
- $currentData[$foundIndex] = $json_data;
|
|
|
-
|
|
|
- // 保存到数据库
|
|
|
- $result = DB::table('mp_anime_episodes')
|
|
|
- ->where('anime_id', $anime_id)
|
|
|
- ->where('id', $episode_id)
|
|
|
- ->update([
|
|
|
- $fieldName => json_encode($currentData, 256),
|
|
|
- 'updated_at' => date('Y-m-d H:i:s')
|
|
|
- ]);
|
|
|
-
|
|
|
- if ($result === false) {
|
|
|
- Utils::throwError('20003:更新' . ($isRole ? '角色' : '场景') . '失败');
|
|
|
+ if (empty($img_url)) {
|
|
|
+ Utils::throwError('20003:图片生成超时,请稍后重试');
|
|
|
}
|
|
|
|
|
|
- dLog('anime')->info('更新' . ($isRole ? '角色' : '场景') . '成功', [
|
|
|
- 'anime_id' => $anime_id,
|
|
|
- 'episode_id' => $episode_id,
|
|
|
- 'type' => $isRole ? 'role' : 'scene',
|
|
|
- 'name' => $targetName,
|
|
|
- 'url' => $url
|
|
|
- ]);
|
|
|
-
|
|
|
- return [
|
|
|
- 'json_data' => $currentData[$foundIndex]
|
|
|
- ];
|
|
|
+ return $img_url;
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
dLog('anime')->error('更新角色或场景失败', [
|
|
|
- 'anime_id' => $anime_id,
|
|
|
- 'episode_id' => $episode_id,
|
|
|
'error' => $e->getMessage()
|
|
|
]);
|
|
|
|
|
|
- // 如果是已知错误,直接抛出
|
|
|
- if (strpos($e->getMessage(), '20003:') === 0) {
|
|
|
- throw $e;
|
|
|
- }
|
|
|
-
|
|
|
Utils::throwError('20003:更新失败: ' . $e->getMessage());
|
|
|
}
|
|
|
}
|