206 lines
9.0 KiB
PHP
206 lines
9.0 KiB
PHP
<?php
|
|
|
|
namespace Peidikeji\Goods\Http\Controllers\Admin;
|
|
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Form\BlockForm;
|
|
use Dcat\Admin\Form\NestedForm;
|
|
use Dcat\Admin\Form\Row;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Grid\Displayers\Actions;
|
|
use Dcat\Admin\Grid\Tools\Selector;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Peidikeji\Goods\Actions\RowGoodsSkuList;
|
|
use Peidikeji\Goods\Models\Goods;
|
|
use Peidikeji\Goods\Models\GoodsBrand;
|
|
use Peidikeji\Goods\Models\GoodsCategory;
|
|
use Peidikeji\Goods\Models\GoodsType;
|
|
|
|
class GoodsController extends AdminController
|
|
{
|
|
protected $translation = 'peidikeji.dcat-admin-extension-goods::goods';
|
|
|
|
protected function grid()
|
|
{
|
|
return Grid::make(Goods::with(['category', 'brand', 'type']), function (Grid $grid) {
|
|
$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', __('peidikeji.dcat-admin-extension-goods::goods.fields.category_id'), $categories->pluck('name', 'id'));
|
|
$selector->selectOne('brand_id', __('peidikeji.dcat-admin-extension-goods::goods.fields.brand_id'), $brands->pluck('name', 'id'));
|
|
$selector->selectOne('type_id', __('peidikeji.dcat-admin-extension-goods::goods.fields.type_id'), $types->pluck('name', 'id'));
|
|
$selector->selectOne('price', __('peidikeji.dcat-admin-extension-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('spec')->view('peidikeji.dcat-admin-extension-goods::grid.spec');
|
|
$grid->column('on_sale')->switch();
|
|
$grid->column('sold_count');
|
|
|
|
$grid->disableRowSelector();
|
|
|
|
$grid->actions(function (Actions $actions) {
|
|
$actions->append(new RowGoodsSkuList());
|
|
});
|
|
});
|
|
}
|
|
|
|
protected function detail($id)
|
|
{
|
|
$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('price');
|
|
$show->field('cover_image')->image('', 100);
|
|
$show->field('images')->image('', 100);
|
|
$show->field('content')->image('');
|
|
$show->field('spec')->view('peidikeji.dcat-admin-extension-goods::grid.spec');
|
|
$show->field('attr')->view('peidikeji.dcat-admin-extension-goods::grid.attr');
|
|
$show->field('part')->view('peidikeji.dcat-admin-extension-goods::grid.spec');
|
|
$show->field('on_sale')->bool();
|
|
$show->field('sold_count');
|
|
$show->field('created_at')->as(fn($v) => $this->created_at->format('Y-m-d H:i:s'));
|
|
$show->field('updated_at')->as(fn($v) => $this->updated_at->format('Y-m-d H:i:s'));
|
|
return $show;
|
|
}
|
|
|
|
protected function form()
|
|
{
|
|
return Form::make(new Goods(), function (Form $form) {
|
|
$model = $form->model();
|
|
$isCreating = $form->isCreating();
|
|
$type = null;
|
|
|
|
if (request('type_id') && $isCreating) {
|
|
$typeId = request('type_id');
|
|
$type = GoodsType::find($typeId);
|
|
if ($type) {
|
|
$attrbutes = [
|
|
'type_id' => $type->id,
|
|
'attr' => $type->attr,
|
|
'spec' => $type->spec,
|
|
'part' => $type->part,
|
|
];
|
|
$form->model($attrbutes);
|
|
}
|
|
}
|
|
$model = $form->model();
|
|
if (!$type) {
|
|
$type = $model && $model->type_id ? GoodsType::find($form->model()->type_id) : null;
|
|
}
|
|
|
|
$form->disableHeader();
|
|
$form->tab('基本设置', function (Form $form) use ($isCreating, $type) {
|
|
if ($isCreating) {
|
|
$form->select('type_id')->options(GoodsType::pluck('name', 'id'));
|
|
} else {
|
|
$form->display('type_id')->with(fn() => $type->name);
|
|
}
|
|
$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('goods_sn');
|
|
$form->image('cover_image')
|
|
->autoUpload()
|
|
->saveFullUrl()
|
|
->move('goods/goods')
|
|
->required();
|
|
$form->multipleImage('images')
|
|
->autoUpload()
|
|
->saveFullUrl()
|
|
->move('goods/goods');
|
|
$form->multipleImage('content')
|
|
->autoUpload()
|
|
->saveFullUrl()
|
|
->move('goods/goods');
|
|
|
|
$form->number('price')->min(0)->attribute('step', 0.01);
|
|
$form->switch('on_sale');
|
|
});
|
|
|
|
$form->tab('属性', function (Form $form) use ($type) {
|
|
$form->array('attr', null, function (NestedForm $table) use ($type) {
|
|
$values = data_get($type, 'attr.' . $table->getKey() . '.values') ?: [];
|
|
$table->text('group', '分组');
|
|
$table->text('name', '属性名')->required();
|
|
$table->autocomplete('value', '属性值')->options($values)->configs(['minChars' => 0]);
|
|
});
|
|
});
|
|
$form->tab('规格', function (Form $form) use ($type) {
|
|
$form->array('spec', null, function (NestedForm $table) use ($type) {
|
|
$table->text('name', '名称')->required();
|
|
$values = data_get($type, 'spec.' . $table->getKey() . '.values') ?: [];
|
|
$table->array('values', null, function (NestedForm $table) use ($values) {
|
|
$index = $table->getKey();
|
|
$table->autocomplete('value', '可选值')
|
|
->default($index === null ? '' : data_get($values, $index, ''))
|
|
->options($values)
|
|
->configs(['minChars' => 0]);
|
|
$table->number('price', '加价')->min(0)->default();
|
|
});
|
|
});
|
|
});
|
|
$form->tab('配件', function (Form $form) use ($type) {
|
|
$form->array('part', null, function (NestedForm $table) use ($type) {
|
|
$table->text('name', '名称')->required();
|
|
$values = data_get($type, 'part.' . $table->getKey() . '.values') ?: [];
|
|
$table->array('values', null, function (NestedForm $table) use ($values) {
|
|
$index = $table->getKey();
|
|
$table->autocomplete('value', '可选值')
|
|
->default($index === null ? '' : data_get($values, $index, ''))
|
|
->options($values)
|
|
->configs(['minChars' => 0]);
|
|
$table->number('price', '加价')->min(0)->default();
|
|
});
|
|
});
|
|
});
|
|
|
|
$form->disableResetButton();
|
|
$form->disableCreatingCheck();
|
|
$form->disableViewCheck();
|
|
$form->disableEditingCheck();
|
|
|
|
$admin_url = request()->url();
|
|
Admin::script(
|
|
<<<JS
|
|
var url = "{$admin_url}"
|
|
var isCreating = "${isCreating}"
|
|
$('[name="type_id"]').change(function (e) {
|
|
if (isCreating) {
|
|
Dcat.reload(url + '?type_id=' + e.target.value);
|
|
}
|
|
})
|
|
JS
|
|
);
|
|
});
|
|
}
|
|
}
|