ExportService.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. <?php
  2. namespace App\Services\common;
  3. use Exception;
  4. use Illuminate\Http\Response;
  5. use Illuminate\Support\Facades\Http;
  6. use App\Traits\ServiceAppAop;
  7. class ExportService
  8. {
  9. use ServiceAppAop;
  10. protected $modelClass=Export::class;
  11. /**
  12. * @param array $row 表头 一维数组
  13. * @param array $list 表体 二维数组
  14. * @param string $fileName 导出Excel文件名
  15. * @param string $createFormat 导出数据格式化方式,默认非格式化
  16. * @param array $mergeColumn 需要合并时 合并列 一维数组
  17. * @param array $mergeRow 需要合并时 合并行 key-val数组 key为合并起点val为终点 例:["1"=>"4"] 第一行到第四行合并为一列
  18. * @return Response
  19. * @throws Exception 请求接口返回异常
  20. */
  21. public function json(array $row, array $list, string $fileName = '记录',
  22. string $createFormat = null, array $mergeColumn = [], array $mergeRow = []) :Response
  23. {
  24. $request['type'] = 'base';
  25. $data = ['row'=>$row,'list'=>$list];
  26. if ($createFormat){
  27. $data["mergeColumn"] = $mergeColumn;
  28. if($mergeRow)$data["mergeRow"] = $mergeRow;
  29. $request["createFormat"] = "merge";
  30. }
  31. $request["data"] = json_encode($data,JSON_UNESCAPED_UNICODE);
  32. $post=Http::post(config('go.export.url'),$request);
  33. if ($post->status() == 500){
  34. throw new Exception($post->header("Msg"));
  35. }
  36. return response($post,200, [
  37. "Content-type"=>"application/octet-stream",
  38. "Content-Disposition"=>"attachment; filename=".$fileName."-".date('ymdHis').'.xlsx',
  39. ]);
  40. }
  41. }