refactor(xlsx): 优化多个报表的导出功能

- 新增创建导出目录的逻辑,确保目录存在
- 修改文件保存路径,使用动态创建的目录
- 优化 URL 生成逻辑,使其与实际文件路径一致
- 引入异常处理,当目录创建失败时抛出异常
This commit is contained in:
mkm 2025-01-01 10:47:19 +08:00
parent 66dbfd5908
commit 99730d82ad
11 changed files with 104 additions and 15 deletions

View File

@ -8,6 +8,8 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
class Beforehand
{
public function export($data, $system_store)
@ -183,8 +185,15 @@ class Beforehand
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . '/' . $order['info'].'出库单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir. '/' . $order['info'].'出库单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/'.date('Y-m'). '/' . $order['info'].'出库单-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单分配
@ -161,8 +162,15 @@ class OrderAllocation
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . '/' .'订单分配-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir. '/' .'订单分配-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/'.date('Y-m'). '/' .'订单分配-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
class OrderDetail
{
@ -148,8 +149,15 @@ class OrderDetail
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . '/' . $system_store.'出库单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' . $system_store.'出库单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/'.date('Y-m') . '/' . $system_store.'出库单-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 采购信息
@ -124,8 +125,15 @@ class OrderInfo
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$url = '/export/' . date('Y-m') . '/' .'采购信息-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .'采购信息-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/'.date('Y-m') . '/' .'采购信息-'.date('Y-m-d H:i') . '.xlsx';
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单清单
@ -159,8 +160,15 @@ class OrderList
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir. '/' .'供投里海农特产品下单清单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .'供投里海农特产品下单清单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单出库
@ -141,8 +142,15 @@ class OrderOutbound
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 财务订单出库
@ -143,8 +144,15 @@ class OrderOutboundFinance
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单出库
@ -149,8 +150,15 @@ class OrderSupplyOutbound
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .$title2.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单退供应商
@ -111,8 +112,15 @@ class ReturnSupplier
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .'供投里海农特产品退供应商结算单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .'供投里海农特产品退供应商结算单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
/**
* 订单退库
@ -127,8 +128,15 @@ class StockReturn
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .'供投里海农特产品退库结算单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .'供投里海农特产品退库结算单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;

View File

@ -6,6 +6,7 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\Style\Border;
use Webman\Exception\BusinessException;
class WarehouseOrdeRentry
{
@ -120,8 +121,15 @@ class WarehouseOrdeRentry
// 保存文件到 public 下
$writer = new Xlsx($spreadsheet);
$dir=public_path().'/export/'.date('Y-m');
if (!file_exists($dir)) {
// 尝试创建目录
if (!mkdir($dir)) {
throw new BusinessException('创建目录失败:/export/' . date('Y-m'));
}
}
$file_path = $dir . '/' .$this->company.'公司入库单-'.date('Y-m-d H:i') . '.xlsx';
$url = '/export/' . date('Y-m') . '/' .$this->company.'公司入库单-'.date('Y-m-d H:i') . '.xlsx';
$file_path = public_path() . $url;
// 保存文件到 public 下
$writer->save($file_path);
return getenv('APP_URL').$url;