275 lines
11 KiB
PHP
275 lines
11 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Controllers;
|
||
|
||
use App\Admin\Actions\Grid\ReleaseCancel;
|
||
use App\Admin\Actions\Grid\ReleaseDown;
|
||
use App\Admin\Actions\Grid\ReleaseUp;
|
||
use App\Admin\Actions\Grid\SkuList;
|
||
use App\Admin\Actions\Grid\SkuSyncSpu;
|
||
use App\Admin\Extensions\Grid\Tools\Product\AddSku;
|
||
use App\Admin\Extensions\Grid\Tools\Product\BatchReleaseCancel;
|
||
use App\Admin\Extensions\Grid\Tools\Product\BatchReleaseDown;
|
||
use App\Admin\Extensions\Grid\Tools\Product\BatchReleaseUp;
|
||
use App\Admin\Extensions\Grid\Tools\Product\BatchSkuSyncSpu;
|
||
use App\Admin\Extensions\Grid\Tools\Product\InitSkuBySpecs;
|
||
use App\Admin\Extensions\Grid\Tools\Product\SettingSpecs;
|
||
use App\Admin\Repositories\ProductSpu;
|
||
use App\Exceptions\BizException;
|
||
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->number('stock');
|
||
$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');
|
||
});
|
||
}
|
||
|
||
public function destroy($id)
|
||
{
|
||
//如果含有子sku
|
||
$spu = ProductSpuModel::findOrFail($id);
|
||
if ($spu->hasSku()) {
|
||
throw new BizException(__('product-spu.options.deny_message'));
|
||
}
|
||
|
||
return parent::destroy($id);
|
||
}
|
||
|
||
/**
|
||
* 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->showRowSelector();
|
||
$grid->setResource('product-skus');
|
||
$grid->id();
|
||
$grid->tools(function (Grid\Tools $tools) use ($spu) {
|
||
//设置规格
|
||
if (Admin::user()->can('dcat.admin.product_spus.setting_specs')) {
|
||
$tools->append(new SettingSpecs($spu->id));
|
||
}
|
||
if ($spu->hasSku()) { //下面有sku,手动创建sku
|
||
if (Admin::user()->can('dcat.admin.product_spus.add_sku')) {
|
||
$tools->append(new AddSku($spu->id));
|
||
}
|
||
} else {//下面无sku,根据规格自动生成sku
|
||
if (Admin::user()->can('dcat.admin.product_spus.init_sku_by_specs')) {
|
||
$tools->append(new InitSkuBySpecs($spu->id));
|
||
}
|
||
}
|
||
});
|
||
$grid->batchActions(function ($batch) {
|
||
//批量下架
|
||
if (Admin::user()->can('dcat.admin.product_skus.batch_release_down')) {
|
||
$batch->add(new BatchReleaseDown());
|
||
}
|
||
//批量上架
|
||
if (Admin::user()->can('dcat.admin.product_skus.batch_release_up')) {
|
||
$batch->add(new BatchReleaseUp());
|
||
}
|
||
//批量取消审核
|
||
if (Admin::user()->can('dcat.admin.product_skus.batch_release_cancel')) {
|
||
$batch->add(new BatchReleaseCancel());
|
||
}
|
||
//批量同步主商品
|
||
if (Admin::user()->can('dcat.admin.product_skus.batch_sku_sync_spu')) {
|
||
$batch->add(new BatchSkuSyncSpu());
|
||
}
|
||
});
|
||
$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');
|
||
$grid->column('stock');
|
||
$grid->column('sales');
|
||
$grid->column('verify_state')
|
||
->using([0=>'正常', 1=>'审核中', 2=>'审核失败'])
|
||
->dot([
|
||
0 => 'success',
|
||
1 => 'primary',
|
||
2 => 'danger',
|
||
]);
|
||
$grid->column('release_at');
|
||
|
||
$grid->actions(function (Grid\Displayers\Actions $actions) {
|
||
if (is_null($actions->row->release_at) || $actions->row->verify_state == 1) {//未上架或者审核未通过
|
||
$actions->disableEdit(Admin::user()->cannot('dcat.admin.product_skus.edit'));
|
||
$actions->disableDelete(Admin::user()->cannot('dcat.admin.product_skus.destroy'));
|
||
if ($actions->row->verify_state == 0) {
|
||
if (Admin::user()->can('dcat.admin.product_skus.release_up')) {
|
||
$actions->append(new ReleaseUp());
|
||
}
|
||
if (Admin::user()->can('dcat.admin.product_skus.sku_sync_spu')) {
|
||
$actions->append(new SkuSyncSpu());
|
||
}
|
||
}
|
||
if ($actions->row->verify_state == 1) {
|
||
if (Admin::user()->can('dcat.admin.product_skus.release_cancel')) {
|
||
$actions->append(new ReleaseCancel());
|
||
}
|
||
}
|
||
}
|
||
if ($actions->row->release_at) {//已上架
|
||
if (Admin::user()->can('dcat.admin.product_skus.release_down')) {
|
||
$actions->append(new ReleaseDown());
|
||
}
|
||
}
|
||
});
|
||
}));
|
||
}
|
||
}
|