71 lines
1.8 KiB
PHP
71 lines
1.8 KiB
PHP
<?php
|
|
|
|
namespace App\Admin\Imports;
|
|
|
|
use App\Exceptions\ImportException;
|
|
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
|
use Illuminate\Http\File;
|
|
use Illuminate\Support\Facades\Storage;
|
|
use Throwable;
|
|
|
|
class Import
|
|
{
|
|
public function readFileByUrl($url)
|
|
{
|
|
$file_name = substr($url, strrpos($url, '/')+1);
|
|
|
|
$path = DIRECTORY_SEPARATOR.'import'.DIRECTORY_SEPARATOR.date('Y-m-d').DIRECTORY_SEPARATOR.'import'.$file_name;
|
|
Storage::put($path, file_get_contents($url));
|
|
$file = new File(storage_path('app'.$path));
|
|
return $this->readFile($file);
|
|
}
|
|
|
|
public function readFile($file)
|
|
{
|
|
$reader = ReaderEntityFactory::createXLSXReader();
|
|
$reader->open($file);
|
|
|
|
$success = 0;
|
|
$fails = 0;
|
|
$errors = [];
|
|
|
|
foreach ($reader->getSheetIterator() as $sheet) {
|
|
foreach ($sheet->getRowIterator() as $num => $row) {
|
|
if ($num === 1) {
|
|
continue;
|
|
}
|
|
try {
|
|
$this->loadRow($row);
|
|
$success++;
|
|
} catch (ImportException $e) {
|
|
$fails++;
|
|
$errors[] = [
|
|
'row'=>$num,
|
|
'reason'=>$e->getMessage(),
|
|
];
|
|
} catch (Throwable $e) {
|
|
$fails++;
|
|
$errors[] = [
|
|
'row'=>$num,
|
|
'reason'=>$e->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
$reader->close();
|
|
return [
|
|
'success'=>$success,
|
|
'fails'=>$fails,
|
|
'errors'=>$errors,
|
|
];
|
|
}
|
|
|
|
public function loadRow($row)
|
|
{
|
|
return;
|
|
}
|
|
}
|