From 58f553586c33f55a3d9f41dfe0dd3c6e98cc5f13 Mon Sep 17 00:00:00 2001 From: liufumingren Date: Thu, 22 Oct 2026 21:04:47 +0800 Subject: [PATCH] =?UTF-8?q?fix(wms):=20split=5Fexcel=20=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=8B=86=E5=8D=95=E6=8E=A5=E5=8F=A3=E5=BC=82=E5=B8=B8=E6=97=B6?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=9C=9F=E5=AE=9E=E5=8E=9F=E5=9B=A0=E5=B9=B6?= =?UTF-8?q?=E5=8A=A0=E5=9B=BA=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 现象: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> --- application/index/controller/wms/Wms.php | 27 +++++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/application/index/controller/wms/Wms.php b/application/index/controller/wms/Wms.php index 5c0f2f8..81006a6 100644 --- a/application/index/controller/wms/Wms.php +++ b/application/index/controller/wms/Wms.php @@ -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;$iextractLastNumber($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();