store goods excel import
parent
3bac83d7ce
commit
1adcc15c7f
|
|
@ -7,7 +7,7 @@ APP_URL=http://localhost
|
|||
ENDPOINT_API_DOMAIN=null
|
||||
ENDPOINT_API_PATH=null
|
||||
|
||||
LOG_CHANNEL=stack
|
||||
LOG_CHANNEL=daily
|
||||
LOG_DEPRECATIONS_CHANNEL=null
|
||||
LOG_LEVEL=debug
|
||||
|
||||
|
|
@ -20,7 +20,7 @@ DB_PASSWORD=
|
|||
|
||||
BROADCAST_DRIVER=log
|
||||
CACHE_DRIVER=file
|
||||
FILESYSTEM_DRIVER=local
|
||||
FILESYSTEM_DRIVER=public
|
||||
QUEUE_CONNECTION=sync
|
||||
SESSION_DRIVER=file
|
||||
SESSION_LIFETIME=120
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ namespace App\Admin\Actions\Form;
|
|||
|
||||
use Dcat\Admin\Widgets\Form;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use App\Admin\Imports\Product;
|
||||
use App\Admin\Imports\{Product, StoreProduct};
|
||||
|
||||
class ProductImportForm extends Form
|
||||
{
|
||||
|
|
@ -13,12 +13,15 @@ class ProductImportForm extends Form
|
|||
try {
|
||||
DB::beginTransaction();
|
||||
$file = $input['file'];
|
||||
(new Product())->load($file);
|
||||
if (data_get($input, 'is_store')) {
|
||||
(new StoreProduct())->load($file);
|
||||
} else {
|
||||
(new Product())->load($file);
|
||||
}
|
||||
DB::commit();
|
||||
return $this->response()->success('数据导入成功')->refresh();
|
||||
} catch (\Exception $e) {
|
||||
DB::rollback();
|
||||
dd($e);
|
||||
return $this->response()->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
|
@ -29,5 +32,6 @@ class ProductImportForm extends Form
|
|||
->attribute('type', 'file')
|
||||
->attribute('accept', 'application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|
||||
->rules('required', ['required' => '文件不能为空']);
|
||||
$this->hidden('is_store');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Actions\Store;
|
||||
|
||||
use Dcat\Admin\Admin;
|
||||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class DownloadProductTemplate extends AbstractTool
|
||||
{
|
||||
protected function html()
|
||||
{
|
||||
$url = url('store-goods.xlsx');
|
||||
return <<<HTML
|
||||
<a href="{$url}" target="_blank" class="btn btn-white waves-effect">下载商品模板</a>
|
||||
HTML;
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Actions\Store;
|
||||
|
||||
use Dcat\Admin\Grid\Tools\AbstractTool;
|
||||
use App\Admin\Actions\Form\ProductImportForm;
|
||||
use Dcat\Admin\Widgets\Modal;
|
||||
|
||||
class ImportProduct extends AbstractTool
|
||||
{
|
||||
protected $title = '导入商品';
|
||||
public function render()
|
||||
{
|
||||
$form = ProductImportForm::make(['is_store' => 1]);
|
||||
return Modal::make()
|
||||
->lg()
|
||||
->title($this->title)
|
||||
->body($form)
|
||||
->button($this->html());
|
||||
}
|
||||
}
|
||||
|
|
@ -111,6 +111,7 @@ class ProductCategoryController extends AdminController
|
|||
$form->select('parent_id')->options(ProductCategoryModel::selectOptions());
|
||||
$form->text('name')->required();
|
||||
$form->image('icon')
|
||||
->uniqueName()
|
||||
->move('product-categories/'.Carbon::now()->toDateString())
|
||||
->saveFullUrl()
|
||||
->removable(false)
|
||||
|
|
|
|||
|
|
@ -74,6 +74,11 @@ class ProductController extends AdminController
|
|||
$filter->equal('productSku.category_id', '分类')->select(admin_route('api.product_categories', ['level' => 2]))->width(3);
|
||||
$filter->like('productSku.name', '名称')->width(3);
|
||||
});
|
||||
$grid->tools(function (Grid\Tools $tools) {
|
||||
$tools->append(new \App\Admin\Actions\Store\ImportProduct());
|
||||
$tools->append(new \App\Admin\Actions\Store\DownloadProductTemplate());
|
||||
});
|
||||
|
||||
$grid->footer(function ($collection) use ($grid) {
|
||||
$query = StoreProductSku::join('product_skus', 'product_skus.id', '=', 'store_product_skus.product_sku_id');
|
||||
$grid->model()->getQueries()->unique()->each(function ($value) use (&$query) {
|
||||
|
|
|
|||
|
|
@ -81,13 +81,6 @@ class StoreController extends AdminController
|
|||
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a show builder.
|
||||
*
|
||||
* @param mixed $id
|
||||
*
|
||||
* @return Show
|
||||
*/
|
||||
protected function detail($id)
|
||||
{
|
||||
if ($this->canAdmin()) {
|
||||
|
|
@ -107,6 +100,10 @@ class StoreController extends AdminController
|
|||
|
||||
$show->tools(function (Tools $tools) {
|
||||
$tools->append(new ExportGoodsSpu());
|
||||
|
||||
$tools->disableList();
|
||||
$tools->disableDelete();
|
||||
$tools->disableEdit();
|
||||
});
|
||||
return $show;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,21 +21,43 @@ class Product
|
|||
continue;
|
||||
}
|
||||
$cells = $row->toArray();
|
||||
|
||||
$index = 0;
|
||||
$goodsName = $cells[$index];// 商品名称
|
||||
$goodsTitle = $cells[++$index];// 商品副标题
|
||||
$categoryName = $cells[++$index];// 分类名称
|
||||
$goodsImage = $cells[++$index];// 商品图片
|
||||
$salePrice = $cells[++$index];// 销售价
|
||||
$marketPrice = $cells[++$index];// 市场价
|
||||
$costPrice = $cells[++$index];// 成本价
|
||||
$vipPrice = $cells[++$index];// 会员价
|
||||
$saleValue = $cells[++$index];// 销售值
|
||||
// 属性加价
|
||||
// 颜色[远峰蓝:0,石墨色:0,银色:0,金色:0,苍岭绿色:0]
|
||||
$rowAttrs = $cells[++$index];
|
||||
// 规格展示
|
||||
// 主体[上市月份:9月,品牌:Apple]
|
||||
$rowSpecs = $cells[++$index];
|
||||
$shipTemplate = $cells[++$index];// 运费模板
|
||||
$goodsWeight = $cells[++$index];// 商品重量(克)
|
||||
// 详细描述
|
||||
// https://qiniu.abcdefg.fun/banner/banner4.png,https://qiniu.abcdefg.fun/banner/banner5.png
|
||||
$goodsContent = $cells[++$index];
|
||||
|
||||
$goods = [
|
||||
'name' => $cells[0],
|
||||
'subtitle' => $cells[1],
|
||||
'name' => $goodsName,
|
||||
'subtitle' => $goodsTitle,
|
||||
'shipping_template_id' => 1,
|
||||
];
|
||||
$category = ProductCategory::where('name', $cells[2])->firstOrFail();
|
||||
$category = ProductCategory::where('name', $categoryName)->firstOrFail();
|
||||
$goods['category_id'] = $category->id;
|
||||
|
||||
// 图片组
|
||||
if ($path = $cells[3]) {
|
||||
if (Str::startsWith($path, ['http://', 'https://'])) {
|
||||
$images = explode(',', $path);
|
||||
if ($goodsImage) {
|
||||
if (Str::startsWith($goodsImage, ['http://', 'https://'])) {
|
||||
$images = explode(',', $goodsImage);
|
||||
} else {
|
||||
$images = $this->getImageUrlFromPath($path);
|
||||
$images = $this->getImageUrlFromPath($goodsImage);
|
||||
}
|
||||
if (count($images) > 0) {
|
||||
$goods['cover'] = $images[0];
|
||||
|
|
@ -44,39 +66,37 @@ class Product
|
|||
}
|
||||
|
||||
// 运费模板
|
||||
if ($cell_11 = data_get($cells, 11)) {
|
||||
$goods['shipping_template_id'] = $cell_11;
|
||||
if ($shipTemplate) {
|
||||
$goods['shipping_template_id'] = $shipTemplate;
|
||||
}
|
||||
// 重量
|
||||
if ($cell_12 = data_get($cells, 12)) {
|
||||
$goods['weight'] = $cell_12;
|
||||
if ($goodsWeight) {
|
||||
$goods['weight'] = $goodsWeight;
|
||||
}
|
||||
// 详细描述
|
||||
if ($cell_13 = data_get($cells, 13)) {
|
||||
if ($goodsContent) {
|
||||
$description = '';
|
||||
foreach(explode(',', $cell_13) as $item) {
|
||||
foreach(explode(',', $goodsContent) as $item) {
|
||||
$description .= '<p><img src="'.$item.'"/></p>';
|
||||
}
|
||||
$goods['description'] = $description;
|
||||
}
|
||||
|
||||
$goods = array_merge($goods, [
|
||||
'sell_price' => $cells[4] * 100,
|
||||
'market_price' => $cells[5] * 100,
|
||||
'cost_price' => $cells[6] * 100,
|
||||
'vip_price' => $cells[7] * 100,
|
||||
'sales_value' => $cells[8],
|
||||
'sell_price' => $salePrice * 100,
|
||||
'market_price' => $marketPrice * 100,
|
||||
'cost_price' => $costPrice * 100,
|
||||
'vip_price' => $vipPrice * 100,
|
||||
'sales_value' => $saleValue,
|
||||
]);
|
||||
|
||||
$product = ProductSpu::updateOrCreate(['name' => $goods['name']],$goods);
|
||||
|
||||
// 属性规格
|
||||
$coll_9 = data_get($cells, 9);
|
||||
$cell_10 = data_get($cells, 10);
|
||||
if ($coll_9) {
|
||||
$format_specs = $this->formatAttr($coll_9);
|
||||
$format_attrs = $this->formatAttr($cell_10);
|
||||
$spec_group = $this->getSpec($cells[0], $coll_9, $cell_10);
|
||||
if ($rowAttrs) {
|
||||
$format_specs = $this->formatAttr($rowAttrs);
|
||||
$format_attrs = $this->formatAttr($rowSpecs);
|
||||
$spec_group = $this->getSpec($cells[0], $rowAttrs, $rowSpecs);
|
||||
|
||||
$attrs = [];
|
||||
foreach($spec_group->attrs as $index => $item) {
|
||||
|
|
|
|||
|
|
@ -0,0 +1,219 @@
|
|||
<?php
|
||||
|
||||
namespace App\Admin\Imports;
|
||||
|
||||
use App\Exceptions\ImportException;
|
||||
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use App\Models\{ProductSpu, ProductCategory, ProductGroup};
|
||||
use Illuminate\Support\Str;
|
||||
use App\Models\ProductSku;
|
||||
use App\Models\Store\{Store, ProductSku as StoreProductSku};
|
||||
|
||||
class StoreProduct
|
||||
{
|
||||
public function load($file)
|
||||
{
|
||||
$reader = ReaderEntityFactory::createXLSXReader();
|
||||
$reader->open($file);
|
||||
|
||||
foreach ($reader->getSheetIterator() as $sheet) {
|
||||
foreach ($sheet->getRowIterator() as $num => $row) {
|
||||
if ($num === 1) {
|
||||
continue;
|
||||
}
|
||||
$cells = $row->toArray();
|
||||
|
||||
$index = 0;
|
||||
$storeName = $cells[$index];// 店铺名称
|
||||
$goodsName = $cells[++$index];// 商品名称
|
||||
$goodsTitle = $cells[++$index];// 商品副标题
|
||||
$categoryName = $cells[++$index];// 分类名称
|
||||
$goodsImage = $cells[++$index];// 商品图片
|
||||
$salePrice = $cells[++$index];// 销售价
|
||||
$marketPrice = $cells[++$index];// 市场价
|
||||
$costPrice = $cells[++$index];// 成本价
|
||||
$vipPrice = $cells[++$index];// 会员价
|
||||
$saleValue = $cells[++$index];// 销售值
|
||||
// 属性加价
|
||||
// 颜色[远峰蓝:0,石墨色:0,银色:0,金色:0,苍岭绿色:0]
|
||||
$rowAttrs = $cells[++$index];
|
||||
// 规格展示
|
||||
// 主体[上市月份:9月,品牌:Apple]
|
||||
$rowSpecs = $cells[++$index];
|
||||
$shipTemplate = $cells[++$index];// 运费模板
|
||||
$goodsWeight = $cells[++$index];// 商品重量(克)
|
||||
// 详细描述
|
||||
// https://qiniu.abcdefg.fun/banner/banner4.png,https://qiniu.abcdefg.fun/banner/banner5.png
|
||||
$goodsContent = $cells[++$index];
|
||||
|
||||
$stock = $cells[++$index];// 库存
|
||||
|
||||
$store = Store::where(['title' => $storeName])->firstOrFail();
|
||||
|
||||
$goods = [
|
||||
'name' => $goodsName,
|
||||
'subtitle' => $goodsTitle,
|
||||
'shipping_template_id' => 1,
|
||||
];
|
||||
$category = ProductCategory::where('name', $categoryName)->firstOrFail();
|
||||
$goods['category_id'] = $category->id;
|
||||
|
||||
// 图片组
|
||||
if ($goodsImage) {
|
||||
if (Str::startsWith($goodsImage, ['http://', 'https://'])) {
|
||||
$images = explode(',', $goodsImage);
|
||||
} else {
|
||||
$images = $this->getImageUrlFromPath($goodsImage);
|
||||
}
|
||||
if (count($images) > 0) {
|
||||
$goods['cover'] = $images[0];
|
||||
$goods['images'] = $images;
|
||||
}
|
||||
}
|
||||
|
||||
// 运费模板
|
||||
if ($shipTemplate) {
|
||||
$goods['shipping_template_id'] = $shipTemplate;
|
||||
}
|
||||
// 重量
|
||||
if ($goodsWeight) {
|
||||
$goods['weight'] = $goodsWeight;
|
||||
}
|
||||
// 详细描述
|
||||
if ($goodsContent) {
|
||||
$description = '';
|
||||
foreach(explode(',', $goodsContent) as $item) {
|
||||
$description .= '<p><img src="'.$item.'"/></p>';
|
||||
}
|
||||
$goods['description'] = $description;
|
||||
}
|
||||
|
||||
$goods = array_merge($goods, [
|
||||
'sell_price' => $salePrice * 100,
|
||||
'market_price' => $marketPrice * 100,
|
||||
'cost_price' => $costPrice * 100,
|
||||
'vip_price' => $vipPrice * 100,
|
||||
'sales_value' => $saleValue,
|
||||
'stock' => $stock,
|
||||
]);
|
||||
|
||||
$product = ProductSpu::updateOrCreate(['name' => $goods['name']],$goods);
|
||||
|
||||
// 属性规格
|
||||
if ($rowAttrs) {
|
||||
$format_specs = $this->formatAttr($rowAttrs);
|
||||
$format_attrs = $this->formatAttr($rowSpecs);
|
||||
$spec_group = $this->getSpec($cells[0], $rowAttrs, $rowSpecs);
|
||||
|
||||
$attrs = [];
|
||||
foreach($spec_group->attrs as $index => $item) {
|
||||
$attr = ['name' => $item['title'], 'attrs' => []];
|
||||
foreach(explode(PHP_EOL, $item['value']) as $value) {
|
||||
array_push($attr['attrs'], ['name' => $value, 'value' => data_get($format_attrs, "$index.values.$value")]);
|
||||
}
|
||||
array_push($attrs, $attr);
|
||||
}
|
||||
$product->update(['attrs' => $attrs]);
|
||||
|
||||
$specs = [];
|
||||
$product->specs()->delete();
|
||||
foreach($spec_group->specs as $index => $item) {
|
||||
$spec = ['name' => $item['title'], 'items' => []];
|
||||
foreach(explode(PHP_EOL, $item['value']) as $value) {
|
||||
array_push($spec['items'], ['name' => $value, 'value' => data_get($format_specs, "$index.values.$value")]);
|
||||
}
|
||||
array_push($specs, $spec);
|
||||
}
|
||||
$product->specs()->createMany($specs);
|
||||
}
|
||||
|
||||
// 清空现有的sku, 重新添加
|
||||
StoreProductSku::where('store_id', $store->id)->whereIn('product_sku_id', $product->skus->pluck('id'))->delete();
|
||||
$product->skus()->delete();
|
||||
ProductSku::createBySpu($product);
|
||||
|
||||
// 将sku 添加到门店
|
||||
$skuList = $product->skus()->get();
|
||||
$skus = [];
|
||||
foreach($skuList as $sku) {
|
||||
array_push($skus, ['amount' => $stock, 'product_sku_id' => $sku->id, 'status' => 1, 'store_id' => $store->id]);
|
||||
}
|
||||
StoreProductSku::insert($skus);
|
||||
}
|
||||
}
|
||||
|
||||
$reader->close();
|
||||
}
|
||||
|
||||
protected function getImageUrlFromPath($dir)
|
||||
{
|
||||
$disk = Storage::disk();
|
||||
$files = $disk->files($dir);
|
||||
$images = [];
|
||||
|
||||
foreach($files as $filename) {
|
||||
array_push($images, $disk->url($filename));
|
||||
}
|
||||
|
||||
return $images;
|
||||
}
|
||||
|
||||
protected function getSpec($name, $specs, $attrs)
|
||||
{
|
||||
if (Str::contains($specs, ':')) {
|
||||
// 创建新的规格
|
||||
$spec = ProductGroup::updateOrCreate([
|
||||
'name' => $name
|
||||
], [
|
||||
'attrs' => $this->formatGroupAttr($this->formatAttr($attrs)),
|
||||
'specs' => $this->formatGroupAttr($this->formatAttr($specs))
|
||||
]);
|
||||
} else {
|
||||
$spec = ProductGroup::where('name', $specs)->firstOrFail();
|
||||
}
|
||||
|
||||
return $spec;
|
||||
}
|
||||
|
||||
/**
|
||||
* 颜色[灰色:0,银色:0]
|
||||
* 尺寸[13寸:0,14寸:500,16寸:1000]
|
||||
* 处理器[M1:0,M1 Pro:1000,M1 Max:1500]
|
||||
* 内存[8G:0,16G:1000,32G:2000]
|
||||
* 硬盘[256G:0,512G:1000,1TB:2000]
|
||||
*
|
||||
* [
|
||||
* ['name' => '颜色', 'values' => ['灰色' => 0, '银色' => 0]],
|
||||
* ['name' => '尺寸', 'values' => ['13寸' => 0, '14寸' => 500, '16寸' => '1000']]
|
||||
* ...
|
||||
* ]
|
||||
*/
|
||||
protected function formatAttr($attrs)
|
||||
{
|
||||
$attr_data = [];
|
||||
if (!Str::contains($attrs, ':')) {
|
||||
return $attr_data;
|
||||
}
|
||||
foreach(explode("\n", $attrs) as $item) {
|
||||
$name = explode('[', $item)[0];
|
||||
$attr_values = [];
|
||||
foreach(explode(',', Str::between($item, '[', ']')) as $item1) {
|
||||
$item1_explode = explode(':', $item1);
|
||||
$attr_values[$item1_explode[0]] = $item1_explode[1];
|
||||
}
|
||||
array_push($attr_data, ['name' => $name, 'values' => $attr_values]);
|
||||
}
|
||||
|
||||
return $attr_data;
|
||||
}
|
||||
|
||||
protected function formatGroupAttr($data)
|
||||
{
|
||||
$list = [];
|
||||
foreach($data as $item) {
|
||||
array_push($list, ['title' => $item['name'], 'value' => implode(PHP_EOL, array_keys($item['values']))]);
|
||||
}
|
||||
return $list;
|
||||
}
|
||||
}
|
||||
|
|
@ -72,7 +72,7 @@ class LoginController extends Controller
|
|||
]);
|
||||
|
||||
$user = User::where('phone', $validated['phone'])->first();
|
||||
|
||||
|
||||
if (! $user?->verifyPassword($validated['password'])) {
|
||||
throw new BizException(__('Incorrect account or password'));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ class StoreController extends Controller
|
|||
$skus = $store->productSkus()
|
||||
->select($fields)
|
||||
->filter($input)
|
||||
->online()
|
||||
// ->online()
|
||||
->wherePivot('status', 1)
|
||||
->simplePaginate(Paginator::resolvePerPage('per_page', 20, 50));
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,8 @@ class Store extends Model
|
|||
{
|
||||
use HasFactory, HasDateTimeFormatter;
|
||||
|
||||
protected $fillable = ['title', 'image'];
|
||||
|
||||
protected $attributes = [
|
||||
'status' => 1,
|
||||
'sort' => 1
|
||||
|
|
|
|||
|
|
@ -353,6 +353,7 @@ class User extends Model implements AuthorizableContract, AuthenticatableContrac
|
|||
*/
|
||||
public function verifyPassword(string $password): bool
|
||||
{
|
||||
logger('password', ['p1' => $password, 'p2' => $this->password]);
|
||||
// 如果旧密码存在,则校验旧密码
|
||||
if ($this->old_password) {
|
||||
return $this->old_password === md5($password);
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ class RouteServiceProvider extends ServiceProvider
|
|||
$this->configureRateLimiting();
|
||||
|
||||
$this->routes(function () {
|
||||
Route::middleware('web')->group(base_path('routes/web.php'));
|
||||
Route::domain(config('endpoint.api.domain'))
|
||||
->group(function () {
|
||||
Route::prefix(config('endpoint.api.path'))
|
||||
|
|
|
|||
Binary file not shown.
|
|
@ -2,9 +2,4 @@
|
|||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::fallback(function () {
|
||||
return response()->json([
|
||||
'errcode' => 404,
|
||||
'message' => 'Not Found',
|
||||
], 404);
|
||||
});
|
||||
Route::redirect('', '/admin');
|
||||
|
|
|
|||
Loading…
Reference in New Issue