cqpCow 8 ay önce
ebeveyn
işleme
c32eff4637

+ 13 - 0
app/Http/Controllers/Api/ProductController.php

@@ -140,6 +140,19 @@ class ProductController extends BaseController
         }
     }
 
+    public function batchUploadImg(Request $request)
+    {
+        $service = new ProductService();
+        $user = $request->userData->toArray();
+        list($status,$data) = $service->batchUploadImg($request->all());
+
+        if($status){
+            return $this->json_return(200,'',$data);
+        }else{
+            return $this->json_return(201,$data);
+        }
+    }
+
     public function productList(Request $request)
     {
         $service = new ProductService();

+ 9 - 3
app/Http/Middleware/OssFileDeal.php

@@ -38,10 +38,16 @@ class OssFileDeal
                 $service = new FileUploadService();
 
                 if(! empty($result['file'])){
-                    //添加oss
                     $file = $result['file'];
-                    if(! empty($file['new'])){
-                        $service->createOssUpload($file['new']);
+                    if(! isset($result['is_batch'])){
+                        //添加oss
+                        if(! empty($file['new'])){
+                            $service->createOssUpload($file['new']);
+                        }
+                    }else{
+                        if(! empty($file['new']['origin']) && ! empty($file['new']['img_list'])){
+                            $service->createOssUploadBatch($file['new']);
+                        }
                     }
 
                     //编辑|删除oss

+ 21 - 2
app/Service/FileUploadService.php

@@ -99,7 +99,7 @@ class FileUploadService extends Service
     }
 
     public function createOssUpload($file){
-        if(! is_array($file) && empty($file)) return;
+        if(! is_array($file) || empty($file)) return;
         foreach ($file as $filename){
             $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
             $timestamp = substr($filename, 0, 8); // 截取前八位数字
@@ -114,7 +114,7 @@ class FileUploadService extends Service
     }
 
     public function createOssUploadOld($file){
-        if(! is_array($file) && empty($file)) return;
+        if(! is_array($file) || empty($file)) return;
 
         foreach ($file as $filename){
             if(strpos($filename, FileUploadService::string.FileUploadService::string2) !== false){
@@ -135,4 +135,23 @@ class FileUploadService extends Service
             }
         }
     }
+
+    public function createOssUploadBatch($file){
+        if(empty($file['origin']) || empty($file['img_list'])) return;
+        $from = $file['origin'];
+        $filename = str_replace(FileUploadService::string.FileUploadService::string2,'', $from);
+        $timestamp = substr($filename, 0, 8); // 截取前八位数字
+        $date = \DateTime::createFromFormat('Ymd', $timestamp);
+        $date = $date->format('Y-m-d');
+        $dir = FileUploadService::tmp_dir . '/' . $date . '/' . $filename;
+        $realPath = storage_path() . "/app/public/" . $dir;
+
+        foreach ($file['img_list'] as $filename){
+            $filename_tmp = str_replace(FileUploadService::string.FileUploadService::string2,'', $filename);
+            $savePath = self::string3 . $date . '/' . $filename_tmp;
+            list($status,$msg) = (new OssService())->uploadFile($realPath,$savePath);
+        }
+
+        Storage::disk('public')->delete($dir);
+    }
 }

+ 49 - 0
app/Service/ProductService.php

@@ -859,6 +859,55 @@ class ProductService extends Service
         return $img;
     }
 
+    public function batchUploadImg($data){
+        if(empty($data['product_id'])) return [false, '请选择产品'];
+
+        $time = time();
+        try {
+            DB::beginTransaction();
+
+            $old = ProductInfo::where('del_time',0)
+                ->where('type',ProductInfo::type_one)
+                ->whereIn('product_id',$data['product_id'])
+                ->select('file')
+                ->get()->toArray();
+            $old = array_column($old,'file');
+            ProductInfo::where('del_time',0)
+                ->where('type',ProductInfo::type_one)
+                ->whereIn('product_id',$data['product_id'])
+                ->update(['del_time' => $time]);
+
+            $new['origin'] = $data['img_url'] ?? "";
+            $new['img_list'] = [];
+            $img = str_replace(FileUploadService::string . FileUploadService::string2, '', $data['img_url']);
+            $img = explode('.', $img);
+
+            $insert = [];
+            if(! empty($data['img_url'])){
+                foreach ($data['product_id'] as $key => $value){
+                    $copy = $img[0] . 'C' . $key . '.' . $img[1];
+                    $copy = FileUploadService::string . FileUploadService::string2 . $copy;
+                    $insert[] = [
+                        'product_id' => $value,
+                        'file' => $copy,
+                        'type' => ProductInfo::type_one,
+                        'name' => $data['img_name'] ?? "",
+                        'crt_time' => $time,
+                    ];
+                    $new['img_list'][] = $copy;
+                }
+                if(! empty($insert)) ProductInfo::insert($insert);
+            }
+
+            DB::commit();
+        }catch (\Throwable $exception){
+            DB::rollBack();
+            return [false, $exception->getMessage()];
+        }
+
+        return [true, ['is_batch' => true, 'file' => ['new' => $new, 'old' => $old]]];
+    }
+
     //获取产品字典
     public function getProductDetail($product_id = []){
         if(empty($product_id)) return [];

+ 1 - 0
routes/api.php

@@ -156,6 +156,7 @@ Route::group(['middleware'=> ['checkLogin']],function ($route){
     $route->any('productDel', 'Api\ProductController@productDel')->middleware('OssFileDeal');
     $route->any('productDetail', 'Api\ProductController@productDetail');
     $route->any('productList2', 'Api\ProductController@productList2');
+    $route->any('batchUploadImg', 'Api\ProductController@batchUploadImg')->middleware('OssFileDeal');
 
     //采购单
     $route->any('purchaseOrderList', 'Api\PurchaseOrderController@purchaseOrderList');