diff --git a/database/seeders/GoodsSeeder.php b/database/seeders/GoodsSeeder.php index 36e46af..064c428 100644 --- a/database/seeders/GoodsSeeder.php +++ b/database/seeders/GoodsSeeder.php @@ -149,6 +149,28 @@ class GoodsSeeder extends Seeder ]], ], ], + [ + 'category_id' => $category_id, + 'type_id' => null, + 'brand_id' => null, + 'goods_sn' => '100026941752', + 'name' => 'K8蓝牙双模机械键盘', + 'cover_image' => 'https://img10.360buyimg.com/N6/s60x60_jfs/t1/93689/26/29208/314387/62c2ba03E05e1a7be/4bc834afac6df7bb.jpg', + 'description' => '', + 'content' => ['https://img30.360buyimg.com/sku/jfs/t1/11779/12/19259/52944/62fdf030E2a066294/99d11515015b29a4.jpg', 'https://img30.360buyimg.com/sku/jfs/t1/211607/38/4986/702616/6165654eE212dd412/ece0aab240b5db9b.jpg'], + 'images' => ['https://img12.360buyimg.com/n1/s450x450_jfs/t1/93689/26/29208/314387/62c2ba03E05e1a7be/4bc834afac6df7bb.jpg', 'https://img12.360buyimg.com/n1/s450x450_jfs/t1/208948/14/4177/99717/61600a26Efbdbbf8b/c9e1abd339f119f1.jpg'], + 'stock' => 100, + 'price' => 449, + 'attr' => [ + ['name' => '基本信息', 'values' => [ + ['name' => '商品名称', 'value' => '京东京造K3'], + ['name' => '商品毛重', 'value' => '0.785kg'], + ['name' => '兼容系统', 'value' => 'Windows,MacOS,iOS'], + ]] + ], + 'spec' => null, + 'part' => null, + ] ]; $service = GoodsService::make(); diff --git a/lang/zh_CN/goods.php b/lang/zh_CN/goods.php index 89cd30a..d75ea2f 100644 --- a/lang/zh_CN/goods.php +++ b/lang/zh_CN/goods.php @@ -33,11 +33,6 @@ return [ 'goods_sn' => '编号', 'cover_image' => '封面图', 'price' => '售价', - 'vip_price' => '会员价', - 'score_max_amount' => '积分最大抵扣', - 'weight' => '重量', - 'volume' => '体积', - 'shipping_tmp_id' => '运费模板', 'spec' => '规格', 'attr' => '属性', 'part' => '配件', @@ -48,13 +43,6 @@ return [ 'content' => '内容', 'created_at' => '创建时间', 'updated_at' => '更新时间', - 'check_remarks' => '未通过原因', - 'check_status' => '审核状态', - 'check_user_id' => '审核人', - 'check_user' => [ - 'name' => '审核人', - ], - 'check_at' => '审核时间', 'is_recommend' => '推荐', ], ]; diff --git a/src/GoodsServiceProvider.php b/src/GoodsServiceProvider.php index b043f73..a3f5a37 100644 --- a/src/GoodsServiceProvider.php +++ b/src/GoodsServiceProvider.php @@ -2,15 +2,15 @@ namespace Peidikeji\Goods; +use Dcat\Admin\Admin; +use Dcat\Admin\Form; +use Dcat\Admin\Layout\Menu; use Illuminate\Support\ServiceProvider; +use Peidikeji\Goods\Form\Attr; +use Peidikeji\Goods\Form\Spec; class GoodsServiceProvider extends ServiceProvider { - protected $menu = [ - ['title' => '商品模块', 'icon' => 'feather icon-layers', 'uri' => ''], - ['parent' => '商品模块', 'title' => '商品分类', 'icon' => '', 'uri' => '/goods-category'], - ]; - public function register() { } @@ -33,5 +33,19 @@ class GoodsServiceProvider extends ServiceProvider // ], 'dcat-admin-goods-migrations'); $this->loadTranslationsFrom(__DIR__.'/../lang', 'dcat-admin-goods'); + + // 添加默认菜单 + Admin::menu(function (Menu $menu) { + $menu->add([ + ['id' => 1, 'parent_id' => 0, 'title' => '商品模块', 'icon' => 'feather icon-layers', 'uri' => ''], + ['id' => 2, 'parent_id' => 1, 'title' => '商品分类', 'icon' => '', 'uri' => '/goods-category'], + ['id' => 3, 'parent_id' => 1, 'title' => '商品品牌', 'icon' => '', 'uri' => '/goods-brand'], + ['id' => 4, 'parent_id' => 1, 'title' => '商品类型', 'icon' => '', 'uri' => '/goods-type'], + ['id' => 5, 'parent_id' => 1, 'title' => '商品管理', 'icon' => '', 'uri' => '/goods'], + ]); + }); + + Form::extend('spec', Spec::class); + Form::extend('attr', Attr::class); } } diff --git a/src/Http/Admin/GoodsController.php b/src/Http/Admin/GoodsController.php index a727489..2c539bc 100644 --- a/src/Http/Admin/GoodsController.php +++ b/src/Http/Admin/GoodsController.php @@ -9,7 +9,10 @@ use Dcat\Admin\Grid\Displayers\Actions; use Dcat\Admin\Grid\Tools\Selector; use Dcat\Admin\Http\Controllers\AdminController; use Dcat\Admin\Layout\Content; +use Dcat\Admin\Layout\Row; use Dcat\Admin\Show; +use Dcat\Admin\Show\Tools; +use Dcat\Admin\Widgets\Card; use Illuminate\Http\Request; use Illuminate\Validation\Rule; use Peidikeji\Goods\Action\RowGoodsSale; @@ -172,11 +175,15 @@ class GoodsController extends AdminController ]); $info = Goods::with(['category', 'brand', 'type'])->findOrFail($id); $show = Show::make($info); - $show->field('goods_sn'); - $show->field('category.name'); - $show->field('brand.name'); - $show->field('type.name'); $show->field('name'); + $show->field('category_id')->as(function () { + if (!$this->category) { + return $this->category_id; + } + $parents = GoodsCategory::whereIn('id', $this->category->parent_ids)->orderBy('level')->pluck('name'); + $parents->push($this->category->name); + return $parents; + })->label(); $show->field('price'); $show->field('cover_image')->image('', 100); $show->field('images')->image('', 100); @@ -184,13 +191,31 @@ class GoodsController extends AdminController $show->field('spec')->view('dcat-admin-goods::goods.grid-attr'); $show->field('attr')->view('dcat-admin-goods::goods.grid-attr'); $show->field('part')->view('dcat-admin-goods::goods.grid-attr'); - $show->field('on_sale')->bool(); - $show->field('is_recommend')->bool(); - $show->field('sold_count'); - $show->field('created_at'); - $show->field('updated_at'); - return $show; + $row = new Row(); + + $row->column(7, $show); + $row->column(5, Show::make($info, function (Show $show) { + $show->panel() + ->title('其他信息') + ->tools(function (Tools $tools) { + $tools->disableEdit(); + $tools->disableList(); + $tools->disableDelete(); + $tools->disableBack(); + }); + + $show->field('goods_sn'); + $show->field('brand.name'); + $show->field('type.name'); + $show->field('on_sale')->bool(); + $show->field('is_recommend')->bool(); + $show->field('sold_count'); + $show->field('created_at'); + $show->field('updated_at'); + })); + + return $row; } protected function form() @@ -209,7 +234,7 @@ class GoodsController extends AdminController $unique->ignore($model->id); } $form->select('category_id')->options(GoodsCategory::selectOptions(null, false))->required(); - // $form->select('brand_id')->options(GoodsBrand::pluck('name', 'id')); + $form->select('brand_id')->options(GoodsBrand::pluck('name', 'id')); $form->text('name')->required(); $form->text('goods_sn')->rules([$unique], [ 'unique' => '商品编号已经存在', @@ -230,7 +255,6 @@ class GoodsController extends AdminController if ($isCreating || !$model->spec) { $form->number('price')->min(0)->attribute('step', 0.01); - $form->number('vip_price')->min(0)->attribute('step', 0.01); } else { $form->display('help', '提示')->value('商品其他信息, 请到 货品列表 去修改'); } diff --git a/src/Models/GoodsCategory.php b/src/Models/GoodsCategory.php index 131709f..adff807 100644 --- a/src/Models/GoodsCategory.php +++ b/src/Models/GoodsCategory.php @@ -4,6 +4,7 @@ namespace Peidikeji\Goods\Models; use Dcat\Admin\Traits\ModelTree; use EloquentFilter\Filterable; +use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Model; use Peidikeji\Goods\Filters\GoodsCategoryFilter; @@ -42,12 +43,6 @@ class GoodsCategory extends Model }); static::deleting(function ($category) { - // 所有下级分类 - $ids = GoodsCategory::where('path', 'like', '%-'.$category->id.'-%')->pluck('id'); - // 检查下级分类是否包含商品 - if (Goods::whereIn('category_id', array_merge($ids, [$category->id]))->exists()) { - // todo 阻止删除该分类 - } // 删除所有下级分类 GoodsCategory::where('path', 'like', '%-'.$category->id.'-%')->delete(); }); @@ -82,4 +77,14 @@ class GoodsCategory extends Model { return $q->where('is_enable', 1); } + + // 所有上级 id + // '-1-2-3' => ['1', '2', '3'] + // '-' => [] + public function parentIds(): Attribute + { + return Attribute::make( + get: fn() => array_slice(explode('-', $this->path), 1, -1), + ); + } }