liuhairui 6 сар өмнө
parent
commit
96743506d8

+ 4 - 3
application/api/controller/Facility.php

@@ -81,8 +81,8 @@ class Facility extends Api
             ->field('b.chinese_description,b.english_description,b.new_image_url,b.custom_image_url,b.size,b.old_image_url,b.img_name')
             ->where('old_image_url', $params)
             ->where('img_name', '<>', '')
-            ->where('custom_image_url', '<>', '')
-            ->where('status', 1)
+            // ->where('custom_image_url', '<>', '')
+            // ->where('status', 1)
             ->order('b.id desc')
             ->select();
         return json(['code' => 0, 'msg' => '查询成功', 'data' => $res,'count'=>count($res)]);
@@ -167,6 +167,7 @@ class Facility extends Api
                 'new_image_url' => $processed['new_image_url'] ?? '',
                 'custom_image_url' => $processed['custom_image_url'] ?? '',
                 'chinese_description' => ($processed['chinese_description'] ?? '') . ($processed['english_description'] ?? ''),
+                'english_description' => ($processed['english_description'] ?? '') . ($processed['english_description'] ?? ''),
                 'same_count' => $sameCountMap[$relative] ?? 0
             ];
         }
@@ -420,7 +421,7 @@ class Facility extends Api
 
     /**
      * 模版
-    */
+     */
     public function Template(){
         $Template = Db::name("template")->find();
         return json([

+ 4 - 4
application/api/controller/WorkOrder.php

@@ -23,12 +23,12 @@ class WorkOrder extends Api
     public function imageToText()
     {
         $params = $this->request->param();
+
         $service = new ImageService();
         $service->handleImage($params);
         $this->success('成功存入队列中');
     }
 
-
     /**
      * 开启队列任务
      */
@@ -122,12 +122,12 @@ class WorkOrder extends Api
         $count1 = $redis->lLen($key1);
         $count = $count+$count1;
         // 计算时间:当前时间前30分钟
-        $time = date('Y-m-d H:i:s', strtotime('-30 minutes'));
+//        $time = date('Y-m-d H:i:s', strtotime('-30 minutes'));
 
         if ($count === 0) {
             // 删除数据库中状态为0且 created_at 在最近30分钟的数据
             $deleteCount = Db::name('queue_log')
-                ->where('created_at', '>=', $time)
+//                ->where('created_at', '>=', $time)
                 ->delete();
             return json([
                 'code' => 1,
@@ -142,7 +142,7 @@ class WorkOrder extends Api
 
         // 删除数据库中状态为0且 created_at 在最近30分钟的数据
         $deleteCount = Db::name('queue_log')
-            ->where('created_at', '>=', $time)
+//            ->where('created_at', '>=', $time)
             ->delete();
 
         return json([

+ 21 - 15
application/job/ImageArrJob.php

@@ -8,23 +8,29 @@ class ImageArrJob
 {
     public function fire(Job $job, $data)
     {
-        echo "批量推送任务已开始\n";
-//        echo "接收的数据: " . json_encode($data) . "\n";
-        //任务分解
-        foreach ($data as $key => $value){
-            // 推送队列前,记录日志
-            $id = Db::name('queue_log')->insertGetId([
-                'job_name' => 'app\job\ImageJob',
-                'status' => 0,
-                'data' => json_encode($value, JSON_UNESCAPED_UNICODE),
-                'created_at' => date('Y-m-d H:i:s'),
-            ]);
+        //第一步:接收前端批量上传图片的任务
+        echo  date('Y-m-d H:i:s') ."批量推送任务已开始\n";
 
-            // 将日志ID传入队列中
-            $value['log_id'] = $id;
-            Queue::push('app\job\ImageJob', $value,"imgtotxt");
+        foreach ($data as $key => $value) {
+            // 判断 type 字段
+            if (isset($value['type']) && $value['type'] === '文生图') {
+                echo date('Y-m-d H:i:s') ."跳过图生文任务,批量执行文生图任务已开始\n";
+                Queue::push('app\job\TextToImageJob', $value, 'txttoimg');
+            } else {
+                // 示例:推送 ImageJob 队列
+                $id = Db::name('queue_log')->insertGetId([
+                    'job_name' => 'app\job\ImageJob',
+                    'status' => 0,
+                    'data' => json_encode($value, JSON_UNESCAPED_UNICODE),
+                    'created_at' => date('Y-m-d H:i:s'),
+                ]);
+                echo date('Y-m-d H:i:s') ."批量推送任务已结束,推送到图生文任务已开始\n";
+                $value['log_id'] = $id;
+                Queue::push('app\job\ImageJob', $value, 'imgtotxt');
+            }
         }
-        echo "批量推送任务结束\n";
+
+        // 最后删除任务
         $job->delete();
     }
 

+ 41 - 18
application/job/ImageJob.php

@@ -26,15 +26,16 @@ class ImageJob
      */
     public function fire(Job $job, $data)
     {
-        echo "图生文开始\n";
+        echo date('Y-m-d H:i:s')."图生文开始\n";
 //        echo "接收的数据: " . json_encode($data) . "\n";
 
         $logId = $data['log_id'] ?? null;
         try {
-            // 如果有 log_id,更新任务状态为“正在处理”
+            //录日志状态为“正在处理”
             if ($logId) {
                 Db::name('queue_log')->where('id', $logId)->update([
-                    'status' => 1, // 正在处理
+                    'status'     => 1,
+                    'log'        => '图生文正在处理',
                     'updated_at' => date('Y-m-d H:i:s')
                 ]);
             }
@@ -42,24 +43,38 @@ class ImageJob
             // 执行业务逻辑
             $str = $this->processImage($data);
             echo $str;
-            echo "图生文结束\n";
+            echo date('Y-m-d H:i:s')."图生文结束\n";
+
+            if ($logId) {
+                Db::name('queue_log')->where('id', $logId)->update([
+                    'status'     => 2,
+                    'log'        => '图生文执行成功',
+                    'updated_at' => date('Y-m-d H:i:s')
+                ]);
+            }
+
             $job->delete();
         } catch (\Exception $e) {
-            // 如果有 log_id,更新任务状态为“执行失败”并记录错误信息
+            //异常处理,记录失败日志
+            echo "错误信息: " . $e->getMessage() . "\n";
+            echo "文件: " . $e->getFile() . "\n";
+            echo "行号: " . $e->getLine() . "\n";
+
             if ($logId) {
                 Db::name('queue_log')->where('id', $logId)->update([
-                    'status' => 3, // 执行失败
-                    'log' => $e->getMessage(),
+                    'status'     => 3, // 3 表示失败
+                    'log'        => '图生文失败:' . $e->getMessage(),
                     'updated_at' => date('Y-m-d H:i:s')
                 ]);
             }
+            $job->delete();
 
             // 最多重试一次(总执行两次)
-            if ($job->attempts() < 2) {
-                $job->release(30); // 延迟30秒再次执行
-            } else {
-                $job->failed(); // 达到最大尝试次数,标记失败
-            }
+            // if ($job->attempts() < 2) {
+            //     $job->release(30); // 延迟30秒再次执行
+            // } else {
+            //     $job->failed(); // 达到最大尝试次数,标记失败
+            // }
         }
     }
 
@@ -88,7 +103,6 @@ class ImageJob
      */
     public function imageToText($sourceDirRaw, $fileName, $prompt, $call_data)
     {
-
         // 自动拆分文件名
         if (!$fileName && preg_match('/([^\/]+\.(jpg|jpeg|png))$/i', $sourceDirRaw, $matches)) {
             $fileName = $matches[1];
@@ -206,7 +220,7 @@ class ImageJob
             'chinese_description' => $chineseDesc,
             'english_description' => $englishDesc,
             'size' => "",
-            'status' => 0 // 正常待图生图状态
+            'status' => 0
         ]);
         //分解任务
         $arr = [
@@ -217,10 +231,19 @@ class ImageJob
             "englishDesc"=>$englishDesc,
             "img_name"=>$img_name
         ];
-        echo "现在推送";
-        Queue::push('app\job\TextToImageJob', $arr,'txttoimg');
-        return ;
-        // 执行文生图
+
+        if($call_data['type'] == '图生文'){
+            echo "执行图生文结束";
+            return ;
+        }else{
+            // 执行文生图
+            echo "现在推送";
+            Queue::push('app\job\TextToImageJob', $arr,'txttoimg');
+            return ;
+        }
+
+
+
 
     }
     public function logToDatabase($data)

+ 62 - 22
application/job/TextToImageJob.php

@@ -21,33 +21,70 @@ class TextToImageJob
 
     public function fire(Job $job, $data)
     {
-        echo "已经到了文生图阶段\n";
+        echo date('Y-m-d H:i:s') . " 任务开始执行\n";
+
         try {
-            $str = $this->textToImage(
-                $data["fileName"],
-                $data["outputDir"],
-                $data["width"],
-                $data["height"],
-                $data["englishDesc"],
-                $data["img_name"]
-            );
-            echo $str;
-            echo "文生图结束\n";
-            $job->delete();
-        } catch (\Exception $e) {
-            echo "Error message: " . $e->getMessage() . "\n";
-            echo "Error file: " . $e->getFile() . "\n";
-            echo "Error line: " . $e->getLine() . "\n";
-            echo "Stack trace: " . $e->getTraceAsString() . "\n";
-            // 如果有 log_id,更新任务状态为“执行失败”并记录错误信息
-            // 最多重试一次(总执行两次)
-            if ($job->attempts() < 2) {
-                $job->release(30); // 延迟30秒再次执行
+            if (isset($data['type']) && $data['type'] === '文生图') {
+                echo date('Y-m-d H:i:s') . " [文生图 - 数据库模式] 阶段开始\n";
+
+                $list = Db::name("text_to_image")
+                    ->where('old_image_url', $data['sourceDir'] . '/' . $data['file_name'])
+                    ->where('img_name', '<>', '')
+                    ->where('status', 0)
+                    ->select();
+
+                if (!empty($list)) {
+                    foreach ($list as $index => $row) {
+                        echo "处理第 " . ($index + 1) . " 条数据,处理ID:" . $row['id'] . "\n";
+
+                        // 调用 textToImage 方法
+                        $result = $this->textToImage(
+                            $data["file_name"],          // 文件名
+                            $data["outputDir"],          // 输出目录
+                            $data["width"],              // 宽度
+                            $data["height"],             // 高度
+                            $row["english_description"], // 英文描述
+                            $row["img_name"]             // 图片名
+                        );
+
+                        echo "处理结果:" . $result . "\n";
+
+                    }
+
+                    echo date('Y-m-d H:i:s') . " 文生图任务全部执行完成\n";
+                } else {
+                    echo "未找到 status=0 的数据,跳过执行\n";
+                }
+
+                $job->delete(); // 删除队列任务
+
             } else {
-                $job->failed(); // 达到最大尝试次数,标记失败
+                echo date('Y-m-d H:i:s') . " [文生图 - 单条传参模式] 开始执行\n";
+
+                $result = $this->textToImage(
+                    $data["fileName"],
+                    $data["outputDir"],
+                    $data["width"],
+                    $data["height"],
+                    $data["englishDesc"],
+                    $data["img_name"]
+                );
+
+                echo "返回结果:" . $result . "\n";
+                echo date('Y-m-d H:i:s') . " 单条文生图执行结束\n";
+
+                $job->delete();
             }
+
+        } catch (\Exception $e) {
+            echo "异常信息: " . $e->getMessage() . "\n";
+            echo "文件: " . $e->getFile() . "\n";
+            echo "行号: " . $e->getLine() . "\n";
+            echo date('Y-m-d H:i:s') . " 任务执行失败\n";
+            $job->delete();
         }
     }
+
     /**
      * 任务失败时的处理
      */
@@ -93,6 +130,9 @@ class TextToImageJob
 //        echo 2345;
         // 调用文生图模型接口生成图像
         $startTime = microtime(true);
+//         echo "<pre>";
+// print_r($prompt);
+// echo "<pre>";die;
         $dalle1024 = $this->callDalleApi($prompt);
         // $dalle1024 = json_decode('{"created":1747932746,"data":[{"revised_prompt":"**First paragraph:**   A geometric abstract design with a central motif consisting of curved and angular shapes in a symmetrical arrangement. The color scheme predominantly features shades of blue, with hints of white and black creating a contrast. The design incorporates smooth, flowing lines mixed with sharp angles. The overall style has a modern, minimalist aesthetic, with a focus on balance and clean shapes.","url":"https:\/\/filesystem.site\/cdn\/20250523\/3NVcCUaZDkLimWjtgOwJYniGezDX8d.png"}],"usage":{"total_tokens":4250,"input_tokens":75,"output_tokens":4175,"input_tokens_details":{"text_tokens":75}}}',true);
 

+ 1 - 0
application/service/ImageService.php

@@ -22,6 +22,7 @@ class ImageService
                 "sourceDir" => $this->sourceDir($v, 1) ?? '',
                 "outputDir" => $this->sourceDir($v, 2) ?? '',
                 "file_name" => $this->sourceDir($v, 3) ?? '',
+                "type" => $params['type'] ?? '',
                 "prompt" => $params['prompt'] ?? '',
                 "width" => $params['width'] ?? 512,
                 "height" => $params['height'] ?? 512