压缩包导入
This commit is contained in:
@@ -10,10 +10,12 @@ use app\common\service\UploadService;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\common\service\SkuService;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpWord\IOFactory as WordIOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
||||
use support\Db;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
use ZipArchive;
|
||||
|
||||
class SkuController extends BaseController
|
||||
{
|
||||
@@ -87,17 +89,23 @@ class SkuController extends BaseController
|
||||
|
||||
public function import(Request $request)
|
||||
{
|
||||
|
||||
$file = $request->file('file');
|
||||
if (!$file) return json(['code'=>1,'msg'=>'未上传文件']);
|
||||
|
||||
$path = runtime_path('upload/' . uniqid() . '.xlsx');
|
||||
$file->move($path);
|
||||
$zipPath = runtime_path('upload/' . uniqid() . '.zip');
|
||||
$file->move($zipPath);
|
||||
|
||||
$extractTo = runtime_path('upload/import/' . uniqid());
|
||||
$this->unzipPackage($zipPath,$extractTo);
|
||||
@unlink($zipPath);
|
||||
|
||||
$path = runtime_path('upload/import/product.xlsx');
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 提取图片(按行号)
|
||||
$rowImages = $this->extractImages($sheet);
|
||||
//$rowImages = $this->extractImages($sheet);
|
||||
|
||||
$highestRow = $sheet->getHighestRow();
|
||||
$highestCol = $sheet->getHighestColumn();
|
||||
@@ -111,7 +119,7 @@ class SkuController extends BaseController
|
||||
'brand' => '品牌',
|
||||
'category_id' => '分类',
|
||||
'price' => '价格',
|
||||
// 'spec_json' => '商品属性'
|
||||
//'spec_json' => '商品属性'
|
||||
];
|
||||
$fieldsName = array_flip($allowFields);
|
||||
|
||||
@@ -119,6 +127,7 @@ class SkuController extends BaseController
|
||||
$now = time();
|
||||
|
||||
Db::beginTransaction();
|
||||
$uploadService = new UploadService();
|
||||
try {
|
||||
for ($row = 2; $row <= $highestRow; $row++) {
|
||||
$rowData = $sheet->rangeToArray(
|
||||
@@ -137,6 +146,8 @@ class SkuController extends BaseController
|
||||
}
|
||||
|
||||
$data['category_id'] = PostCategory::where('name', $rowData[3])->value('id')??0;
|
||||
|
||||
//商品属性
|
||||
if(!empty($rowData[5])){
|
||||
$speci = explode(';', $rowData[5]);
|
||||
$spec = [];
|
||||
@@ -146,31 +157,43 @@ class SkuController extends BaseController
|
||||
continue;
|
||||
}
|
||||
$attr = ['attr_name'=>$item[0],'attr_value'=>$item[1]];
|
||||
$skuAttribute = SkuAttribute::where('attr_name', $item[0])->first();
|
||||
$skuAttribute = SkuAttribute::where('attr_name', $item[0])
|
||||
->where('category_id', $data['category_id'])->first();
|
||||
if (empty($skuAttribute)) {
|
||||
continue;
|
||||
}
|
||||
$attr['attr_id'] = $skuAttribute->id;
|
||||
$attr['category_id'] = $skuAttribute->category_id;
|
||||
$attr['category_id'] = $data['category_id'];
|
||||
$spec[] = $attr;
|
||||
$data['spec_json'] = json_encode($spec);
|
||||
}
|
||||
}
|
||||
|
||||
//主图
|
||||
if(!empty($rowData[6])){
|
||||
$tmpPath = runtime_path('upload/import/main_pic/'.$rowData[6]);
|
||||
$res = $uploadService->uploadLocalFile(['path' => $tmpPath, 'scene' => 'sku']);
|
||||
$data['main_image'] = $res['file_url'];
|
||||
}
|
||||
//详情
|
||||
if(!empty($rowData[7])){
|
||||
$docxFullPath = runtime_path('upload/import/docs/'.$rowData[7]);
|
||||
$data['desc'] =$this->docxToHtmlWithImages($docxFullPath);
|
||||
}
|
||||
|
||||
$data['code'] = $this->service->generateUniqueCode();
|
||||
$data['brand'] = $data['brand'] ?: '西门子';
|
||||
$data['price'] = $data['price'] ?: 0;
|
||||
$data['status'] = $data['status'] ?? 2;
|
||||
|
||||
// 图片
|
||||
$data['main_image'] = $rowImages[$row] ?? null;
|
||||
//$data['main_image'] = $rowImages[$row] ?? null;
|
||||
|
||||
$data['created_at'] = $now;
|
||||
$data['updated_at'] = $now;
|
||||
|
||||
$insertData[] = $data;
|
||||
|
||||
var_dump($insertData);
|
||||
if (count($insertData) >= 200) {
|
||||
Sku::insert($insertData);
|
||||
// Db::table('sku')->insert($insertData);
|
||||
@@ -194,6 +217,88 @@ class SkuController extends BaseController
|
||||
return $this->success([], '导入成功');
|
||||
}
|
||||
|
||||
/**
|
||||
* 解压 zip 文件
|
||||
*
|
||||
* @param string $zipPath zip 文件绝对路径
|
||||
* @param string $extractTo 解压目录
|
||||
* @return bool
|
||||
*/
|
||||
function unzipPackage(string $zipPath, string $extractTo): bool
|
||||
{
|
||||
if (!file_exists($zipPath)) {
|
||||
throw new \Exception('ZIP 文件不存在');
|
||||
}
|
||||
|
||||
if (!is_dir($extractTo)) {
|
||||
mkdir($extractTo, 0755, true);
|
||||
}
|
||||
|
||||
$zip = new ZipArchive();
|
||||
if ($zip->open($zipPath) !== true) {
|
||||
throw new \Exception('ZIP 文件打开失败');
|
||||
}
|
||||
|
||||
$zip->extractTo($extractTo);
|
||||
$zip->close();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* docx 转 HTML(自动上传图片到 OSS)
|
||||
*
|
||||
* @param string $docxPath docx 文件绝对路径
|
||||
* @return string HTML
|
||||
*/
|
||||
function docxToHtmlWithImages(string $docxPath): string
|
||||
{
|
||||
if (!is_file($docxPath)) {
|
||||
return '';
|
||||
}
|
||||
|
||||
// 1. 读取 docx
|
||||
$phpWord = WordIOFactory::load($docxPath);
|
||||
|
||||
// 2. 输出 HTML 到临时文件
|
||||
$tmpHtml = runtime_path('tmp/' . uniqid() . '.html');
|
||||
if (!is_dir(dirname($tmpHtml))) {
|
||||
mkdir(dirname($tmpHtml), 0755, true);
|
||||
}
|
||||
|
||||
$writer = WordIOFactory::createWriter($phpWord, 'HTML');
|
||||
$writer->save($tmpHtml);
|
||||
|
||||
$html = file_get_contents($tmpHtml);
|
||||
|
||||
$uploadService = new UploadService();
|
||||
// 3. 处理 HTML 中的图片
|
||||
$html = preg_replace_callback(
|
||||
'/<img[^>]+src="([^"]+)"[^>]*>/i',
|
||||
function ($matches) use ($tmpHtml,$uploadService) {
|
||||
$src = $matches[1];
|
||||
|
||||
// docx 导出的图片一般是相对路径
|
||||
if (!str_starts_with($src, 'http')) {
|
||||
$localImg = dirname($tmpHtml) . '/' . $src;
|
||||
if (is_file($localImg)) {
|
||||
// 上传到 OSS
|
||||
$ossUrl = $uploadService->uploadLocalFile(['path' => $localImg, 'scene' => 'desc']);
|
||||
return str_replace($src, $ossUrl, $matches[0]);
|
||||
}
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
},
|
||||
$html
|
||||
);
|
||||
|
||||
// 清理临时文件
|
||||
@unlink($tmpHtml);
|
||||
|
||||
return $html;
|
||||
}
|
||||
|
||||
private function extractImages($sheet): array
|
||||
{
|
||||
$images = [];
|
||||
|
||||
Reference in New Issue
Block a user