PrintService.php 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace App\Services;
  3. use App\PrintTemplate;
  4. use App\Traits\DrawImage;
  5. use App\Traits\ModelSearchWay;
  6. use App\Traits\ServiceAppAop;
  7. use Illuminate\Support\Str;
  8. use Intervention\Image\Facades\Image;
  9. class PrintService
  10. {
  11. use ModelSearchWay;
  12. use ServiceAppAop;
  13. use DrawImage;
  14. /**
  15. * 处理打印请求参数
  16. * @param $oracleDocOrderDeliveryInfos
  17. * @return array
  18. */
  19. public function disposesPrintInfo($oracleDocOrderDeliveryInfos): array
  20. {
  21. foreach ($oracleDocOrderDeliveryInfos as $key => $info){
  22. $data[] = [
  23. 'documentID' => $info->orderno ?? '',
  24. 'encryptedData' => $info->userDefine4->encryptedData ?? '',
  25. 'signature' => $info->userDefine4->signature ?? '',
  26. 'templateURL' => $info->userDefine4->templateURL ?? '',
  27. 'ver' => $info->userDefine4->ver ?? '',
  28. ];
  29. }
  30. return $data ?? [];
  31. }
  32. /**
  33. * 处理打印参数 返回 打印图片
  34. * @param array $params
  35. * @return mixed
  36. */
  37. public function processingPrintData(array $params)
  38. {
  39. $array = [];
  40. foreach ($params as $param) {
  41. $img_base64_code = $param['base64'];
  42. $img = Image::make($img_base64_code);
  43. $printTemplate = PrintTemplate::query()->where('name','123')->first();
  44. $img = $this->convertPrintImage($img,$printTemplate);
  45. $img_name = Str::uuid().'.png';
  46. $path_arr = ['app','public','print'];
  47. $dir_path = implode(DIRECTORY_SEPARATOR,$path_arr);
  48. if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
  49. $path_arr[] = 'image';
  50. $dir_path = implode(DIRECTORY_SEPARATOR,$path_arr);
  51. if (!file_exists(storage_path($dir_path))) mkdir(storage_path($dir_path));
  52. $path_arr[] = $img_name;
  53. $path = storage_path(implode(DIRECTORY_SEPARATOR,$path_arr));
  54. $img->save($path); // 图片保存
  55. $file = file_get_contents($path);
  56. $array[] = [
  57. 'type' => '',
  58. 'base64' => base64_encode($file),
  59. 'logistic_number' => '',
  60. ''
  61. ];
  62. }
  63. return $array;
  64. }
  65. /**
  66. * 数据转图片
  67. * @param $image
  68. * @param $printTemplate
  69. * @param null $order_package
  70. * @return mixed
  71. */
  72. public function convertPrintImage($image,$printTemplate,$order_package = null)
  73. {
  74. $items = $printTemplate->value;
  75. $bg = array_filter($items,function ($item){
  76. return $item['type'] == 'bg';
  77. });
  78. $items = array_filter($items,function($item){
  79. return $item['type'] !== 'bg';
  80. });
  81. usort($items,function ($a, $b) {
  82. return $a['z_index'] > $b['z_index'] ? 1 : 0;
  83. });
  84. return $image;
  85. }
  86. }