1、excel 导出类中重构export方法,用一个自定义异常类用来替代exit 实现跳出执行到输出。
这里没法用return ,因为export方法返回会回到vendor方法中继续执行其他输出。
namespace App\Admin\Excel\Exporters;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\WithMapping;
class AppointOrdersExport extends ExcelExporter implements WithMapping
{
use Exportable;
protected $fileName = '购票记录.xlsx';
protected $columns = [
'order_sn'=>'订单号',
'pay_no'=>'支付流水号',
'order_num'=>'购票数量',
...
];
public function map($order) : array
{
$pay_types = ['0'=>'微信', '1'=>'小程序', '2'=>'支付宝'];
$statuss = [
'0'=>'待支付',
'1'=>'已支付未核销',
'2'=>'已支付',
'3'=>'已取消',
'-1'=>'已退款'
];
return [
$order->order_sn,
$order->pay_no,
$order->order_num,
.....
];
}
public function export()
{
throw new \App\Exceptions\SwooleExitException($this->download($this->fileName)->prepare(request()));
}
}
2、创建一个自定义异常类用来替代exit 实现跳出执行到输出
namespace App\Exceptions;
use Throwable;
use Exception;
use Illuminate\Http\Request;
class SwooleExitException extends Exception
{
protected $response;
public function __construct($response,$message = "", $code = 0, Throwable $previous = null)
{
$this->response = $response;
parent::__construct($message, $code, $previous);
}
public function render(Request $request) {
return $this->response->prepare($request);
}
}
3、把这个异常类放到 App\Exceptions\handle.php 的 $dontReport中,因为没必要记录这个异常
protected $dontReport = [
//
SwooleExitException::class,
];