diff --git a/app/admin/controller/SkuController.php b/app/admin/controller/SkuController.php index 6c16339..4f6aa36 100644 --- a/app/admin/controller/SkuController.php +++ b/app/admin/controller/SkuController.php @@ -3,10 +3,17 @@ 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\service\UploadService; use app\common\traits\crud\CrudControllerTrait; use app\common\service\SkuService; +use PhpOffice\PhpSpreadsheet\IOFactory; +use support\Db; use support\Request; +use support\Response; +use ZipArchive; class SkuController extends BaseController { @@ -57,4 +64,169 @@ class SkuController extends BaseController } return $data; } + + + + public function import(Request $request) + { +// ini_set('memory_limit', '512M'); + $file = $request->file('file'); + if (!$file) return $this->error('未上传文件');; + + $zipPath = runtime_path('upload/' . uniqid() . '.zip'); + $file->move($zipPath); + + $extractTo = runtime_path('upload/import/'); + + clearstatcache(true, $zipPath); + $this->unzipPackage($zipPath,$extractTo); + + $path = runtime_path('upload/import/product.xlsx'); + $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); +// $attributeFields = [ +// '外观' => '外观', +// '产品尺寸(宽*高*深)' => '产品尺寸(宽*高*深)', +// '开孔尺寸(宽*高*深)' => '开孔尺寸(宽*高*深)', +// '容积' => '容积', +// '产品卖点' => '产品卖点', +// ]; +// $attributeFieldsName = array_flip($attributeFields); + + $insertData = []; + $now = time(); + + Db::beginTransaction(); + $uploadService = new UploadService(); + try { + for ($row = 2; $row <= $highestRow; $row++) { + $rowData = $sheet->rangeToArray( + "A{$row}:{$highestCol}{$row}", null, true, false + )[0]; + + $data = []; + $attributes = []; + foreach ($header as $i => $field) { + if (in_array($field, $allowFields)) { + $data[$fieldsName[$field]] = trim((string)($rowData[$i] ?? '')); + } + + //商品属性 +// if (in_array($field, $attributeFields) && !empty($rowData[$i])) { +// $attributes[$attributeFieldsName[$field]] = trim((string)($rowData[$i] ?? '')); +// } + } + + if (empty($data['product_name'])) { + continue; + } + + $data['category_id'] = PostCategory::where('name', $rowData[4])->value('id')??0; + + +// $sku = Sku::where('model', $data['model'])->first(); +// $sku->spec_json = $data['spec_json']; +// $sku->save(); + + $data['code'] = $this->service->generateUniqueCode(); + $data['brand'] = $data['brand'] ?: '西门子'; + $data['price'] = $data['price'] ?: 0; + $data['status'] = $data['status'] ?? 1; + + // 图片 + //$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 = []; + } + } + @unlink($zipPath); + + if ($insertData) { + Sku::insert($insertData); +// Db::table('sku')->insert($insertData); + } + + Db::commit(); + } catch (\Throwable $e) { + Db::rollBack(); + return $this->error($e->getMessage().$e->getLine().$e->getFile()); + } finally { + @unlink($path); + } + + 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; + } + + + /** + * 下载 Excel 示例模板 + */ + public function template(Request $request): Response + { + $filePath = runtime_path() . '/upload/import.zip'; + + if (!file_exists($filePath)) { + return response('文件不存在', 404); + } + + return response()->download( + $filePath, + '导入示例.zip' // 下载时显示的文件名 + ); + } } \ No newline at end of file diff --git a/app/admin/service/UserService.php b/app/admin/service/UserService.php index a90ff10..cd98d10 100644 --- a/app/admin/service/UserService.php +++ b/app/admin/service/UserService.php @@ -89,7 +89,9 @@ class UserService // 关键字 if (isset($p['keyword']) && $p['keyword']) { $k = '%' . $p['keyword'] . '%'; - $config['where']['name'] = ['like', $k]; + foreach (['name', 'mobile', 'sn'] as $col) { + $config['orWhere'][] = [$col, 'like', $k]; + } } if (isset($p['status']) && $p['status']) { $config['where']['status'] = $p['status']; diff --git a/composer.json b/composer.json index 37f2ab7..0de725f 100644 --- a/composer.json +++ b/composer.json @@ -40,7 +40,8 @@ "firebase/php-jwt": "^6.11", "webman/cache": "^2.1", "shopwwi/webman-filesystem": "1.1.0", - "shopwwi/filesystem-oss": "^1.0" + "shopwwi/filesystem-oss": "^1.0", + "phpoffice/phpspreadsheet": "5.3" }, "suggest": { "ext-event": "For better performance. " diff --git a/composer.lock b/composer.lock index a640ebc..ac9e4a9 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cfcedd2ce8245f797ac93d1eb374abf6", + "content-hash": "afdbc6f2862a2ea64961bd7406fa5698", "packages": [ { "name": "aliyuncs/oss-sdk-php", @@ -180,6 +180,65 @@ ], "time": "2024-02-09T16:56:22+00:00" }, + { + "name": "composer/pcre", + "version": "3.3.2", + "dist": { + "type": "zip", + "url": "https://mirrors.cloud.tencent.com/repository/composer/composer/pcre/3.3.2/composer-pcre-3.3.2.zip", + "reference": "b2bed4734f0cc156ee1fe9c0da2550420d99a21e", + "shasum": "" + }, + "require": { + "php": "^7.4 || ^8.0" + }, + "conflict": { + "phpstan/phpstan": "<1.11.10" + }, + "require-dev": { + "phpstan/phpstan": "^1.12 || ^2", + "phpstan/phpstan-strict-rules": "^1 || ^2", + "phpunit/phpunit": "^8 || ^9" + }, + "type": "library", + "extra": { + "phpstan": { + "includes": [ + "extension.neon" + ] + }, + "branch-alias": { + "dev-main": "3.x-dev" + } + }, + "autoload": { + "psr-4": { + "Composer\\Pcre\\": "src" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "PCRE wrapping library that offers type-safe preg_* replacements.", + "keywords": [ + "PCRE", + "preg", + "regex", + "regular expression" + ], + "support": { + "issues": "https://github.com/composer/pcre/issues", + "source": "https://github.com/composer/pcre/tree/3.3.2" + }, + "time": "2024-11-12T16:29:46+00:00" + }, { "name": "doctrine/inflector", "version": "2.1.0", @@ -1986,6 +2045,167 @@ ], "time": "2024-09-21T08:32:55+00:00" }, + { + "name": "maennchen/zipstream-php", + "version": "3.1.2", + "dist": { + "type": "zip", + "url": "https://mirrors.cloud.tencent.com/repository/composer/maennchen/zipstream-php/3.1.2/maennchen-zipstream-php-3.1.2.zip", + "reference": "aeadcf5c412332eb426c0f9b4485f6accba2a99f", + "shasum": "" + }, + "require": { + "ext-mbstring": "*", + "ext-zlib": "*", + "php-64bit": "^8.2" + }, + "require-dev": { + "brianium/paratest": "^7.7", + "ext-zip": "*", + "friendsofphp/php-cs-fixer": "^3.16", + "guzzlehttp/guzzle": "^7.5", + "mikey179/vfsstream": "^1.6", + "php-coveralls/php-coveralls": "^2.5", + "phpunit/phpunit": "^11.0", + "vimeo/psalm": "^6.0" + }, + "suggest": { + "guzzlehttp/psr7": "^2.4", + "psr/http-message": "^2.0" + }, + "type": "library", + "autoload": { + "psr-4": { + "ZipStream\\": "src/" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Paul Duncan", + "email": "pabs@pablotron.org" + }, + { + "name": "Jonatan Männchen", + "email": "jonatan@maennchen.ch" + }, + { + "name": "Jesse Donat", + "email": "donatj@gmail.com" + }, + { + "name": "András Kolesár", + "email": "kolesar@kolesar.hu" + } + ], + "description": "ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.", + "keywords": [ + "stream", + "zip" + ], + "support": { + "issues": "https://github.com/maennchen/ZipStream-PHP/issues", + "source": "https://github.com/maennchen/ZipStream-PHP/tree/3.1.2" + }, + "time": "2025-01-27T12:07:53+00:00" + }, + { + "name": "markbaker/complex", + "version": "3.0.2", + "dist": { + "type": "zip", + "url": "https://mirrors.cloud.tencent.com/repository/composer/markbaker/complex/3.0.2/markbaker-complex-3.0.2.zip", + "reference": "95c56caa1cf5c766ad6d65b6344b807c1e8405b9", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-master", + "phpcompatibility/php-compatibility": "^9.3", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Complex\\": "classes/src/" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Baker", + "email": "mark@lange.demon.co.uk" + } + ], + "description": "PHP Class for working with complex numbers", + "homepage": "https://github.com/MarkBaker/PHPComplex", + "keywords": [ + "complex", + "mathematics" + ], + "support": { + "issues": "https://github.com/MarkBaker/PHPComplex/issues", + "source": "https://github.com/MarkBaker/PHPComplex/tree/3.0.2" + }, + "time": "2022-12-06T16:21:08+00:00" + }, + { + "name": "markbaker/matrix", + "version": "3.0.1", + "dist": { + "type": "zip", + "url": "https://mirrors.cloud.tencent.com/repository/composer/markbaker/matrix/3.0.1/markbaker-matrix-3.0.1.zip", + "reference": "728434227fe21be27ff6d86621a1b13107a2562c", + "shasum": "" + }, + "require": { + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-master", + "phpcompatibility/php-compatibility": "^9.3", + "phpdocumentor/phpdocumentor": "2.*", + "phploc/phploc": "^4.0", + "phpmd/phpmd": "2.*", + "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0", + "sebastian/phpcpd": "^4.0", + "squizlabs/php_codesniffer": "^3.7" + }, + "type": "library", + "autoload": { + "psr-4": { + "Matrix\\": "classes/src/" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Mark Baker", + "email": "mark@demon-angel.eu" + } + ], + "description": "PHP Class for working with matrices", + "homepage": "https://github.com/MarkBaker/PHPMatrix", + "keywords": [ + "mathematics", + "matrix", + "vector" + ], + "support": { + "issues": "https://github.com/MarkBaker/PHPMatrix/issues", + "source": "https://github.com/MarkBaker/PHPMatrix/tree/3.0.1" + }, + "time": "2022-12-02T22:17:43+00:00" + }, { "name": "monolog/monolog", "version": "2.10.0", @@ -2458,6 +2678,106 @@ ], "time": "2025-08-20T22:55:33+00:00" }, + { + "name": "phpoffice/phpspreadsheet", + "version": "5.3.0", + "dist": { + "type": "zip", + "url": "https://mirrors.cloud.tencent.com/repository/composer/phpoffice/phpspreadsheet/5.3.0/phpoffice-phpspreadsheet-5.3.0.zip", + "reference": "4d597c1aacdde1805a33c525b9758113ea0d90df", + "shasum": "" + }, + "require": { + "composer/pcre": "^1||^2||^3", + "ext-ctype": "*", + "ext-dom": "*", + "ext-fileinfo": "*", + "ext-gd": "*", + "ext-iconv": "*", + "ext-libxml": "*", + "ext-mbstring": "*", + "ext-simplexml": "*", + "ext-xml": "*", + "ext-xmlreader": "*", + "ext-xmlwriter": "*", + "ext-zip": "*", + "ext-zlib": "*", + "maennchen/zipstream-php": "^2.1 || ^3.0", + "markbaker/complex": "^3.0", + "markbaker/matrix": "^3.0", + "php": "^8.1", + "psr/http-client": "^1.0", + "psr/http-factory": "^1.0", + "psr/simple-cache": "^1.0 || ^2.0 || ^3.0" + }, + "require-dev": { + "dealerdirect/phpcodesniffer-composer-installer": "dev-main", + "dompdf/dompdf": "^2.0 || ^3.0", + "friendsofphp/php-cs-fixer": "^3.2", + "mitoteam/jpgraph": "^10.5", + "mpdf/mpdf": "^8.1.1", + "phpcompatibility/php-compatibility": "^9.3", + "phpstan/phpstan": "^1.1 || ^2.0", + "phpstan/phpstan-deprecation-rules": "^1.0 || ^2.0", + "phpstan/phpstan-phpunit": "^1.0 || ^2.0", + "phpunit/phpunit": "^10.5", + "squizlabs/php_codesniffer": "^3.7", + "tecnickcom/tcpdf": "^6.5" + }, + "suggest": { + "dompdf/dompdf": "Option for rendering PDF with PDF Writer", + "ext-intl": "PHP Internationalization Functions, required for NumberFormat Wizard", + "mitoteam/jpgraph": "Option for rendering charts, or including charts with PDF or HTML Writers", + "mpdf/mpdf": "Option for rendering PDF with PDF Writer", + "tecnickcom/tcpdf": "Option for rendering PDF with PDF Writer" + }, + "type": "library", + "autoload": { + "psr-4": { + "PhpOffice\\PhpSpreadsheet\\": "src/PhpSpreadsheet" + } + }, + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Maarten Balliauw", + "homepage": "https://blog.maartenballiauw.be" + }, + { + "name": "Mark Baker", + "homepage": "https://markbakeruk.net" + }, + { + "name": "Franck Lefevre", + "homepage": "https://rootslabs.net" + }, + { + "name": "Erik Tilt" + }, + { + "name": "Adrien Crivelli" + } + ], + "description": "PHPSpreadsheet - Read, Create and Write Spreadsheet documents in PHP - Spreadsheet engine", + "homepage": "https://github.com/PHPOffice/PhpSpreadsheet", + "keywords": [ + "OpenXML", + "excel", + "gnumeric", + "ods", + "php", + "spreadsheet", + "xls", + "xlsx" + ], + "support": { + "issues": "https://github.com/PHPOffice/PhpSpreadsheet/issues", + "source": "https://github.com/PHPOffice/PhpSpreadsheet/tree/5.3.0" + }, + "time": "2025-11-24T15:47:10+00:00" + }, { "name": "phpoption/phpoption", "version": "1.9.4", diff --git a/config/route.php b/config/route.php index e333380..1bbb829 100644 --- a/config/route.php +++ b/config/route.php @@ -232,6 +232,8 @@ Route::group('/admin', function () { Route::any('/store', [app\admin\controller\SkuController::class, 'store']); Route::any('/update', [app\admin\controller\SkuController::class, 'update']); Route::any('/destroy', [app\admin\controller\SkuController::class, 'destroy']); + Route::any('/import', [app\admin\controller\SkuController::class, 'import']); + Route::any('/template', [app\admin\controller\SkuController::class, 'template']); }); // 公告