313 lines
12 KiB
PHP
313 lines
12 KiB
PHP
<?php
|
|
|
|
namespace Peidikeji\Goods\Http\Admin;
|
|
|
|
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;
|
|
use Dcat\Admin\Layout\Row;
|
|
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;
|
|
use Peidikeji\Goods\Form\Goods\PartForm;
|
|
use Peidikeji\Goods\Form\Goods\SpecForm;
|
|
use Peidikeji\Goods\GoodsService;
|
|
use Peidikeji\Goods\Models\Goods;
|
|
use Peidikeji\Goods\Models\GoodsBrand;
|
|
use Peidikeji\Goods\Models\GoodsCategory;
|
|
use Peidikeji\Goods\Models\GoodsSku;
|
|
use Peidikeji\Goods\Models\GoodsType;
|
|
|
|
class GoodsController extends AdminController
|
|
{
|
|
protected $translation = 'dcat-admin-goods::goods';
|
|
|
|
public function list(Request $request)
|
|
{
|
|
$query = Goods::filter($request->all());
|
|
|
|
$query->select(['id', 'name as text']);
|
|
|
|
if ($request->filled('_paginate') || $request->filled('amp;_paginate')) {
|
|
$list = $query->paginate();
|
|
} else {
|
|
$list = $query->get();
|
|
}
|
|
|
|
return $list;
|
|
}
|
|
|
|
public function attr($goods, Content $content)
|
|
{
|
|
$goods = Goods::with(['type'])->findOrFail($goods);
|
|
$form = AttrForm::make([
|
|
'type' => $goods->type,
|
|
'attr' => $goods->attr,
|
|
])->payload(['type_id' => $goods->type?->id, 'goods_id' => $goods->id])->appendHtmlAttribute('class', 'bg-white');
|
|
|
|
return $content
|
|
->translation($this->translation())
|
|
->title($goods->name)
|
|
->description($goods->type?->name)
|
|
->body($form);
|
|
}
|
|
|
|
public function spec($goods, Content $content)
|
|
{
|
|
$goods = Goods::findOrFail($goods);
|
|
$form = SpecForm::make([
|
|
'type' => $goods->type,
|
|
'spec' => $goods->spec,
|
|
])->payload(['type_id' => $goods->type_id, 'goods_id' => $goods->id])->appendHtmlAttribute('class', 'bg-white');
|
|
|
|
return $content
|
|
->translation($this->translation())
|
|
->title($goods->name)
|
|
->description($goods->type?->name)
|
|
->body($form);
|
|
}
|
|
|
|
public function part($goods, Content $content)
|
|
{
|
|
$goods = Goods::findOrFail($goods);
|
|
$form = PartForm::make([
|
|
'type' => $goods->type,
|
|
'part' => $goods->part,
|
|
])->payload(['type_id' => $goods->type_id, 'goods_id' => $goods->id])->appendHtmlAttribute('class', 'bg-white');
|
|
|
|
return $content
|
|
->translation($this->translation())
|
|
->title($goods->name)
|
|
->description($goods->type?->name)
|
|
->body($form);
|
|
}
|
|
|
|
protected function grid()
|
|
{
|
|
return Grid::make(Goods::with(['category', 'brand', 'type', 'skus']), function (Grid $grid) {
|
|
$user = Admin::user();
|
|
|
|
$grid->export();
|
|
$grid->model()->sort();
|
|
|
|
$grid->selector(function (Selector $selector) {
|
|
$brands = GoodsBrand::get();
|
|
$types = GoodsType::get();
|
|
$categories = GoodsCategory::where('level', 3)->get();
|
|
$prices = ['0-999', '1000-1999', '2000-4999', '5000+'];
|
|
$selector->selectOne('category_id', __('dcat-admin-goods::goods.fields.category_id'), $categories->pluck('name', 'id'));
|
|
$selector->selectOne('brand_id', __('dcat-admin-goods::goods.fields.brand_id'), $brands->pluck('name', 'id'));
|
|
$selector->selectOne('type_id', __('dcat-admin-goods::goods.fields.type_id'), $types->pluck('name', 'id'));
|
|
$selector->selectOne('price', __('dcat-admin-goods::goods.fields.price'), $prices, function ($q, $value) use ($prices) {
|
|
$parsePrice = data_get($prices, $value);
|
|
if ($parsePrice) {
|
|
$parts = explode('-', $parsePrice);
|
|
$parts = array_map(fn ($v) => (int) $v, $parts);
|
|
if (count($parts) > 1) {
|
|
$q->whereBetween('price', $parts);
|
|
} else {
|
|
$q->where('price', '>', $parts[0]);
|
|
}
|
|
}
|
|
});
|
|
});
|
|
|
|
$grid->column('goods_sn');
|
|
$grid->column('category.name');
|
|
$grid->column('brand.name');
|
|
$grid->column('type.name')->label();
|
|
$grid->column('name')->display(function () {
|
|
return ($this->cover_image ? '<img src="' . $this->cover_image . '" width="60" class="img-thumbnail"/> ' : '') . '<a href="' . admin_url('goods/' . $this->id) . '">' . $this->name . '</a>';
|
|
});
|
|
$grid->column('price');
|
|
$grid->column('stock')
|
|
->if(fn () => !!$this->spec)
|
|
->display(fn () => $this->skus->sum('stock'))
|
|
->else()
|
|
->editable();
|
|
$grid->column('on_sale')->bool();
|
|
$grid->column('is_recommend')->if(fn() => $user->can('dcat.admin.goods.edit'))->switch()->else()->bool();
|
|
$grid->column('sold_count');
|
|
|
|
$grid->createMode(Grid::CREATE_MODE_DEFAULT);
|
|
|
|
$grid->showCreateButton($user->can('dcat.admin.goods.create'));
|
|
|
|
$grid->actions(function (Actions $actions) use ($user) {
|
|
$row = $actions->row;
|
|
|
|
$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('<a href="' . admin_route('goods.attr', ['goods' => $row->id]) . '" class="">属性介绍</a>');
|
|
$actions->append('<a href="' . admin_route('goods.spec', ['goods' => $row->id]) . '" class="">商品规格</a>');
|
|
$actions->append('<a href="' . admin_route('goods.part', ['goods' => $row->id]) . '" class="">商品配件</a>');
|
|
}
|
|
|
|
if ($row->spec) {
|
|
$actions->append('<a href="' . admin_route('goods_sku.index', ['goods' => $row->id]) . '" class="">货品列表</a>');
|
|
}
|
|
|
|
if ($user->can('dcat.admin.goods.edit')) {
|
|
$actions->append(new RowGoodsSale());
|
|
}
|
|
|
|
$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'));
|
|
});
|
|
});
|
|
}
|
|
|
|
protected function detail($id)
|
|
{
|
|
Admin::css([
|
|
'vendor/dcat-admin-goods/goods.css',
|
|
]);
|
|
$info = Goods::with(['category', 'brand', 'type'])->findOrFail($id);
|
|
$show = Show::make($info);
|
|
$show->field('name');
|
|
$show->field('description');
|
|
$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);
|
|
$show->field('content')->image('');
|
|
$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->disableEditButton();
|
|
$show->disableListButton();
|
|
$show->disableDeleteButton();
|
|
|
|
$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()
|
|
{
|
|
return Form::make(Goods::with([]), function (Form $form) {
|
|
$model = $form->model();
|
|
$isCreating = $form->isCreating();
|
|
$unique = Rule::unique('goods', 'goods_sn');
|
|
|
|
if ($isCreating) {
|
|
$form->select('type_id')->options(GoodsType::pluck('name', 'id'));
|
|
$form->select('merchant_id')->ajax('api/merchants?_paginate=1');
|
|
} else {
|
|
$type = $model->type_id ? GoodsType::find($model->type_id) : null;
|
|
$form->display('type_id')->with(fn () => $model->type_id ? $type->name : '');
|
|
$unique->ignore($model->id);
|
|
}
|
|
$form->select('category_id')->options(GoodsCategory::selectOptions(null, false))->required();
|
|
$form->select('brand_id')->options(GoodsBrand::pluck('name', 'id'));
|
|
$form->text('name')->required();
|
|
$form->text('description');
|
|
$form->text('goods_sn')->rules([$unique], ['unique' => '商品编号已经存在']);
|
|
$form->image('cover_image')
|
|
->uniqueName()
|
|
->autoUpload()
|
|
->saveFullUrl()
|
|
->retainable()
|
|
->removable(false)
|
|
->autoSave(false)
|
|
->move('goods/cover-image')
|
|
->required();
|
|
$form->multipleImage('images')
|
|
->uniqueName()
|
|
->autoUpload()
|
|
->saveFullUrl()
|
|
->retainable()
|
|
->removable(false)
|
|
->autoSave(false)
|
|
->move('goods/images');
|
|
$form->multipleImage('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('商品其他信息, 请到 <a href="' . admin_route('goods_sku.index', ['goods' => $model->id]) . '" target="_blank">货品列表<a/> 去修改');
|
|
}
|
|
|
|
$form->hidden('stock')->default(0);
|
|
$form->hidden('is_recommend')->default(0);
|
|
$form->disableResetButton();
|
|
$form->disableCreatingCheck();
|
|
$form->disableViewCheck();
|
|
$form->disableEditingCheck();
|
|
$form->disableDeleteButton();
|
|
$form->disableViewButton();
|
|
|
|
$form->creating(function (Form $form) {
|
|
if (!$form->goods_sn) {
|
|
$form->goods_sn = GoodsService::make()->generateSn();
|
|
}
|
|
});
|
|
|
|
$form->deleting(function (Form $form) {
|
|
$data = $form->model()->toArray();
|
|
// 删除 SKU
|
|
GoodsSku::whereIn('goods_id', array_column($data, 'id'))->delete();
|
|
});
|
|
});
|
|
}
|
|
}
|