195 lines
7.2 KiB
PHP
195 lines
7.2 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Controllers;
|
||
|
||
use App\Admin\Actions\Grid\SkuList;
|
||
use App\Admin\Extensions\Grid\Tools\Product\AddSku;
|
||
use App\Admin\Extensions\Grid\Tools\Product\InitSkuBySpecs;
|
||
use App\Admin\Extensions\Grid\Tools\Product\SettingSpecs;
|
||
use App\Admin\Repositories\ProductSpu;
|
||
use App\Models\ProductBuynote;
|
||
use App\Models\ProductFeature;
|
||
use App\Models\ProductGroup;
|
||
use App\Models\ProductSku;
|
||
use App\Models\ProductSpu as ProductSpuModel;
|
||
use Carbon\Carbon;
|
||
use Dcat\Admin\Admin;
|
||
use Dcat\Admin\Form;
|
||
use Dcat\Admin\Grid;
|
||
use Dcat\Admin\Http\Controllers\AdminController;
|
||
use Dcat\Admin\Layout\Content;
|
||
use Dcat\Admin\Show;
|
||
|
||
class ProductSpuController extends AdminController
|
||
{
|
||
/**
|
||
* Make a grid builder.
|
||
*
|
||
* @return Grid
|
||
*/
|
||
protected function grid()
|
||
{
|
||
return Grid::make(new ProductSpu(), function (Grid $grid) {
|
||
$grid->column('id')->sortable();
|
||
$grid->column('name');
|
||
$grid->column('subtitle');
|
||
// $grid->column('cover')->image(80, 80);
|
||
$grid->column('sell_price')->prepend('¥');
|
||
$grid->column('market_price')->prepend('¥');
|
||
$grid->column('cost_price')->prepend('¥');
|
||
$grid->column('vip_price')->prepend('¥');
|
||
$grid->column('weight');
|
||
$grid->column('created_at')->sortable();
|
||
|
||
//排序
|
||
$grid->model()->orderBy('created_at', 'desc');
|
||
|
||
/** 操作 **/
|
||
//新增
|
||
if (Admin::user()->can('dcat.admin.product_spus.create')) {
|
||
$grid->disableCreateButton(false);
|
||
}
|
||
//修改
|
||
// $grid->showQuickEditButton(Admin::user()->can('dcat.admin.product_spus.edit'));
|
||
//删除以及自定义操作
|
||
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
||
$actions->disableEdit(Admin::user()->cannot('dcat.admin.product_spus.edit'));
|
||
$actions->disableDelete(Admin::user()->cannot('dcat.admin.product_spus.destroy'));
|
||
if (Admin::user()->can('dcat.admin.product_spus.list')) {
|
||
$actions->prepend(new SkuList(__('admin.list')));
|
||
}
|
||
});
|
||
|
||
/** 查询 **/
|
||
$grid->filter(function (Grid\Filter $filter) {
|
||
$filter->panel();
|
||
$filter->equal('name')->width(3);
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a show builder.
|
||
*
|
||
* @param mixed $id
|
||
*
|
||
* @return Show
|
||
*/
|
||
protected function detail($id)
|
||
{
|
||
return Show::make($id, new ProductSpu(), function (Show $show) {
|
||
$show->field('id');
|
||
$show->field('name');
|
||
$show->field('subtitle');
|
||
$show->field('cover');
|
||
$show->field('images');
|
||
$show->field('description');
|
||
$show->field('sell_price');
|
||
$show->field('market_price');
|
||
$show->field('cost_price');
|
||
$show->field('vip_price');
|
||
$show->field('media');
|
||
$show->field('weight');
|
||
$show->field('attrs');
|
||
$show->field('created_at');
|
||
$show->field('updated_at');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a form builder.
|
||
*
|
||
* @return Form
|
||
*/
|
||
protected function form()
|
||
{
|
||
$builder = ProductSpu::with(['features']);
|
||
return Form::make($builder, function (Form $form) {
|
||
$form->display('id');
|
||
|
||
if ($form->isCreating()) {
|
||
$form->select('one_category')->options(admin_route('api.product_categories'))->load('two_category', admin_route('api.product_categories'));
|
||
$form->select('two_category')->load('category_id', admin_route('api.product_categories'));
|
||
$form->select('category_id')->required();
|
||
}
|
||
$form->text('name')->required();
|
||
$form->text('subtitle');
|
||
$form->divider();
|
||
$form->image('cover')
|
||
->move('product-spus/cover/'.Carbon::now()->toDateString())
|
||
->saveFullUrl()
|
||
->removable(false)
|
||
->autoUpload();
|
||
$form->multipleImage('images')
|
||
->move('product-spus/'.Carbon::now()->toDateString())
|
||
->saveFullUrl()
|
||
->removable(false)
|
||
->autoUpload();
|
||
$form->file('media')->chunked()
|
||
->accept('mp4', 'mp4/*')
|
||
->move('prduct-spu-medias/'.Carbon::now()->toDateString())
|
||
->saveFullUrl()
|
||
->removable(false)
|
||
->autoUpload();
|
||
$form->multipleSelect('features')->options(ProductFeature::all()->pluck('name', 'id'))->customFormat(function ($v) {
|
||
if (! $v) {
|
||
return [];
|
||
}
|
||
// 从数据库中查出的二维数组中转化成ID
|
||
return array_column($v, 'id');
|
||
});
|
||
$form->editor('description');
|
||
$form->select('buynote_id')->options(ProductBuynote::all()->pluck('name', 'id'));
|
||
$form->number('weight');
|
||
$form->divider();
|
||
$form->currency('sell_price')->symbol('¥')->default(0);
|
||
$form->currency('market_price')->symbol('¥')->default(0);
|
||
$form->currency('cost_price')->symbol('¥')->default(0);
|
||
$form->currency('vip_price')->symbol('¥');
|
||
$form->divider();
|
||
$form->select('attr_group')->options(ProductGroup::all()->pluck('name', 'id'));
|
||
$form->selectAttr('attrs')->listen('attr_group');
|
||
if ($form->isCreating()) {
|
||
$form->selectSpec('specs')->listen('attr_group');
|
||
}
|
||
|
||
$form->ignore(['one_category', 'two_category', 'attr_group']);
|
||
$form->display('created_at');
|
||
$form->display('updated_at');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Undocumented function
|
||
*
|
||
* @return void
|
||
*/
|
||
public function list(Content $content, ProductSpuModel $spu)
|
||
{
|
||
return $content->header(__('product-spu.labels.ProductSpu'))
|
||
->description($spu->name)
|
||
->body(Grid::make(ProductSku::where('spu_id', $spu->id), function ($grid) use ($spu) {
|
||
$grid->id();
|
||
$grid->disableRowSelector(false);
|
||
$grid->tools(function (Grid\Tools $tools) use ($spu) {
|
||
//设置规格
|
||
$tools->append(new SettingSpecs($spu->id));
|
||
|
||
if ($spu->hasSku()) { //下面有sku,手动创建sku
|
||
$tools->append(new AddSku($spu->id));
|
||
} else {//下面无sku,根据规格自动生成sku
|
||
$tools->append(new InitSkuBySpecs($spu->id));
|
||
}
|
||
});
|
||
$grid->column('name');
|
||
$grid->column('subtitle');
|
||
$grid->column('specs')->label();
|
||
$grid->column('sell_price')->prepend('¥');
|
||
$grid->column('market_price')->prepend('¥');
|
||
$grid->column('cost_price')->prepend('¥');
|
||
$grid->column('vip_price')->prepend('¥');
|
||
$grid->column('weight');
|
||
}));
|
||
}
|
||
}
|