Gorden 4 months ago
parent
commit
d27bd5de3e
1 changed files with 109 additions and 41 deletions
  1. 109 41
      app/command/GoodsThumbCommand.php

+ 109 - 41
app/command/GoodsThumbCommand.php

@@ -3,6 +3,7 @@
 namespace app\command;
 
 use app\model\Goods;
+use app\model\GoodsDetail;
 use Intervention\Image\Gd\Driver;
 use Intervention\Image\ImageManager;
 use Intervention\Image\ImageManagerStatic;
@@ -32,52 +33,119 @@ class GoodsThumbCommand extends Command
      * @param OutputInterface $output
      * @return int
      */
+//    protected function execute(InputInterface $input, OutputInterface $output): int
+//    {
+//        $goods = Goods::select('goods_id','goods_cover')->skip(10)->limit(10)->get()->toArray();
+//
+//        foreach ($goods as $good){
+//            if (!file_exists(public_path($good['goods_cover']))){
+//                echo "【".$good['goods_id']."】源文件不存在,跳过 \n";
+//                continue;
+//            }
+//            $fileNameArray = explode(DIRECTORY_SEPARATOR,$good['goods_cover']);
+//            $two = $fileNameArray[count($fileNameArray)-2];
+//            if ($two == 'thumb'){
+//                echo "【".$good['goods_id']."】已存在,跳过 \n";
+//                continue;
+//            }
+//            array_splice($fileNameArray,-1,0,'thumb');
+//
+//            $thumbPath = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$fileNameArray),DIRECTORY_SEPARATOR));
+//            if (file_exists($thumbPath)){
+//                echo "【".$good['goods_id']."】缩略图已存在,跳过 \n";
+//                continue;
+//            }
+//            $path = array_slice($fileNameArray,0,-1);
+//            $pathStr = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$path),DIRECTORY_SEPARATOR));
+//
+//            if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)){
+//                throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
+//            }
+//
+//            $image = ImageManagerStatic::make(public_path($good['goods_cover']));
+//
+//            $imgWidth = $image->width();
+//            $imgHeight = $image->height();
+//            $rate = round($imgWidth / 200, 2);
+//            $height = intval($imgHeight / $rate);
+//            $image = $image->resize(200, $height);
+//            $encoded = $image->encode('jpg');
+//            $encoded->save($thumbPath);
+//
+//            Goods::where('goods_id',$good['goods_id'])->update(['goods_cover'=>implode(DIRECTORY_SEPARATOR,$fileNameArray)]);
+//
+//            echo "【".$good['goods_id']."】已完成 \n";
+//        }
+//
+//
+//
+//        return self::SUCCESS;
+//    }
+
     protected function execute(InputInterface $input, OutputInterface $output): int
     {
-        $goods = Goods::select('goods_id','goods_cover')->get()->toArray();
-
-        foreach ($goods as $good){
-            if (!file_exists(public_path($good['goods_cover']))){
-                echo "【".$good['goods_id']."】源文件不存在,跳过 \n";
-                continue;
-            }
-            $fileNameArray = explode(DIRECTORY_SEPARATOR,$good['goods_cover']);
-            $two = $fileNameArray[count($fileNameArray)-2];
-            if ($two == 'thumb'){
-                echo "【".$good['goods_id']."】已存在,跳过 \n";
-                continue;
-            }
-            array_splice($fileNameArray,-1,0,'thumb');
-
-            $thumbPath = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$fileNameArray),DIRECTORY_SEPARATOR));
-            if (file_exists($thumbPath)){
-                echo "【".$good['goods_id']."】缩略图已存在,跳过 \n";
-                continue;
-            }
-            $path = array_slice($fileNameArray,0,-1);
-            $pathStr = public_path(ltrim(implode(DIRECTORY_SEPARATOR,$path),DIRECTORY_SEPARATOR));
-
-            if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)){
-                throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
+        $details = GoodsDetail::select('join_detail_goods_id', 'goods_detail_slider_json')
+            ->skip(0)
+            ->limit(1)
+            ->get()
+            ->toArray();
+
+        foreach ($details as $detail) {
+            if (!empty($detail['goods_detail_slider_json'])) {
+                $sliderJson = json_decode($detail['goods_detail_slider_json'], true);
+                $sliders = explode(',', $sliderJson['slider']);
+                $sliderData = [];
+                foreach ($sliders as $slider) {
+                    if (!file_exists(public_path($slider))) {
+                        $sliderData[] = $slider;
+                        echo "【" . $slider . "】源文件不存在,跳过 \n";
+                        continue;
+                    }
+                    $fileNameArray = explode('/', $slider);
+                    $two = $fileNameArray[count($fileNameArray) - 2];
+                    if ($two == 'thumb') {
+                        echo "【" . $detail['join_detail_goods_id'] . "】已存在,跳过 \n";
+                        continue;
+                    }
+                    array_splice($fileNameArray, -1, 0, 'thumb');
+
+                    $thumbPath = public_path(ltrim(implode('/', $fileNameArray), '/'));
+                    if (file_exists($thumbPath)) {
+                        $sliderData[] = implode('/', $fileNameArray);
+                        echo "【" . $detail['join_detail_goods_id'] . "】缩略图已存在,跳过 \n";
+                        continue;
+                    }
+                    $path = array_slice($fileNameArray, 0, -1);
+                    $pathStr = public_path(ltrim(implode('/', $path), '/'));
+
+                    if (!is_dir($pathStr) && !mkdir($pathStr, 0755, true)) {
+                        throw new StorageException('文件夹创建失败,请核查是否有对应权限。');
+                    }
+
+                    $image = ImageManagerStatic::make(public_path($slider));
+                    $imgWidth = $image->width();
+                    $imgHeight = $image->height();
+                    $rate = round($imgWidth / 200, 2);
+                    $height = intval($imgHeight / $rate);
+                    $image = $image->resize(200, $height);
+                    $encoded = $image->encode('jpg');
+                    $encoded->save($thumbPath);
+
+                    $sliderData[] = implode('/', $fileNameArray);
+
+                }
+                if (empty($sliderData)){
+                    echo "【" . $detail['join_detail_goods_id'] . "】跳过 \n";
+                    continue;
+                }
+
+                GoodsDetail::where('join_detail_goods_id',$detail['join_detail_goods_id'])
+                    ->update(['goods_detail_slider_json'=>json_encode(['slider'=>implode(',',$sliderData)])]);
+
+                echo "【" . $detail['join_detail_goods_id'] . "】已完成 \n";
             }
-
-            $image = ImageManagerStatic::make(public_path($good['goods_cover']));
-
-            $imgWidth = $image->width();
-            $imgHeight = $image->height();
-            $rate = round($imgWidth / 200, 2);
-            $height = intval($imgHeight / $rate);
-            $image = $image->resize(200, $height);
-            $encoded = $image->encode('jpg');
-            $encoded->save($thumbPath);
-
-            Goods::where('goods_id',$good['goods_id'])->update(['goods_cover'=>implode(DIRECTORY_SEPARATOR,$fileNameArray)]);
-
-            echo "【".$good['goods_id']."】已完成 \n";
         }
 
-
-
         return self::SUCCESS;
     }