diff --git a/app/common/controller/UsedCarController.php b/app/common/controller/UsedCarController.php index b0b1df0..cade0db 100644 --- a/app/common/controller/UsedCarController.php +++ b/app/common/controller/UsedCarController.php @@ -7,6 +7,7 @@ use app\common\service\OssService; use app\common\traits\crud\CrudControllerTrait; use app\common\service\UsedCarService; use app\common\utils\pdf\service\MpdfDriver; +use support\exception\BusinessException; use support\Request; class UsedCarController extends BaseController @@ -134,7 +135,7 @@ class UsedCarController extends BaseController array_map('unlink', glob(runtime_path('pdf/attachment/*.pdf'))); return $this->success($pdf); } catch (\Throwable $e) { - return $this->error($e->getMessage().$e->getLine().$e->getFile()); + return $this->error($e->getMessage()); } } @@ -151,4 +152,40 @@ class UsedCarController extends BaseController $raw['product_name'].'-'.$raw['id'].'.pdf' // 下载时显示的文件名 ); } + + public function getPdf(Request $request) + { + try { + $raw = $this->service->serviceShow($request); + $filePath = runtime_path('pdf') . '/' . $raw['pdf_src']; + + if (empty($raw['pdf_src']) || !file_exists($filePath)) { + throw new BusinessException('文件不存在'); + } + + // 确保 public/pdf 存在 + $publicDir = public_path('pdf'); + if (!is_dir($publicDir)) { + mkdir($publicDir, 0755, true); + } + + // 拷贝到 public 目录 + $publicFile = $publicDir . '/' . $raw['pdf_src']; + if (!file_exists($publicFile)) { + copy($filePath, $publicFile); + } + + // 返回访问地址 + $baseUrl = 'https://' . request()->header('host'); + $url = $baseUrl . '/pdf/' . $raw['pdf_src']; + + return $this->success([ + 'pdf_url' => $url, + 'file_name' => $raw['product_name'] . '-' . $raw['id'] . '.pdf' + ]); + } catch (\Throwable $e) { + return $this->error($e->getMessage()); + } + } + } \ No newline at end of file diff --git a/app/process/Task.php b/app/process/Task.php index 5acd138..7a88f05 100644 --- a/app/process/Task.php +++ b/app/process/Task.php @@ -21,6 +21,7 @@ class Task ->update(['pdf_src' => '']); array_map('unlink', glob(runtime_path('pdf/*.pdf'))); + array_map('unlink', glob(public_path('pdf/*.pdf'))); Log::info('send follow end',[time()]); }); diff --git a/config/route.php b/config/route.php index f0503a8..c927730 100644 --- a/config/route.php +++ b/config/route.php @@ -59,6 +59,7 @@ Route::group('/common', function () { Route::any('/list', [app\common\controller\UsedCarController::class, 'index']); Route::any('/produce/pdf', [app\common\controller\UsedCarController::class, 'producePdf']); Route::any('/download/pdf', [app\common\controller\UsedCarController::class, 'downloadPdf']); + Route::any('/pdf', [app\common\controller\UsedCarController::class, 'getPdf']); Route::any('/show', [app\common\controller\UsedCarController::class, 'show']); Route::any('/store', [app\common\controller\UsedCarController::class, 'store']); Route::any('/update', [app\common\controller\UsedCarController::class, 'update']);