144 lines
5.4 KiB
PHP
144 lines
5.4 KiB
PHP
<?php
|
||
|
||
namespace App\Admin\Controllers;
|
||
|
||
use App\Admin\Repositories\Good;
|
||
use App\Models\{ActivityGood, GoodsCart, Good as GoodModel};
|
||
use Dcat\Admin\Form;
|
||
use Dcat\Admin\Grid;
|
||
use Dcat\Admin\Show;
|
||
use Dcat\Admin\Http\Controllers\AdminController;
|
||
use Carbon\Carbon;
|
||
use Dcat\Admin\Layout\Content;
|
||
use Dcat\Admin\Http\JsonResponse;
|
||
|
||
class GoodController extends AdminController
|
||
{
|
||
/**
|
||
* Make a grid builder.
|
||
*
|
||
* @return Grid
|
||
*/
|
||
protected function grid()
|
||
{
|
||
$builder = Good::with(['category', 'type']);
|
||
return Grid::make($builder, function (Grid $grid) {
|
||
$grid->column('id')->sortable();
|
||
$grid->column('goods_name');
|
||
$grid->column('goods_cover')->image(100, 100);
|
||
$grid->column('category.category_name', '商品分类')->label();
|
||
// $grid->column('goods_des');
|
||
// $grid->column('goods_spu');
|
||
$grid->column('type.type_name', '商品类型')->label();
|
||
$grid->column('goods_price');
|
||
$grid->column('sell_num');
|
||
$grid->column('float_sell_null');
|
||
// $grid->column('is_too')->bool(['1' => true, '0' => false]);
|
||
$grid->column('is_hot')->switch();
|
||
$grid->column('is_sell')->switch()->help('已上架商品,下架会导致已加入购物车的商品清空,同时清空对应活动兑换配置');
|
||
// $grid->column('created_at');
|
||
// $grid->column('updated_at')->sortable();
|
||
$grid->filter(function (Grid\Filter $filter) {
|
||
// $filter->equal('id');
|
||
$filter->like('goods_name');
|
||
$filter->equal('category_id', '商品分类')->select(admin_route('goods_categories.api'));
|
||
$filter->equal('type_id', '商品类型')->select(admin_route('goods_types.api'));
|
||
|
||
});
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a show builder.
|
||
*
|
||
* @param mixed $id
|
||
*
|
||
* @return Show
|
||
*/
|
||
protected function detail($id)
|
||
{
|
||
$builder = Good::with(['category', 'type']);
|
||
return Show::make($id, $builder, function (Show $show) {
|
||
$show->field('id');
|
||
$show->field('goods_name');
|
||
$show->field('goods_des');
|
||
// $show->field('goods_spu');
|
||
$show->field('goods_cover')->image(100, 100);
|
||
$show->field('goods_image')->image(100, 100);
|
||
$show->field('category.category_name', '商品分类');
|
||
$show->field('type.type_name', '商品类型');
|
||
$show->field('goods_price');
|
||
$show->field('created_at');
|
||
$show->field('updated_at');
|
||
});
|
||
}
|
||
|
||
/**
|
||
* Make a form builder.
|
||
*
|
||
* @return Form
|
||
*/
|
||
protected function form()
|
||
{
|
||
$builder = Good::with('goods_attrs');
|
||
return Form::make($builder, function (Form $form) {
|
||
$form->display('id');
|
||
$form->text('goods_name')->required();
|
||
$form->textarea('goods_des');
|
||
// $form->text('goods_spu');
|
||
$form->image('goods_cover')
|
||
->accept('jpg,png,gif,jpeg', 'image/*')
|
||
->move('milk-tea/goods/'.Carbon::now()->toDateString())
|
||
->saveFullUrl()->removable(false)->autoUpload()
|
||
->help('建议图片尺寸:150*150');
|
||
// $form->image('goods_image')
|
||
// ->accept('jpg,png,gif,jpeg', 'image/*')
|
||
// ->move('milk-tea/goods/'.Carbon::now()->toDateString())
|
||
// ->saveFullUrl()->removable(false)->autoUpload()
|
||
// ->required()->help('建议图片尺寸:750*580');
|
||
$form->select('category_id')->options(admin_route('goods_categories.api'))->required();
|
||
$form->select('type_id')->options(admin_route('goods_types.api'))->required();
|
||
$form->currency('goods_price')->symbol('¥')->default(0)->required();
|
||
$form->currency('market_price')->symbol('¥')->default(0);
|
||
$form->number('float_sell_null')->default(0);
|
||
|
||
// $form->switch('is_too', '第二杯半价')->default(0);
|
||
//上下架
|
||
if ($form->isEditing()) {
|
||
$form->hidden('is_hot');
|
||
$form->hidden('is_sell');
|
||
$form->saving(function (Form $form) {
|
||
//如果下架
|
||
if($form->model()->is_sell == 1 && !$form->is_sell){
|
||
// ActivityGood::where('goods_id', $form->model()->id)->delete();//清空对应兑换数据
|
||
GoodsCart::where('goods_id', $form->model()->id)->delete();//清空购物车数据
|
||
}
|
||
});
|
||
}
|
||
|
||
$form->selectAttr('goods_attrs', '属性')->listen('type_id');
|
||
// $form->sku('goods_attrs', '属性');
|
||
$form->display('created_at');
|
||
$form->display('updated_at');
|
||
});
|
||
}
|
||
|
||
public function edit($id, Content $content){
|
||
$goods = GoodModel::findOrFail($id);
|
||
if($goods->is_sell == 1){
|
||
admin_toastr('请先下架该商品', 'error');
|
||
return redirect(admin_route('goods.index'));
|
||
}
|
||
return parent::edit($id, $content);
|
||
}
|
||
|
||
public function destroy($id){
|
||
$goods = GoodModel::findOrFail($id);
|
||
if($goods->is_sell == 1){
|
||
return JsonResponse::make()->error('请先下架该商品');
|
||
// return Admin::make()->success('请先下架该商品');
|
||
}
|
||
return parent::destroy($id);
|
||
}
|
||
}
|