作者 lyh

变更数据

@@ -69,24 +69,56 @@ class LyhImportTest extends Command @@ -69,24 +69,56 @@ class LyhImportTest extends Command
69 * @method :post 69 * @method :post
70 * @time :2025/10/22 16:47 70 * @time :2025/10/22 16:47
71 */ 71 */
  72 + use PhpOffice\PhpSpreadsheet\IOFactory;
  73 + use Illuminate\Support\Facades\DB;
  74 +
72 public function download_1517_action($url) 75 public function download_1517_action($url)
73 { 76 {
74 - // 下载到 Laravel storage 的临时路径  
75 - $tempPath = storage_path('app/temp_url.xlsx');  
76 - file_put_contents($tempPath, file_get_contents($url));  
77 - // 载入 Excel 77 + // 临时文件路径(确保目录存在)
  78 + $tempDir = storage_path('app');
  79 + if (!is_dir($tempDir)) {
  80 + mkdir($tempDir, 0755, true);
  81 + }
  82 + $tempPath = $tempDir . '/temp_url.xlsx';
78 try { 83 try {
  84 + // 使用 cURL 下载文件,防止403问题
  85 + $ch = curl_init($url);
  86 + curl_setopt_array($ch, [
  87 + CURLOPT_RETURNTRANSFER => true,
  88 + CURLOPT_FOLLOWLOCATION => true, // 跟随重定向
  89 + CURLOPT_USERAGENT => 'Mozilla/5.0 (compatible; LaravelApp/1.0)', // 模拟浏览器
  90 + CURLOPT_TIMEOUT => 30, // 超时防止卡死
  91 + ]);
  92 + $data = curl_exec($ch);
  93 + $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  94 + curl_close($ch);
  95 + // 检查下载是否成功
  96 + if ($httpCode !== 200 || !$data) {
  97 + echo "文件下载失败,HTTP状态码:{$httpCode}";
  98 + }
  99 + // 保存到临时文件
  100 + file_put_contents($tempPath, $data);
  101 + // 加载 Excel 文件
79 $spreadsheet = IOFactory::load($tempPath); 102 $spreadsheet = IOFactory::load($tempPath);
80 $sheet = $spreadsheet->getActiveSheet(); 103 $sheet = $spreadsheet->getActiveSheet();
81 $rows = $sheet->toArray(); 104 $rows = $sheet->toArray();
82 - unlink($tempPath); 105 + // 删除临时文件
  106 + if (file_exists($tempPath)) {
  107 + unlink($tempPath);
  108 + }
83 dd($rows); 109 dd($rows);
84 - }catch (\Exception $e){  
85 - echo '文件打不开'.PHP_EOL; 110 + } catch (\Exception $e) {
  111 + // 输出更清晰的错误信息
  112 + echo '文件下载或解析失败:' . $e->getMessage() . PHP_EOL;
  113 + // 清理资源
  114 + if (file_exists($tempPath)) {
  115 + unlink($tempPath);
  116 + }
86 DB::disconnect('custom_mysql'); 117 DB::disconnect('custom_mysql');
87 } 118 }
88 } 119 }
89 120
  121 +
90 /** 122 /**
91 * @remark :3951项目导入产品 123 * @remark :3951项目导入产品
92 * @name :import3951Product 124 * @name :import3951Product