You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
329 lines
14 KiB
329 lines
14 KiB
<?php
|
|
|
|
namespace App\Admin\Controllers\v3;
|
|
|
|
use App\Admin\Actions\Grid\v3\GoodsCopy;
|
|
use App\Admin\Actions\Grid\v3\GoodsImage;
|
|
use App\Admin\Actions\Grid\v3\GoodsSpec;
|
|
use App\Admin\Common\Auth;
|
|
use App\Admin\Repositories\v3\Goods;
|
|
use Dcat\Admin\Admin;
|
|
use Dcat\Admin\Form;
|
|
use Dcat\Admin\Grid;
|
|
use Dcat\Admin\Show;
|
|
use Dcat\Admin\Controllers\AdminController;
|
|
use App\Models\v3\Category as CategoryModel;
|
|
use App\Models\v3\Store as StoreModel;
|
|
use Dcat\Admin\Form\NestedForm;
|
|
use App\Models\v3\Goods as GoodsModel;
|
|
use App\Models\v3\Market as MarketModel;
|
|
use App\Models\v3\GoodsCategory as GoodsCategoryModel;
|
|
use App\Models\v3\GoodsBanners as GoodsBannersModel;
|
|
|
|
class GoodsController extends AdminController
|
|
{
|
|
/**
|
|
* Make a grid builder.
|
|
*
|
|
* @return Grid
|
|
*/
|
|
protected function grid()
|
|
{
|
|
$marketId = Auth::getMarket();
|
|
if ($marketId){
|
|
$build = new \App\Models\v3\Goods();
|
|
$build = $build::where('market_id',$marketId);
|
|
$storeList = StoreModel::getStoreArray([['market_id','=',$marketId]]);
|
|
}else{
|
|
$build = new Goods();
|
|
$storeList = StoreModel::getStoreArray();
|
|
}
|
|
return Grid::make($build, function (Grid $grid) use($storeList){
|
|
// 二级分类
|
|
$categoryList = CategoryModel::getArray([['parent_id','>',0]],['选择分类'=>0]);
|
|
// 商品类目
|
|
$goodsCategoryList = GoodsCategoryModel::getArray([],['选择类目'=>0]);
|
|
|
|
//市场
|
|
$marketList = MarketModel::getMarketArray();
|
|
|
|
$grid->id->sortable();
|
|
$grid->cover_img_url->image('',50);
|
|
$grid->column('banners')->display(function () {
|
|
$id = $this->id;
|
|
$banners = GoodsBannersModel::getArray([['goods_id','=',$id],['type','=',1]]);
|
|
return $banners;
|
|
})->image('', 50)->width('5%');
|
|
$grid->name->editable();
|
|
$grid->price->editable();
|
|
$grid->original_price->editable();
|
|
$grid->column('goods_unit','单位')->editable();
|
|
$grid->column('spec','规格')->display(function($spec){
|
|
$item = '';
|
|
if(!empty($spec) && is_array($spec)){
|
|
foreach($spec as $key => $value){
|
|
$specKey = isset($value['spec_key'])?$value['spec_key']:'';
|
|
$specValue = isset($value['spec_value'])?$value['spec_value']:'';
|
|
$item .= $specKey.':'.$specValue.'</br>';
|
|
}
|
|
}
|
|
return $item;
|
|
});
|
|
$grid->column('market_id')->display(function($marketId) use($marketList){
|
|
return isset($marketList[$marketId]) ? $marketList[$marketId] : '';
|
|
});
|
|
$grid->store_id->display(function ($storeId) use($storeList){
|
|
return isset($storeList[$storeId]) ? $storeList[$storeId] : '';
|
|
})->width('12%');
|
|
$grid->category_id->select($categoryList);
|
|
// $grid->category_id->display(function ($categoryId) use($categoryList){
|
|
// return isset($categoryList[$categoryId]) ? $categoryList[$categoryId] : '';
|
|
// });
|
|
$grid->goods_category_id->select($goodsCategoryList)->help('注意商品类目所属的分类!!!');
|
|
// $grid->goods_category_id->display(function ($goodsCategoryId) use($goodsCategoryList){
|
|
// return isset($goodsCategoryList[$goodsCategoryId]) ? $goodsCategoryList[$goodsCategoryId] : '';
|
|
// });
|
|
|
|
// 简介
|
|
$grid->content->width(150);
|
|
|
|
$grid->on_sale->switch();
|
|
|
|
$grid->actions([new GoodsCopy(),new GoodsImage(),new GoodsSpec()]);
|
|
$grid->filter(function (Grid\Filter $filter) use($storeList, $categoryList, $goodsCategoryList, $marketList){
|
|
$filter->equal('id');
|
|
$filter->like('name');
|
|
$filter->equal('store_id')->select($storeList);
|
|
$filter->equal('category_id')->select($categoryList);
|
|
$filter->equal('goods_category_id')->select($goodsCategoryList);
|
|
if (!(Admin::user()->isRole('market_service'))){
|
|
$filter->equal('market_id')->select($marketList);
|
|
}
|
|
$filter->equal('on_sale')->select(GoodsModel::$_ONSALE);
|
|
$filter->equal('price');
|
|
$filter->equal('goods_unit');
|
|
});
|
|
|
|
$grid->model()->orderBy('id', 'desc');
|
|
// 每页10条
|
|
$grid->paginate(10);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a show builder.
|
|
*
|
|
* @param mixed $id
|
|
*
|
|
* @return Show
|
|
*/
|
|
protected function detail($id)
|
|
{
|
|
// $g = GoodsModel::find(1572);
|
|
// dd($g->toArray());
|
|
return Show::make($id, new Goods(), function (Show $show) {
|
|
$show->row(function (Show\Row $show) {
|
|
$show->width(6)->field('id');
|
|
$show->width(6)->field('name');
|
|
$show->width(6)->category_id->as(function ($categoryId){
|
|
$item = CategoryModel::getInfo($categoryId,'title');
|
|
return empty($item) ? '' : $item->title;
|
|
});
|
|
$show->width(6)->store_id->as(function ($storeId){
|
|
$item = storeModel::getStoreInfo($storeId,'name');
|
|
return empty($item) ? '' : $item->name;
|
|
});
|
|
$show->width(6)->goods_category_id->as(function ($goodsCategoryId){
|
|
$item = GoodsCategoryModel::getInfo($goodsCategoryId,'title');
|
|
return empty($item) ? '' : $item->title;
|
|
});
|
|
$show->width(6)->cover_img->image();
|
|
$show->width(6)->price;
|
|
$show->width(6)->original_price;
|
|
$show->width(6)->vip_price;
|
|
$show->width(6)->inventory;
|
|
$show->width(6)->goods_unit;
|
|
|
|
});
|
|
$show->row(function (Show\Row $show) {
|
|
$show->width(6)->on_sale_text;
|
|
$show->width(6)->content;
|
|
$show->width(6)->sort;
|
|
$show->width(6)->restrict_num;
|
|
$show->width(6)->start_num;
|
|
$show->width(6)->is_infinite_text;
|
|
|
|
$show->width(6)->tags->as(function ($value){
|
|
return $value;
|
|
});
|
|
$show->width(6)->spec->as(function ($value){
|
|
$text = '';
|
|
if($value && is_array($value)){
|
|
foreach ($value as $v){
|
|
$specKey = isset($v['spec_key'])?$v['spec_key']:'';
|
|
$specValue = isset($v['spec_value'])?$v['spec_value']:'';
|
|
$text .= '【'.$specKey .':'.$specValue.'】 ';
|
|
}
|
|
}
|
|
return $text;
|
|
});
|
|
$goodsModel = new GoodsModel();
|
|
$show->html(function () use($goodsModel){
|
|
if(empty($this->details_imgs)){
|
|
return '';
|
|
}else{
|
|
// 还需要优化
|
|
$value = $this->details_imgs;
|
|
$text = '';
|
|
foreach ($value as $v){
|
|
$img = $goodsModel->imageUrl($v);
|
|
$text .= "<img src='$img' />";
|
|
}
|
|
return $text;
|
|
}
|
|
});
|
|
|
|
// $show->created_at;
|
|
// $show->updated_at;
|
|
});
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Make a form builder.
|
|
*
|
|
* @return Form
|
|
*/
|
|
protected function form()
|
|
{
|
|
$builder = GoodsModel::with(['ImageBanners','VideoBanners']);
|
|
$marketId = Auth::getMarket();
|
|
return Form::make($builder, function (Form $form) use($marketId){
|
|
$form->hidden('id');
|
|
|
|
// 二级分类
|
|
$categoryList = CategoryModel::getArray([['parent_id','>',0]]);
|
|
// 商品类目
|
|
// $goodsCategoryList = GoodsCategoryModel::getArray();
|
|
if($marketId){
|
|
$storeList = StoreModel::getStoreArray([['market_id','=',$marketId]]);
|
|
}else{
|
|
$storeList = StoreModel::getStoreArray();
|
|
}
|
|
//市场
|
|
// $marketList = MarketModel::getMarketArray();
|
|
|
|
$form->column(6, function (Form $form) use($storeList,$categoryList){
|
|
$form->hidden('market_id')->default(0);
|
|
$form->select('category_id')->required()->options($categoryList)->load('goods_category_id', '/api/goods_category_list');
|
|
$form->select('goods_category_id');
|
|
$form->select('store_id')->required()->options($storeList);
|
|
$form->text('name')->required()->maxLength(20);
|
|
if($form->isCreating()){
|
|
$form->image('cover_img')->uniqueName()->autoUpload()->retainable()->required();
|
|
}else{
|
|
$form->image('cover_img')->uniqueName()->autoUpload()->required();
|
|
}
|
|
$form->number('sort');
|
|
});
|
|
$form->column(6, function (Form $form) {
|
|
$form->currency('price')->required()->symbol('¥');
|
|
$form->currency('original_price')->required()->symbol('¥');
|
|
$form->currency('vip_price')->required()->symbol('¥');
|
|
$form->currency('origin_shop_price')->required()->symbol('¥');
|
|
$form->text('goods_unit')->help('如:50克,100克,250克,500克,1000克,1500克等');
|
|
$form->radio('is_infinite')->options(['关闭','开启'])->default(1);
|
|
$form->number('inventory')->required()->attribute('min', 0)->default(1);
|
|
$form->number('restrict_num')->attribute('min', 0)->default(0)->help('0表示不限购');
|
|
$form->number('start_num')->attribute('min', 1)->default(1);
|
|
});
|
|
$form->column(12, function (Form $form) {
|
|
$form->hasMany('image_banners', function (Form\NestedForm $form) {
|
|
$form->image('path')->uniqueName()->autoUpload()->required()->width(2);
|
|
$form->number('sort')->width(2)->default(0);
|
|
$form->hidden('type')->value(1)->default(1);
|
|
})->required();
|
|
$form->divider();
|
|
$form->hasMany('video_banners', function (Form\NestedForm $form) {
|
|
$form->file('path')->uniqueName()->autoUpload()->width(2);
|
|
$form->number('sort')->width(2)->default(0);
|
|
$form->hidden('type')->value(2)->default(2);
|
|
});
|
|
$form->divider();
|
|
|
|
$form->multipleImage('details_imgs')->help('按图片顺序上传');
|
|
$form->text('content')->maxLength(50);
|
|
$form->text('remark')->maxLength(50);
|
|
|
|
$form->tags('tags','标签')->options(['新品','热销','新鲜']);
|
|
$form->table('spec', function (NestedForm $table) {
|
|
$table->text('spec_key')->required()->help('如:净含量:500克,保质期:120天,包装:12个/盒等');
|
|
$table->text('spec_value')->required();
|
|
});
|
|
$form->switch('on_sale')
|
|
->customFormat(function ($v) {
|
|
return $v == 1 ? 1 : 0;
|
|
})
|
|
->saving(function ($v) {
|
|
return $v == 1 ? 1 : 0;
|
|
})->default(1);
|
|
|
|
$form->disableResetButton();
|
|
$form->disableViewCheck();
|
|
$form->disableEditingCheck();
|
|
$form->disableCreatingCheck();
|
|
});
|
|
$form->saving(function (Form $form){
|
|
$id = $form->getKey;
|
|
$storeId = $form->input('store_id');
|
|
$tags = $form->input('tags');
|
|
$spec = $form->input('spec');
|
|
$detailsImgs = $form->input('details_imgs');
|
|
$goodsCategoryId = $form->input('goods_category_id');
|
|
$isInfinite = $form->input('is_infinite');
|
|
$inventory = $form->input('inventory');
|
|
$onSale = $form->input('on_sale');
|
|
$remark = $form->input('remark');
|
|
|
|
$OldInventory = $form->model()->inventory;
|
|
$OldIsInfinite = $form->model()->is_infinite;
|
|
|
|
$isInfinite = empty($isInfinite)?$OldIsInfinite:$isInfinite;
|
|
$inventory = empty($inventory)?$OldInventory:$inventory;
|
|
|
|
if(!empty($onSale) && $onSale == 1 && $inventory <= 0 && $isInfinite == 0){
|
|
return $form->error('商品上架且关闭无限库存时,库存必须为大于0的数字');
|
|
}
|
|
|
|
if(!empty($storeId)){
|
|
// 商品所属市场,根据店铺查询市场
|
|
$info = StoreModel::getStoreInfo($storeId,'market_id');
|
|
$form->market_id = $info->market_id;
|
|
$form->remark = $remark ? $remark : '';
|
|
}
|
|
|
|
$tagsArr = [];
|
|
if(!empty($tags)){
|
|
foreach($tags as $kt => $tag){
|
|
if(empty($tag)){
|
|
unset($tags[$kt]);
|
|
continue;
|
|
}
|
|
$tagsArr[] = $tag;
|
|
}
|
|
};
|
|
$form->tags = json_encode($tagsArr);
|
|
if($storeId && empty($spec)){
|
|
$form->spec = [];
|
|
}
|
|
if($storeId && empty($detailsImgs)){
|
|
$form->details_imgs = [];
|
|
}
|
|
if($storeId && empty($goodsCategoryId)){
|
|
$form->goods_category_id = 0;
|
|
}
|
|
});
|
|
});
|
|
}
|
|
|
|
}
|