where('id', $logId)->update([ 'status' => 1, 'log' => '文生文处理中', 'update_time' => date('Y-m-d H:i:s') ]); } $fullPath = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/'); $list = Db::name("text_to_image") ->where('old_image_url', $fullPath) ->where('img_name', '<>', '') // ->where('status', 0) ->select(); if (!empty($list)) { foreach ($list as $index => $row) { $currentTime = date('Y-m-d H:i:s'); echo "处理时间:{$currentTime}\n"; echo "👉 正在处理第 " . ($index + 1) . " 条,ID: {$row['id']}\n"; $result = $this->textToTxt($row['id'],$data["txttotxt_selectedOption"],$fullPath); echo $result; echo "✅ 处理结果:完成\n"; echo "完成时间:" . date('Y-m-d H:i:s') . "\n"; echo "Processed: " . static::class . "\n\n"; } // 更新日志为成功 if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 2, 'log' => '文生文处理成功', 'update_time' => date('Y-m-d H:i:s') ]); } echo "处理完成\n"; } else { echo "⚠ 未找到可处理的数据,跳过执行\n"; if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => 2, 'log' => '无数据可处理,已跳过', 'update_time' => date('Y-m-d H:i:s') ]); } } // 链式执行:检查是否还有下一个任务 if (!empty($data['chain_next'])) { $nextType = array_shift($data['chain_next']); // 获取下一个任务类型 $data['type'] = $nextType; Queue::push('app\job\ImageArrJob', [ 'task_id' => $data['task_id'], 'data' => [$data] // 继续传一个任务 ], 'arrimage'); } } catch (\Exception $e) { echo "❌ 错误信息: " . $e->getMessage() . "\n"; echo "📄 文件: " . $e->getFile() . ",第 " . $e->getLine() . " 行\n"; if ($logId) { Db::name('image_task_log')->where('id', $logId)->update([ 'status' => -1, 'log' => '文生文失败:' . $e->getMessage(), 'update_time' => date('Y-m-d H:i:s') ]); } } $job->delete(); } /** * 文生文核心处理逻辑(调用 GPT 接口) * @param int $id text_to_image 表主键 * @return string */ public function textToTxt($id,$txttotxt_selectedOption,$fullPath) { $template = Db::name('template') ->field('id,english_content,content') ->where('path',$fullPath) ->where('ids',1) ->find(); $record = Db::name('text_to_image') ->field('id,english_description,chinese_description') ->where('id',$id) ->order('id desc') ->find(); if (!$record) {return '没有找到匹配的图像记录';} // 拼接提示词调用 文生文 接口 $ai = new AIGatewayService(); $gptRes = $ai->txtGptApi($template['english_content'].$record['chinese_description'],$txttotxt_selectedOption); $gptText = trim($gptRes['choices'][0]['message']['content'] ?? ''); // 更新数据库记录 Db::name('text_to_image')->where('id', $record['id'])->update([ 'english_description' => $gptText, 'status_name' => "文生文" ]); return 0; } }