fix(wms): split_excel 上传拆单接口异常时返回真实原因并加固解析
现象:POST /index/wms/wms/split_excel 返回 {"msg":{}}。
原因:catch(Exception) 里直接 json(["msg"=>\]),Exception 对象序列化为 {};
而 TP5 的 ErrorException 继承 think\Exception,说明 try 内抛的是 PHP
notice/warning(7.4 下 error_reporting 含 E_NOTICE 时全部转异常)。
加固点:
- Excel 行取列改为 (\['E'] ?? '')、B/C/D 同样兜底,避免表格列数不足时
"Undefined index: E" 中断
- 上传文件为空时返回明确提示(原先是调用 null->validate() 的致命错误)
- unlink 前判断文件存在
- 导出目录不存在时自动创建
- catch 改为 \Throwable,日志记录 message@file:line,响应返回真实错误信息
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This commit is contained in:
parent
3f28fa9423
commit
58f553586c
@ -7636,6 +7636,9 @@ class Wms extends Frontend
|
||||
try {
|
||||
|
||||
$file = request()->file('file'); // 从请求中获取上传的文件
|
||||
if(!$file){
|
||||
return json(['status'=>'error','msg'=>'未收到上传文件,请确认表单字段名为 file']);
|
||||
}
|
||||
$info = $file->validate(['size'=>15678000,'ext'=>'xls,xlsx'])->move( '../uploads');
|
||||
if($info){
|
||||
$filename = $info->getSaveName();
|
||||
@ -7644,19 +7647,22 @@ class Wms extends Frontend
|
||||
// 使用 PhpSpreadsheet 解析 Excel 文件
|
||||
$spreadsheet = IOFactory::load($filePath);
|
||||
$sheetData = $spreadsheet->getActiveSheet()->toArray(null, true, true, true);
|
||||
for($i=2;$i<count($sheetData);$i++){
|
||||
$item = $sheetData[$i];
|
||||
$goods = explode(",",$item['E']);
|
||||
$rowCount = count($sheetData);
|
||||
for($i=2;$i<$rowCount;$i++){
|
||||
$item = $sheetData[$i] ?? [];
|
||||
$goods = explode(",",$item['E'] ?? '');
|
||||
$goods = array_filter($goods, 'strlen');
|
||||
foreach ($goods as $r){
|
||||
$matches = $this->extractLastNumber($r);
|
||||
//print_r($matches);die();
|
||||
array_push($list,[$item['B'],$item['C'],$item['D'],isset($matches[0])?$matches[0]:'',isset($matches[1])?$matches[1]:'']);
|
||||
array_push($list,[$item['B'] ?? '',$item['C'] ?? '',$item['D'] ?? '',isset($matches[0])?$matches[0]:'',isset($matches[1])?$matches[1]:'']);
|
||||
}
|
||||
|
||||
}
|
||||
// 删除上传的临时文件
|
||||
unlink($filePath);
|
||||
if(is_file($filePath)){
|
||||
unlink($filePath);
|
||||
}
|
||||
$spreadsheet = new Spreadsheet();
|
||||
$worksheet = $spreadsheet->getActiveSheet();
|
||||
$title = '导出数据';
|
||||
@ -7671,7 +7677,11 @@ class Wms extends Frontend
|
||||
// 将秒数转换为毫秒并拼接微秒数
|
||||
$millisecondTimestamp = (int)($sec * 1000) + (int)($usec * 1000);
|
||||
|
||||
$filePath = "{$rootpath}/../../../public/export/{$millisecondTimestamp}.xlsx";
|
||||
$exportDir = "{$rootpath}/../../../public/export";
|
||||
if(!is_dir($exportDir)){
|
||||
mkdir($exportDir, 0755, true);
|
||||
}
|
||||
$filePath = "{$exportDir}/{$millisecondTimestamp}.xlsx";
|
||||
$url = "https://ye4s.wms.gztbyc.com/export/{$millisecondTimestamp}.xlsx";
|
||||
$write = IOFactory::createWriter($spreadsheet, 'Xlsx');
|
||||
$res = $write->save($filePath);
|
||||
@ -7679,8 +7689,9 @@ class Wms extends Frontend
|
||||
} else {
|
||||
return json(['status' => 'error', 'message' => $file->getError()]);
|
||||
}
|
||||
} catch (Exception $e) {
|
||||
return json(["msg"=>$e]);
|
||||
} catch (\Throwable $e) {
|
||||
\think\Log::error('[split_excel] ' . $e->getMessage() . ' @ ' . $e->getFile() . ':' . $e->getLine());
|
||||
return json(['status' => 'error', 'msg' => $e->getMessage(), 'at' => $e->getFile() . ':' . $e->getLine()]);
|
||||
}
|
||||
}
|
||||
return $this->view->fetch();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user