导入
This commit is contained in:
@@ -3,16 +3,25 @@ namespace app\admin\controller;
|
||||
|
||||
use app\common\controller\BaseController;
|
||||
use app\common\model\PostCategory;
|
||||
use app\common\model\Sku;
|
||||
use app\common\model\SkuAttribute;
|
||||
use app\common\service\OssService;
|
||||
use app\common\traits\crud\CrudControllerTrait;
|
||||
use app\common\service\SkuService;
|
||||
use PhpOffice\PhpSpreadsheet\IOFactory;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
|
||||
use support\Db;
|
||||
use support\Request;
|
||||
use support\Response;
|
||||
|
||||
class SkuController extends BaseController
|
||||
{
|
||||
use CrudControllerTrait;
|
||||
|
||||
protected $noNeedLogin = ['template'];
|
||||
|
||||
protected $noNeedHeaderData = ['template'];
|
||||
|
||||
protected $service;
|
||||
|
||||
public function __construct()
|
||||
@@ -74,4 +83,140 @@ class SkuController extends BaseController
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
|
||||
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);
|
||||
|
||||
$spreadsheet = IOFactory::load($path);
|
||||
$sheet = $spreadsheet->getActiveSheet();
|
||||
|
||||
// 提取图片(按行号)
|
||||
// $rowImages = $this->extractImages($sheet);
|
||||
|
||||
$highestRow = $sheet->getHighestRow();
|
||||
$highestCol = $sheet->getHighestColumn();
|
||||
|
||||
$header = $sheet->rangeToArray("A1:{$highestCol}1")[0];
|
||||
$header = array_map('trim', $header);
|
||||
|
||||
$allowFields = [
|
||||
'product_name' => '产品名称',
|
||||
'model' => '产品型号',
|
||||
'brand' => '品牌',
|
||||
'category_id' => '分类',
|
||||
'price' => '价格',
|
||||
// 'spec_json' => '商品属性'
|
||||
];
|
||||
$fieldsName = array_flip($allowFields);
|
||||
|
||||
$insertData = [];
|
||||
$now = time();
|
||||
|
||||
Db::beginTransaction();
|
||||
try {
|
||||
for ($row = 2; $row <= $highestRow; $row++) {
|
||||
$rowData = $sheet->rangeToArray(
|
||||
"A{$row}:{$highestCol}{$row}", null, true, false
|
||||
)[0];
|
||||
|
||||
$data = [];
|
||||
foreach ($header as $i => $field) {
|
||||
if (in_array($field, $allowFields)) {
|
||||
$data[$fieldsName[$field]] = trim((string)($rowData[$i] ?? ''));
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($data['product_name'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$data['category_id'] = PostCategory::where('name', $rowData[3])->value('id')??0;
|
||||
// if(!empty($rowData[5])){
|
||||
// $desci = explode(';', $rowData[5]);
|
||||
// $desc = [];
|
||||
// foreach ($desci as $k => $v){
|
||||
// $item = explode(':', $v);
|
||||
// $desc[$item[0]] = $item[1];
|
||||
// $data['desc'] = json_encode($desc);
|
||||
// }
|
||||
// }
|
||||
|
||||
$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['created_at'] = $now;
|
||||
$data['updated_at'] = $now;
|
||||
|
||||
$insertData[] = $data;
|
||||
|
||||
if (count($insertData) >= 200) {
|
||||
Sku::insert($insertData);
|
||||
// Db::table('sku')->insert($insertData);
|
||||
$insertData = [];
|
||||
}
|
||||
}
|
||||
|
||||
if ($insertData) {
|
||||
Sku::insert($insertData);
|
||||
// Db::table('sku')->insert($insertData);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
} catch (\Throwable $e) {
|
||||
Db::rollBack();
|
||||
return $this->error($e->getMessage());
|
||||
} finally {
|
||||
@unlink($path);
|
||||
}
|
||||
|
||||
return $this->success([], '导入成功');
|
||||
}
|
||||
|
||||
private function extractImages($sheet): array
|
||||
{
|
||||
$images = [];
|
||||
foreach ($sheet->getDrawingCollection() as $drawing) {
|
||||
if ($drawing instanceof Drawing) {
|
||||
preg_match('/\d+/', $drawing->getCoordinates(), $m);
|
||||
$row = (int)$m[0];
|
||||
|
||||
$dir = runtime_path('upload/images/');
|
||||
if (!is_dir($dir)) mkdir($dir, 0755, true);
|
||||
|
||||
$file = uniqid() . '.' . $drawing->getExtension();
|
||||
copy($drawing->getPath(), $dir . $file);
|
||||
|
||||
$images[$row] = '/upload/images/' . $file;
|
||||
}
|
||||
}
|
||||
return $images;
|
||||
}
|
||||
|
||||
/**
|
||||
* 下载 Excel 示例模板
|
||||
*/
|
||||
public function template(Request $request): Response
|
||||
{
|
||||
$filePath = runtime_path() . '\upload\import_post.xlsx';
|
||||
var_dump($filePath);
|
||||
|
||||
if (!file_exists($filePath)) {
|
||||
return response('文件不存在', 404);
|
||||
}
|
||||
|
||||
return response()->download(
|
||||
$filePath,
|
||||
'导入示例.xlsx' // 下载时显示的文件名
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user