cqpCow hai 1 ano
pai
achega
ac980aafb6
Modificáronse 2 ficheiros con 25 adicións e 1 borrados
  1. 10 1
      app/Service/ProductService.php
  2. 15 0
      app/Service/Service.php

+ 10 - 1
app/Service/ProductService.php

@@ -208,10 +208,16 @@ class ProductService extends Service
                 $models->save();
             }
 
+            $old = ProductInfo::where('del_time',0)
+                ->where('product_id',$data['id'])
+                ->select('file')
+                ->get()->toArray();
+            $old = array_column($old,'file');
             ProductInfo::where('del_time',0)
                 ->where('product_id',$data['id'])
                 ->update(['del_time' => $time]);
 
+            $new = [];
             if(! empty($data['img'])){
                 $insert = [];
                 foreach ($data['img'] as $value){
@@ -222,10 +228,10 @@ class ProductService extends Service
                         'name' => $value['name'],
                         'crt_time' => $time,
                     ];
+                    $new[] = $value['url'];
                 }
                 ProductInfo::insert($insert);
             }
-
             if(! empty($data['file'])){
                 $insert = [];
                 foreach ($data['file'] as $value){
@@ -236,6 +242,7 @@ class ProductService extends Service
                         'name' => $value['name'],
                         'crt_time' => $time,
                     ];
+                    $new[] = $value['url'];
                 }
                 ProductInfo::insert($insert);
             }
@@ -263,6 +270,8 @@ class ProductService extends Service
             return [false,$exception->getMessage()];
         }
 
+        $this->delStorageFile($old, $new);
+
         return [true,''];
     }
 

+ 15 - 0
app/Service/Service.php

@@ -6,6 +6,7 @@ namespace App\Service;
 use App\Jobs\OperationLog;
 use App\Model\Employee;
 use Illuminate\Support\Facades\Redis;
+use Illuminate\Support\Facades\Storage;
 
 /**
  * 公用的公共服务
@@ -461,4 +462,18 @@ class Service
 
         return true;
     }
+
+    public function delStorageFile($old, $new, $dir = "upload_files/"){
+        foreach ($old as $value){
+            if(! in_array($value, $new)){
+                $filename_rep = "/api/uploadFiles/";
+                $filename = str_replace($filename_rep, "", $value);
+                $filePath = $dir . $filename;
+                if (Storage::disk('public')->exists($filePath)) {
+                    // 文件存在 进行删除操作
+                    Storage::disk('public')->delete($filePath);
+                }
+            }
+        }
+    }
 }