TextToTextJob.php 4.4 KB

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