205 lines
7.7 KiB
PHP
205 lines
7.7 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\SkuSyncSpu;
|
|
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\Repositories\ProductSku;
|
|
use App\Exceptions\BizException;
|
|
use App\Models\ProductBuynote;
|
|
use App\Models\ProductGroup;
|
|
use App\Models\ProductSku as ProductSkuModel;
|
|
use Carbon\Carbon;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Http\Controllers\AdminController;
|
|
use Dcat\Admin\Show;
|
|
use Illuminate\Http\Request;
|
|
|
|
class ProductSkuController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
$builder = ProductSku::with('category');
|
|
return Grid::make($builder, function (Grid $grid) {
|
|
$grid->showRowSelector();
|
|
$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());
|
|
}
|
|
});
|
|
$grid->column('id')->sortable();
|
|
// $grid->column('spu_id');
|
|
$grid->column('name');
|
|
$grid->column('subtitle');
|
|
$grid->column('category.name');
|
|
$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->model()->orderBy('created_at', 'desc');
|
|
$grid->model()->orderBy('release_at', 'desc');
|
|
$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());
|
|
}
|
|
}
|
|
});
|
|
|
|
$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 ProductSku(), function (Show $show) {
|
|
$show->field('id');
|
|
$show->field('spu_id');
|
|
$show->field('name');
|
|
$show->field('subtitle');
|
|
$show->field('category_id');
|
|
$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('specs');
|
|
$show->field('stock');
|
|
$show->field('sales');
|
|
$show->field('release_at');
|
|
$show->field('created_at');
|
|
$show->field('updated_at');
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
return Form::make(new ProductSku(), function (Form $form) {
|
|
$form->display('id');
|
|
$form->text('name')->required();
|
|
$form->text('subtitle');
|
|
$form->divider();
|
|
$form->image('cover')
|
|
->move('product-skus/cover/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload();
|
|
$form->multipleImage('images')
|
|
->move('product-skus/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload();
|
|
$form->file('media')->chunked()
|
|
->accept('mp4', 'mp4/*')
|
|
->move('prduct-sku-medias/'.Carbon::now()->toDateString())
|
|
->saveFullUrl()
|
|
->removable(false)
|
|
->autoUpload();
|
|
$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');
|
|
|
|
$form->ignore(['attr_group']);
|
|
$form->display('created_at');
|
|
$form->display('updated_at');
|
|
});
|
|
}
|
|
|
|
public function destroy($id)
|
|
{
|
|
$sku = ProductSkuModel::findOrFail($id);
|
|
if ($sku->release_at || $sku->verify_state == 1) {//如果是上架中、或者审核中的,无法删除
|
|
throw new BizException(__('product-sku.options.deny_message'));
|
|
}
|
|
return parent::destroy($id);
|
|
}
|
|
|
|
public function skus(Request $request)
|
|
{
|
|
$name = $request->input('q');
|
|
$query = ProductSkuModel::select('id', 'name as text')->where('name', 'like', "%$name%");
|
|
|
|
$data = $query->get();
|
|
return response()->json($data);
|
|
}
|
|
}
|