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.
		
		
		
		
		
			
		
			
				
					
					
						
							142 lines
						
					
					
						
							4.2 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							142 lines
						
					
					
						
							4.2 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\Admin\Controllers;
							 | 
						|
								
							 | 
						|
								use App\Admin\Extensions\Grid\AuditProduct;
							 | 
						|
								use App\Admin\Repositories\Product;
							 | 
						|
								use App\Common\ProductStatus;
							 | 
						|
								use App\Common\UserStatus;
							 | 
						|
								use App\Models\Category;
							 | 
						|
								use App\Models\Supplier;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Illuminate\Support\Facades\Route;
							 | 
						|
								
							 | 
						|
								class ProductController extends AdminController
							 | 
						|
								{
							 | 
						|
								    /**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Product(['supplier:id,name', 'category:id,name']), function (Grid $grid) {
							 | 
						|
											//如果是审核页面,多加where条件判断
							 | 
						|
											if (strpos(Route::current()->uri, 'audit')) {
							 | 
						|
												$grid->model()->where('status', ProductStatus::UNAUDITED);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											$grid->column('id')->sortable();
							 | 
						|
											$grid->column('category.name', '分类');
							 | 
						|
								            $grid->column('picture')->image(60, 60);
							 | 
						|
											$grid->column('title');
							 | 
						|
											$grid->column('original_price');
							 | 
						|
								            $grid->column('price');
							 | 
						|
								            $grid->column('sale');
							 | 
						|
								            $grid->column('stock');
							 | 
						|
											$grid->column('supplier.name', '供应商');
							 | 
						|
											$grid->column('status')
							 | 
						|
												->if(fn() => $this->status == ProductStatus::UNAUDITED)
							 | 
						|
												->display('')
							 | 
						|
												->then(function ($column) {
							 | 
						|
													$column->append((new AuditProduct(null, 1))->setKey($this->id))->append(' ');
							 | 
						|
													$column->append((new AuditProduct(null, 2))->setKey($this->id));
							 | 
						|
												})
							 | 
						|
												->else()
							 | 
						|
												->using(ProductStatus::array())
							 | 
						|
												->dot([
							 | 
						|
													ProductStatus::ON_SALE => 'success',
							 | 
						|
													ProductStatus::UNAUDITED => '',
							 | 
						|
													ProductStatus::REFUSE => 'danger',
							 | 
						|
													ProductStatus::SOLD_OUT => 'warning',
							 | 
						|
												], 'primary');
							 | 
						|
								            $grid->column('created_at');
							 | 
						|
								            $grid->column('updated_at');
							 | 
						|
								
							 | 
						|
								            $grid->filter(function (Grid\Filter $filter) {
							 | 
						|
												$filter->panel();
							 | 
						|
								                $filter->equal('id');
							 | 
						|
								
							 | 
						|
								            });
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new Product(['supplier:id,name', 'category:id,name']), function (Show $show) {
							 | 
						|
								            $show->field('id');
							 | 
						|
								            $show->field('category.name', '所属分类');
							 | 
						|
											$show->field('title');
							 | 
						|
											$show->field('pictures')->image(80, 80);
							 | 
						|
								            $show->field('original_price');
							 | 
						|
								            $show->field('price');
							 | 
						|
								            $show->field('sale');
							 | 
						|
								            $show->field('stock');
							 | 
						|
											$show->field('status')->using(ProductStatus::array());
							 | 
						|
								            $show->field('supplier.name', '供应商');
							 | 
						|
											$show->field('know')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
											$show->field('content')->unescape()->as(fn($v) => preg_replace('/<script.*?>.*?<\/script>/is', '', $v));
							 | 
						|
								            $show->field('created_at');
							 | 
						|
								            $show->field('updated_at');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new Product(), function (Form $form) {
							 | 
						|
								            $form->display('id');
							 | 
						|
								
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', 0));
							 | 
						|
											array_shift($options);
							 | 
						|
								
							 | 
						|
											$form->select('category_id', '所属分类')
							 | 
						|
												->options($options)
							 | 
						|
												->required();
							 | 
						|
											$form->text('title')->required();
							 | 
						|
											$form->multipleImage('pictures')->required();
							 | 
						|
								            $form->text('original_price')->required();
							 | 
						|
								            $form->text('price')->required();
							 | 
						|
								            $form->text('sale')->default(0);
							 | 
						|
								            $form->text('stock')->default(9999)->required();
							 | 
						|
											$form->select('status')
							 | 
						|
												->options(ProductStatus::array())
							 | 
						|
												->default(ProductStatus::ON_SALE)
							 | 
						|
												->required();
							 | 
						|
											$form->select('supplier_id', '供应商')
							 | 
						|
												->options(Supplier::where('status', UserStatus::NORMAL)->pluck('name', 'id'))
							 | 
						|
												->required();
							 | 
						|
											$form->editor('know');
							 | 
						|
											$form->editor('content');
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
											//不允许编辑的字段
							 | 
						|
											if ($form->isEditing()) {
							 | 
						|
												$form->ignore(['id', 'created_at', 'updated_at', 'deleted_at']);
							 | 
						|
											}
							 | 
						|
								
							 | 
						|
											//特殊字段处理
							 | 
						|
											$form->sale = $form->sale ?? 0;
							 | 
						|
								
							 | 
						|
											//过滤null字段
							 | 
						|
											foreach ($form->input() as $k => $v) {
							 | 
						|
												if (is_null($v)) {
							 | 
						|
													$form->$k = '';
							 | 
						|
												}
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 |