| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- <?php
- namespace app\job;
- use think\Db;
- use think\queue\Job;
- use think\Queue;
- class TextToTextJob
- {
- protected $config = [
- 'gpt' => [
- 'api_key' => 'sk-Bhos1lXTRpZiAAmN06624a219a874eCd91Dc068b902a3e73',
- 'api_url' => 'https://one.opengptgod.com/v1/chat/completions'
- ],
- 'dalle' => [
- 'api_key' => 'sk-e0JuPjMntkbgi1BoMjrqyyzMKzAxILkQzyGMSy3xiMupuoWY',
- 'api_url' => 'https://niubi.zeabur.app/v1/images/generations'
- ]
- ];
- /**
- * 文生文
- */
- 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']);
- 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')
- ]);
- }
- }
- } 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();
- }
- /**
- * 文生文接口
- */
- public function textToTxt($id)
- {
- $template = Db::name('template')
- ->field('id,english_content')
- ->where('ids',1)
- ->find();
- $record = Db::name('text_to_image')
- ->field('id,english_description')
- ->where('id',$id)
- ->order('id desc')
- ->find();
- if (!$record) {return '没有找到匹配的图像记录';}
- // 调用文生文
- $gptRes = $this->TxtGptApi($template['english_content'].$record['english_description']);
- $gptText = trim($gptRes['choices'][0]['message']['content'] ?? '');
- // 更新数据库记录
- Db::name('text_to_image')->where('id', $record['id'])->update([
- 'english_description' => $gptText
- ]);
- return 0;
- }
- /**
- * 文升文模型
- */
- public function TxtGptApi($prompt)
- {
- $data = [
- 'prompt' => $prompt,
- 'model' => 'gpt-4',
- 'session_id' => null,
- 'context_reset' => true
- ];
- return $this->callApi($this->config['gpt']['api_url'],$this->config['gpt']['api_key'],$data);
- }
- /**
- * 通用API调用方法
- */
- public function callApi($url, $apiKey, $data)
- {
- $maxRetries = 2;
- $attempt = 0;
- $lastError = '';
- while ($attempt <= $maxRetries) {
- $ch = curl_init();
- curl_setopt_array($ch, [
- CURLOPT_URL => $url,
- CURLOPT_RETURNTRANSFER => true,
- CURLOPT_POST => true,
- CURLOPT_POSTFIELDS => json_encode($data),
- CURLOPT_HTTPHEADER => [
- 'Content-Type: application/json',
- 'Authorization: Bearer ' . $apiKey
- ],
- CURLOPT_TIMEOUT => 120,
- CURLOPT_SSL_VERIFYPEER => false,
- CURLOPT_SSL_VERIFYHOST => 0,
- CURLOPT_TCP_KEEPALIVE => 1,
- CURLOPT_FORBID_REUSE => false
- ]);
- $response = curl_exec($ch);
- $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
- $curlError = curl_error($ch);
- curl_close($ch);
- if ($response !== false && $httpCode === 200) {
- $result = json_decode($response, true);
- return $result;
- }
- $lastError = $curlError ?: "HTTP错误:{$httpCode}";
- $attempt++;
- sleep(1);
- }
- throw new \Exception("请求失败(重试{$maxRetries}次):{$lastError}");
- }
- }
|