| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- <?php
- namespace app\job;
- use app\service\AIGatewayService;
- use think\Db;
- use think\queue\Job;
- use think\Queue;
- /**
- * 文生文任务队列处理类
- * 基于图生文生成的描述,继续生成扩展文本内容(链式任务中间环)
- */
- class TextToTextJob
- {
- /**
- * 队列任务入口
- * @param Job $job 当前队列任务
- * @param array $data 包含任务上下文、路径信息、链式类型等
- */
- public function fire(Job $job, $data)
- {
- $logId = $data['log_id'] ?? null;
- echo "━━━━━━━━━━ ▶ 文生文任务开始处理━━━━━━━━━━\n";
- try {
- $startTime = date('Y-m-d H:i:s');
- if ($logId) {
- Db::name('image_task_log')->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;
- }
- }
|