TextToTextJob.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. namespace app\job;
  3. use app\service\AIGatewayService;
  4. use think\Db;
  5. use think\queue\Job;
  6. use think\Queue;
  7. /**
  8. * 文生文任务队列处理类
  9. * 基于图生文生成的描述,继续生成扩展文本内容(链式任务中间环)
  10. */
  11. class TextToTextJob
  12. {
  13. /**
  14. * 队列任务入口
  15. * @param Job $job 当前队列任务
  16. * @param array $data 包含任务上下文、路径信息、链式类型等
  17. */
  18. public function fire(Job $job, $data)
  19. {
  20. $logId = $data['log_id'] ?? null;
  21. echo "━━━━━━━━━━ ▶ 文生文任务开始处理━━━━━━━━━━\n";
  22. try {
  23. $startTime = date('Y-m-d H:i:s');
  24. if ($logId) {
  25. Db::name('image_task_log')->where('id', $logId)->update([
  26. 'status' => 1,
  27. 'log' => '文生文处理中',
  28. 'update_time' => date('Y-m-d H:i:s')
  29. ]);
  30. }
  31. $fullPath = rtrim($data['sourceDir'], '/') . '/' . ltrim($data['file_name'], '/');
  32. $list = Db::name("text_to_image")
  33. ->where('old_image_url', $fullPath)
  34. ->where('img_name', '<>', '')
  35. // ->where('status', 0)
  36. ->select();
  37. if (!empty($list)) {
  38. foreach ($list as $index => $row) {
  39. $currentTime = date('Y-m-d H:i:s');
  40. echo "处理时间:{$currentTime}\n";
  41. echo "👉 正在处理第 " . ($index + 1) . " 条,ID: {$row['id']}\n";
  42. $result = $this->textToTxt($row['id'],$data["txttotxt_selectedOption"],$fullPath);
  43. echo $result;
  44. echo "✅ 处理结果:完成\n";
  45. echo "完成时间:" . date('Y-m-d H:i:s') . "\n";
  46. echo "Processed: " . static::class . "\n\n";
  47. }
  48. // 更新日志为成功
  49. if ($logId) {
  50. Db::name('image_task_log')->where('id', $logId)->update([
  51. 'status' => 2,
  52. 'log' => '文生文处理成功',
  53. 'update_time' => date('Y-m-d H:i:s')
  54. ]);
  55. }
  56. echo "处理完成\n";
  57. } else {
  58. echo "⚠ 未找到可处理的数据,跳过执行\n";
  59. if ($logId) {
  60. Db::name('image_task_log')->where('id', $logId)->update([
  61. 'status' => 2,
  62. 'log' => '无数据可处理,已跳过',
  63. 'update_time' => date('Y-m-d H:i:s')
  64. ]);
  65. }
  66. }
  67. // 链式执行:检查是否还有下一个任务
  68. if (!empty($data['chain_next'])) {
  69. $nextType = array_shift($data['chain_next']); // 获取下一个任务类型
  70. $data['type'] = $nextType;
  71. Queue::push('app\job\ImageArrJob', [
  72. 'task_id' => $data['task_id'],
  73. 'data' => [$data] // 继续传一个任务
  74. ], 'arrimage');
  75. }
  76. } catch (\Exception $e) {
  77. echo "❌ 错误信息: " . $e->getMessage() . "\n";
  78. echo "📄 文件: " . $e->getFile() . ",第 " . $e->getLine() . " 行\n";
  79. if ($logId) {
  80. Db::name('image_task_log')->where('id', $logId)->update([
  81. 'status' => -1,
  82. 'log' => '文生文失败:' . $e->getMessage(),
  83. 'update_time' => date('Y-m-d H:i:s')
  84. ]);
  85. }
  86. }
  87. $job->delete();
  88. }
  89. /**
  90. * 文生文核心处理逻辑(调用 GPT 接口)
  91. * @param int $id text_to_image 表主键
  92. * @return string
  93. */
  94. public function textToTxt($id,$txttotxt_selectedOption,$fullPath)
  95. {
  96. $template = Db::name('template')
  97. ->field('id,english_content,content')
  98. ->where('path',$fullPath)
  99. ->where('ids',1)
  100. ->find();
  101. $record = Db::name('text_to_image')
  102. ->field('id,english_description,chinese_description')
  103. ->where('id',$id)
  104. ->order('id desc')
  105. ->find();
  106. if (!$record) {return '没有找到匹配的图像记录';}
  107. // 拼接提示词调用 文生文 接口
  108. $ai = new AIGatewayService();
  109. $gptRes = $ai->txtGptApi($template['english_content'].$record['chinese_description'],$txttotxt_selectedOption);
  110. $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
  111. // 更新数据库记录
  112. Db::name('text_to_image')->where('id', $record['id'])->update([
  113. 'english_description' => $gptText,
  114. 'status_name' => "文生文"
  115. ]);
  116. return 0;
  117. }
  118. }