You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
<?php
namespace App\Jobs;
use App\Imports\ProductImport;use Exception;use Illuminate\Bus\Queueable;use Illuminate\Contracts\Queue\ShouldBeUnique;use Illuminate\Contracts\Queue\ShouldQueue;use Illuminate\Foundation\Bus\Dispatchable;use Illuminate\Queue\InteractsWithQueue;use Illuminate\Queue\SerializesModels;use Illuminate\Support\Facades\Storage;use Maatwebsite\Excel\Facades\Excel;use ZipArchive;
class ImportProductFromExcel implements ShouldQueue{ use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
private int $supplier_id; private string $zip_file;
/** * Create a new job instance. * * @return void */ public function __construct(int $supplier_id, string $zip_file) { $this->supplier_id = $supplier_id; $this->zip_file = $zip_file; }
/** * Execute the job. * * @return void * @throws Exception */ public function handle() { if (!$this->supplier_id) { throw new Exception('未指定要导入的供应商'); } else if (!$this->zip_file) { throw new Exception('未指定要导入的zip文件'); }
$extract_dir = "excel/extract/{$this->supplier_id}/"; Storage::deleteDirectory($extract_dir); Storage::makeDirectory($extract_dir); $extract_path = Storage::path($extract_dir . basename($this->zip_file, '.zip'));
$zip = new ZipArchive; if ($zip->open($this->zip_file) === TRUE && $zip->extractTo($extract_path)) { $zip->close(); } else { throw new Exception("解压文件 {$this->zip_file} 失败!"); }
# 解压后删除压缩文件
unlink($this->zip_file);
Excel::import(new ProductImport($this->supplier_id, $extract_path), $extract_path . '/产品.xlsx'); }}
|