diff --git a/README.md b/README.md index 8dacf3d..55ceb68 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ $menus = [ ['title' => '商品分类', 'icon' => '', 'uri' => '/goods-category', 'permission' => 'goods_category'], ['title' => '商品品牌', 'icon' => '', 'uri' => '/goods-brand', 'permission' => 'goods_brand'], ['title' => '商品类别', 'icon' => '', 'uri' => '/goods-type', 'permission' => 'goods_type'], - ['title' => '商品管理', 'icon' => '', 'uri' => '/goods', 'permission' => 'goods'], + ['title' => '商品管理', 'icon' => '', 'uri' => '/goods', 'permission' => 'goods', 'children' => ['import' => '导入']], ]], ]; ``` diff --git a/src/Action/BatchGoodsDown.php b/src/Action/BatchGoodsDown.php new file mode 100644 index 0000000..f16526a --- /dev/null +++ b/src/Action/BatchGoodsDown.php @@ -0,0 +1,25 @@ +getKey(); + Goods::whereIn('id', $ids)->update(['on_sale' => 0]); + + return $this->response()->success('操作成功')->refresh(); + } + + public function confirm() + { + return ['是否确定?']; + } +} + diff --git a/src/Action/BatchGoodsUp.php b/src/Action/BatchGoodsUp.php new file mode 100644 index 0000000..ce4d803 --- /dev/null +++ b/src/Action/BatchGoodsUp.php @@ -0,0 +1,25 @@ +getKey(); + Goods::whereIn('id', $ids)->update(['on_sale' => 1]); + + return $this->response()->success('操作成功')->refresh(); + } + + public function confirm() + { + return ['是否确定?']; + } +} + diff --git a/src/Form/Goods/ImportForm.php b/src/Form/Goods/ImportForm.php index 80d192a..e937342 100644 --- a/src/Form/Goods/ImportForm.php +++ b/src/Form/Goods/ImportForm.php @@ -35,20 +35,20 @@ class ImportForm extends Form array_shift($rows); foreach ($rows as $row) { $index = 0; - $sn = $row[$index]; - $category = $row[++$index]; - $brand = $row[++$index]; - $type = $row[++$index]; - $name = $row[++$index]; - $description = $row[++$index]; - $cover = $row[++$index]; - $images = $row[++$index]; - $content = $row[++$index]; - $price = $row[++$index]; - $stock = $row[++$index]; - $attr = $row[++$index]; - $spec = $row[++$index]; - $part = $row[++$index]; + $sn = data_get($row, $index); + $category = data_get($row, ++$index); + $brand = data_get($row, ++$index); + $type = data_get($row, ++$index); + $name = data_get($row, ++$index); + $description = data_get($row, ++$index); + $cover = data_get($row, ++$index); + $images = data_get($row, ++$index); + $content = data_get($row, ++$index); + $price = data_get($row, ++$index); + $stock = data_get($row, ++$index); + $attr = data_get($row, ++$index); + $spec = data_get($row, ++$index); + $part = data_get($row, ++$index); if (!$sn) { throw new \Exception('编号必填'); @@ -68,13 +68,13 @@ class ImportForm extends Form } $attributes = [ 'category_id' => $category->id, - 'brand_id' => $brand?->id, - 'type_id' => $type?->id, + 'brand_id' => data_get($brand, 'id'), + 'type_id' => data_get($type, 'id'), 'name' => $name, 'description' => $description, - 'cover_image' => $cover, - 'images' => $images, - 'content' => $content, + 'cover_image' => $this->parseFile($cover), + 'images' => $this->parseFiles($images), + 'content' => $this->parseFiles($content), 'price' => $price, 'stock' => $stock, 'attr' => $this->parseAttr($attr), @@ -82,11 +82,7 @@ class ImportForm extends Form 'part' => $this->parseAttr($part), ]; - $goods = Goods::where('goods_sn', $sn)->first(); - if (!$goods) { - $goods = new Goods(); - } - $goods->updateQuietly($attributes); + Goods::updateOrCreate(['goods_sn' => $sn], $attributes); } } @@ -103,7 +99,7 @@ class ImportForm extends Form $matches = [$str]; } foreach($matches as $item) { - preg_match_all('/(.*)\[(.*)\]/', $item[0], $itemMatch); + preg_match_all('/(.*)\[(.*)\]/', $item, $itemMatch); $name = data_get($itemMatch, '1.0'); $values = []; foreach(explode(',', data_get($itemMatch, '2.0')) as $k) { @@ -118,4 +114,33 @@ class ImportForm extends Form return $attr; } + + protected function parseFile($path = null) + { + if (!$path) { + return null; + } + if (Str::startsWith($path, ['http://', 'https://'])) { + return $path; + } + $disk = Storage::disk('public'); + + return $disk->url($path); + } + + protected function parseFiles($paths = null) + { + if (!$paths) { + return null; + } + if (!is_array($paths)) { + $paths = explode(',', $paths); + } + $data = []; + foreach($paths as $path) { + array_push($data, $this->parseFile($path)); + } + + return $data; + } } diff --git a/src/Http/Admin/GoodsController.php b/src/Http/Admin/GoodsController.php index f7417ae..451e548 100644 --- a/src/Http/Admin/GoodsController.php +++ b/src/Http/Admin/GoodsController.php @@ -6,6 +6,7 @@ use Dcat\Admin\Admin; use Dcat\Admin\Form; use Dcat\Admin\Grid; use Dcat\Admin\Grid\Displayers\Actions; +use Dcat\Admin\Grid\Tools\BatchActions; use Dcat\Admin\Grid\Tools\Selector; use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Layout\Content; @@ -14,6 +15,8 @@ use Dcat\Admin\Show; use Dcat\Admin\Show\Tools; use Illuminate\Http\Request; use Illuminate\Validation\Rule; +use Peidikeji\Goods\Action\BatchGoodsDown; +use Peidikeji\Goods\Action\BatchGoodsUp; use Peidikeji\Goods\Action\GridImportGoods; use Peidikeji\Goods\Action\RowGoodsSale; use Peidikeji\Goods\Form\Goods\AttrForm; @@ -123,9 +126,9 @@ class GoodsController extends AdminController $grid->column('brand.name'); $grid->column('type.name')->label(); $grid->column('name')->display(function () { - return ($this->cover_image ? ' ' : '').''.$this->name.''; + return ($this->cover_image ? ' ' : '') . '' . $this->name . ''; }); $grid->column('price'); $grid->column('stock') @@ -137,7 +140,6 @@ class GoodsController extends AdminController $grid->column('is_recommend')->switch(); $grid->column('sold_count'); - $grid->disableRowSelector(); $grid->createMode(Grid::CREATE_MODE_DEFAULT); $user = Admin::user(); @@ -149,24 +151,35 @@ class GoodsController extends AdminController $actions->view($user->can('dcat.admin.goods.show')); - $actions->edit($user->can('dcat.admin.goods.edit') && ! $row->on_sale); - if ($user->can('dcat.admin.goods.edit') && ! $row->on_sale) { - $actions->append('属性介绍'); - $actions->append('商品规格'); - $actions->append('商品配件'); + $actions->edit($user->can('dcat.admin.goods.edit') && !$row->on_sale); + if ($user->can('dcat.admin.goods.edit') && !$row->on_sale) { + $actions->append('属性介绍'); + $actions->append('商品规格'); + $actions->append('商品配件'); } if ($row->spec) { - $actions->append('货品列表'); + $actions->append('货品列表'); } if ($user->can('dcat.admin.goods.edit')) { $actions->append(new RowGoodsSale()); } - $actions->delete($user->can('dcat.admin.goods.destroy') && ! $row->on_sale); + $actions->delete($user->can('dcat.admin.goods.destroy') && !$row->on_sale); + }); + $grid->tools(function (Grid\Tools $tools) use ($user) { + if ($user->can('dcat.admin.goods.import')) { + $tools->append(new GridImportGoods()); + } + }); + $grid->batchActions(function (BatchActions $batch) use ($user) { + if ($user->can('dcat.admin.goods.edit')) { + $batch->add(new BatchGoodsUp()); + $batch->add(new BatchGoodsDown()); + } + $batch->disableDelete($user->cannot('dcat.admin.goods.destroy')); }); - $grid->tools(new GridImportGoods()); }); } @@ -210,7 +223,7 @@ class GoodsController extends AdminController $tools->disableDelete(); $tools->disableBack(); }); - + $show->field('goods_sn'); $show->field('brand.name'); $show->field('type.name'); @@ -243,27 +256,37 @@ class GoodsController extends AdminController $form->select('brand_id')->options(GoodsBrand::pluck('name', 'id')); $form->text('name')->required(); $form->text('description'); - $form->text('goods_sn')->rules([$unique], [ - 'unique' => '商品编号已经存在', - ]); + $form->text('goods_sn')->rules([$unique], ['unique' => '商品编号已经存在']); $form->image('cover_image') - ->autoUpload() - ->saveFullUrl() - ->move('goods/cover-image') - ->required(); + ->uniqueName() + ->autoUpload() + ->saveFullUrl() + ->retainable() + ->removable(false) + ->autoSave(false) + ->move('goods/cover-image') + ->required(); $form->multipleImage('images') - ->autoUpload() - ->saveFullUrl() - ->move('goods/images'); + ->uniqueName() + ->autoUpload() + ->saveFullUrl() + ->retainable() + ->removable(false) + ->autoSave(false) + ->move('goods/images'); $form->multipleImage('content') - ->autoUpload() - ->saveFullUrl() - ->move('goods/content'); + ->uniqueName() + ->autoUpload() + ->saveFullUrl() + ->retainable() + ->removable(false) + ->autoSave(false) + ->move('goods/content'); if ($isCreating || !$model->spec) { $form->currency('price')->symbol('¥'); } else { - $form->display('help', '提示')->value('商品其他信息, 请到 货品列表 去修改'); + $form->display('help', '提示')->value('商品其他信息, 请到 货品列表 去修改'); } $form->hidden('stock')->default(0); @@ -276,7 +299,7 @@ class GoodsController extends AdminController $form->disableViewButton(); $form->creating(function (Form $form) { - if (! $form->goods_sn) { + if (!$form->goods_sn) { $form->goods_sn = GoodsService::make()->generateSn(); } });