Przeglądaj źródła

修改富勒下发接口的不同快递面单的获取方式

ajun 4 lat temu
rodzic
commit
b4d5fc7efd

+ 23 - 11
app/Services/DeliveryService.php

@@ -37,8 +37,8 @@ class DeliveryService
         preg_match_all('/[\w]+/', $printStr, $nos);
 
         foreach ($nos[0] as $no) {
-            if (strstr($no, 'SO')) $orderCodes[] = $no;
-            elseif (strstr($no, 'W')) $batchesCodes[] = $no;
+            if (strstr($no, 'SO') || strstr($no,'so')) $orderCodes[] = $no;
+            elseif (strstr($no, 'W') || strstr($no, 'w')  ) $batchesCodes[] = $no;
             else $logisticNumbers[] = $no;
         }
         return [$batchesCodes ?? [], $orderCodes ?? [], $logisticNumbers ?? []];
@@ -50,11 +50,20 @@ class DeliveryService
      * @param $items
      * @return array
      */
-    public function customProcessing(array $items)
+    public function customProcessing(array $items): array
     {
         foreach ($items as $key =>$item) {
-//            if ($item['is_process'] == false) continue;
-             $items[$key] = app(TBDeliveryService::class)->processing($item);
+            if ($item['is_process'] == false) continue;
+            switch ($item('component_type')){
+                case 'TB':
+                    $items[$key] = app(TBDeliveryService::class)->processing($item);
+                    break;
+                case 'PDD':
+                    $items[$key] = app(PDDDeliveryService::class)->processing($item);
+                    break;
+                default:
+                    break;
+            }
         }
         return $items;
     }
@@ -62,20 +71,23 @@ class DeliveryService
     /**
      * 根据缓存删除十分钟前的文件
      */
-    public function destroyFile(){
-        if (Cache::has('print-template-file-list')){
+    public function destroyFileOfCache()
+    {
+        if (Cache::has('print-template-file-list')) {
             $arr = Cache::get('print-template-file-list');
             $now = Carbon::now();
-            foreach ($arr as $key=>$item) {
+            foreach ($arr as $key => $item) {
                 $time = (new Carbon($item['time']))->addMilliseconds(10);
-                if ($now->lt($time)){
-                    if(file_exists($item['path'])){
+                if ($now->lt($time)) {
+                    if (file_exists($item['path'])) {
                         unlink($item['path']);
                     }
                     unset($arr[$key]);
                 }
             }
-            Cache::put('print-template-file-list',$arr);
+            Cache::put('print-template-file-list', $arr);
         }
     }
+
+
 }

+ 25 - 0
app/Services/Interfaces/DeliveryInterface.php

@@ -3,10 +3,35 @@
 namespace App\Services\Interfaces;
 
 
+use Illuminate\Support\Str;
+
 interface DeliveryInterface
 {
+
+    /*
+     *     'type' => 'CAINIAO',              // 需要前台过沟通的组件
+     *     'task_id' => Str::uuid()          // 组件打印任务id
+     *     'is_process' => false,            // is_process 是否已通过自定义模板处理
+     *     'data' => $item['userdefine1'],   // 组件所需要的数据
+     *     'component_type' => 'TB',         // 富勒下发的接口
+     *     'owner_code' => h                 // 货主
+     *     'logistic_code' =>                // 承运商
+     *     'logistic_number' =>              // 快递单号
+     *     'delivery' => $this->getDelivery($item),
+     *     'base64' =>                       // 快递面单的base64编码
+     */
     function getDeliveryInfo($oracleDocOrderDeliveryInfos);
+
     function getDelivery($item);
+
+    /**
+     * 获取打印图片的base64编码
+     * @param $item
+     * @return mixed
+     */
     function getBase64($item);
+    /*
+     * 处理加工
+     */
     function processing(& $params);
 }

+ 18 - 5
app/Services/JDDeliveryService.php

@@ -4,16 +4,20 @@ namespace App\Services;
 
 use App\OracleDocOrderDeliveryInfo;
 use App\OracleDOCOrderHeader;
+use App\OwnerLogisticPrintTemplate;
 use App\Services\Interfaces\DeliveryInterface;
+use App\Traits\DrawImage;
+use Illuminate\Database\Query\Builder;
 use Illuminate\Support\Str;
 
 class JDDeliveryService implements DeliveryInterface
 {
+    use DrawImage;
+
     function getDeliveryInfo($logistic_number)
     {
         return OracleDOCOrderHeader::query()->whereIn('deliveryno',$logistic_number)
             ->where('consigneeId',"JD")
-//            ->where('sostatus', '<>',90)
             ->get()->map(function($item){
             return [
                 // TODO 需要动态获取面单获取的组件 TYPE
@@ -26,23 +30,32 @@ class JDDeliveryService implements DeliveryInterface
                 'logistic_code' => $item->userdefine1 ?? '',
                 'logistic_number' => $item['deliveryno'],
                 'delivery' => null,
-                'base64' => null,
+                'base64' => $this->getBase64($item),
             ];
         })->toArray();
     }
 
-    public function getBase64($item)
+    public function getBase64($item): string
     {
-
+        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', function(Builder $query)use($item){
+            $query->from("owners")->where("code",$item['owner_code']);
+        })->where('logistic_id', function(Builder $query)use($item){
+            $query->from("logistic")->where("code",$item['logistic_code']);
+        })->first();
+        $image = $this->draw($item['delivery'],$item,null);
+        $path = '';
+        $this->saveImage($image,$path);
+        return $this->readImageBase64($path);
     }
 
     function getDelivery($item)
     {
-        // TODO: Implement getDelivery() method.
+
         return null;
     }
 
     function processing(&$params)
     {
+
     }
 }

+ 30 - 13
app/Services/PDDDeliveryService.php

@@ -2,14 +2,16 @@
 
 namespace App\Services;
 
+use App\Logistic;
 use App\OracleDocOrderDeliveryInfo;
 use App\OracleDOCOrderHeader;
-use App\PrintPartImage;
+use App\Owner;
+use App\OwnerLogisticPrintTemplate;
 use App\Services\Interfaces\DeliveryInterface;
 use App\Traits\DrawImage;
+use Illuminate\Support\Carbon;
+use Illuminate\Support\Facades\Cache;
 use Illuminate\Support\Facades\File;
-use Illuminate\Support\Str;
-use Intervention\Image\Facades\Image;
 
 class PDDDeliveryService implements DeliveryInterface
 {
@@ -54,15 +56,30 @@ class PDDDeliveryService implements DeliveryInterface
 
     function processing(&$params)
     {
-        $data = $params['data'];
-        $image = Image::make($data);
-        $orderDeliveryInfo = OracleDocOrderDeliveryInfo::query()->where('')->with('docOrderHead')->where('trackingNo', $params['trackingNo'])->first();
-        $info = [
-            'ORDERNO' => $orderDeliveryInfo->orderno,
-            'TRACKINGNO' => $orderDeliveryInfo->trackingNo,
-        ];
-        $printPartTemp = PrintPartImage::query()->first();
-        $image = $this->draw($info, $printPartTemp, $image);
-        $params['base64'] = $this->getBase64($image,$params);
+        // TODO 获取模板
+        $owner_query = Owner::query()->selectRaw('id')->where('code', $params['owner_code']);
+        $logistic_query = Logistic::query()->selectRaw('id')->where('code', $params['logistic_code']);
+        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', $owner_query)->where('logistic_id', $logistic_query)->first();
+        $item = $item->printTemplate;
+        $item->printTemplate;
+        $path = '';
+        $img = $this->getImage($params['base64'],$path);
+        $img = $this->draw(null,$item,$img);
+        $img->save($path);
+
+        $arr =  Cache::get('print-template-file-list') ?? [];
+
+        $arr[] = ['time' => Carbon::now(),'path' => $path];
+
+        Cache::put('print-template-file-list',$arr);
+
+        // TODO 将文件进行base64编码
+        if ($fp = fopen($path,"rp",0)){
+            $gambar = fread($fp,filesize($path));
+            fclose($fp);
+            $params['base64'] = chunk_split(base64_encode($gambar));
+        }
+        $params['is_process'] = true;
+        return $params;
     }
 }

+ 15 - 4
app/Services/SFDeliveryService.php

@@ -4,11 +4,16 @@ namespace App\Services;
 
 use App\OracleDocOrderDeliveryInfo;
 use App\OracleDOCOrderHeader;
+use App\OwnerLogisticPrintTemplate;
 use App\Services\Interfaces\DeliveryInterface;
+use App\Traits\DrawImage;
+use Illuminate\Database\Query\Builder;
 use Illuminate\Support\Str;
 
 class SFDeliveryService implements DeliveryInterface
 {
+    use DrawImage;
+
     function getDeliveryInfo($OracleDocOrderDeliveryInfos): array
     {
         $items = $OracleDocOrderDeliveryInfos->filter(function($item){
@@ -20,7 +25,7 @@ class SFDeliveryService implements DeliveryInterface
                 // TODO 需要动态获取面单获取的组件 TYPE
                 'type' => 'SF',
                 'task_id' => Str::uuid(),
-                'is_process' => false,
+                'is_process' => true,
                 'data' => $item->docOrderHeader['user'],
                 'component_type' => 'SF',
                 'owner_code' => $item->docOrderHeader->customerid ?? '',
@@ -32,10 +37,17 @@ class SFDeliveryService implements DeliveryInterface
         })->toArray();
     }
 
-
     public function getBase64($item)
     {
-        return null;
+        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', function(Builder $query)use($item){
+            $query->from("owners")->where("code",$item['owner_code']);
+        })->where('logistic_id', function(Builder $query)use($item){
+            $query->from("logistic")->where("code",$item['logistic_code']);
+        })->first();
+        $image = $this->draw($item['delivery'],$item,null);
+        $path = '';
+        $this->saveImage($image,$path);
+        return $this->readImageBase64($path);
     }
 
     function getDelivery($item)
@@ -45,6 +57,5 @@ class SFDeliveryService implements DeliveryInterface
 
     function processing(&$params)
     {
-        // TODO: Implement processing() method.
     }
 }

+ 16 - 5
app/Services/SFQHDDeliveryService.php

@@ -5,11 +5,17 @@ namespace App\Services;
 
 use App\OracleDocOrderDeliveryInfo;
 use App\OracleDOCOrderHeader;
+use App\OwnerLogisticPrintTemplate;
 use App\Services\Interfaces\DeliveryInterface;
+use App\Traits\DrawImage;
+use Illuminate\Database\Query\Builder;
 use Illuminate\Support\Str;
 
 class SFQHDDeliveryService implements DeliveryInterface
 {
+
+    use DrawImage;
+
     function getDeliveryInfo($logistic_number): array
     {
         return OracleDOCOrderHeader::query()->whereIn('deliveryNo',$logistic_number)->where('ConsigneeId',"SFQHD")->where('SoStatus', '<>','90')->get()->map(function($item){
@@ -23,7 +29,7 @@ class SFQHDDeliveryService implements DeliveryInterface
                 'logistic_code' => $item->userdefine1 ?? '',
                 'logistic_number' => $item['deliveryno'],
                 'delivery' => $this->getDelivery($item),
-                'base64' => null,
+                'base64' => $this->getBase64($item),
             ];
         })->toArray();
     }
@@ -32,7 +38,15 @@ class SFQHDDeliveryService implements DeliveryInterface
 
     public function getBase64($item)
     {
-        return null;
+        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', function(Builder $query)use($item){
+            $query->from("owners")->where("code",$item['owner_code']);
+        })->where('logistic_id', function(Builder $query)use($item){
+            $query->from("logistic")->where("code",$item['logistic_code']);
+        })->first();
+        $image = $this->draw($item['delivery'],$item,null);
+        $path = '';
+        $this->saveImage($image,$path);
+        return $this->readImageBase64($path);
     }
 
 
@@ -43,10 +57,7 @@ class SFQHDDeliveryService implements DeliveryInterface
 
     function processing(&$params)
     {
-        foreach ($params as $param) {
-            if($param['type'] != 'SFQHD') continue;
 
-        }
     }
 
     function construct($param)

+ 5 - 8
app/Services/TBDeliveryService.php

@@ -2,13 +2,9 @@
 
 namespace App\Services;
 
-
 use App\Logistic;
-use App\OracleDocOrderDeliveryInfo;
-use App\OracleDOCOrderHeader;
 use App\Owner;
 use App\OwnerLogisticPrintTemplate;
-use App\PrintTemplate;
 use App\Services\Interfaces\DeliveryInterface;
 use App\Traits\DrawImage;
 use Illuminate\Support\Carbon;
@@ -53,13 +49,14 @@ class TBDeliveryService implements DeliveryInterface
         return null;
     }
 
+    // 将图片上传并转为base64编码
     function processing(&$params)
     {
         // TODO 获取模板
-//        $owner_query = Owner::query()->selectRaw('id')->where('code', $params['owner_code']);
-//        $logistic_query = Logistic::query()->selectRaw('id')->where('code', $params['owner_code']);
-//        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', $owner_query)->where('logistic_id', $logistic_query)->first();
-        $item = PrintTemplate::query()->first();
+        $owner_query = Owner::query()->selectRaw('id')->where('code', $params['owner_code']);
+        $logistic_query = Logistic::query()->selectRaw('id')->where('code', $params['logistic_code']);
+        $item = OwnerLogisticPrintTemplate::query()->with('printTemplate')->where('owner_id', $owner_query)->where('logistic_id', $logistic_query)->first();
+        $item = $item->printTemplate;
         $item->printTemplate;
         $path = '';
         $img = $this->getImage($params['base64'],$path);

+ 26 - 5
app/Traits/DrawImage.php

@@ -26,7 +26,8 @@ trait DrawImage
             return $item['type'] == 'bg';
         });
         $array = array_shift($array);
-//        dd($array,$img->getHeight(),$img->getWidth());
+
+        // 按照模型比列修改image
         $items = (function()use($items,$img,$array){
             $heightScale = $img->getHeight() / $array['height'];
             $weightScale = $img->getWidth() / $array['width'];
@@ -179,6 +180,14 @@ trait DrawImage
 
     public function getImage($base64, &$path): \Intervention\Image\Image
     {
+
+        $path = $this->getImagePath();
+        $img = Image::make($base64);
+        $img->save($path);          // 图片保存
+        return $img;
+    }
+
+    private function getImagePath(){
         $img_name = Str::uuid() . '.png';
         $path_arr = ['app', 'public', 'print'];
         $dir_path = implode(DIRECTORY_SEPARATOR, $path_arr);
@@ -189,11 +198,23 @@ trait DrawImage
         if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
 
         $path_arr[] = $img_name;
-        $path = storage_path(implode(DIRECTORY_SEPARATOR, $path_arr));
 
-        $img = Image::make($base64);
-        $img->save($path);          // 图片保存
-        return $img;
+        return storage_path(implode(DIRECTORY_SEPARATOR, $path_arr));
+    }
+
+    public function saveImage($image,&$path){
+        $path = $this->getImagePath();
+        $image->save($path);          // 图片保存
+    }
+
+    public function readImageBase64($path): string
+    {
+        if ($fp = fopen($path,"rp",0)){
+            $gambar = fread($fp,filesize($path));
+            fclose($fp);
+            return  chunk_split(base64_encode($gambar));
+        }
+        return "";
     }
 
 }