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.
		
		
		
		
		
			
		
			
				
					
					
						
							115 lines
						
					
					
						
							3.0 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							115 lines
						
					
					
						
							3.0 KiB
						
					
					
				
								<?php
							 | 
						|
								
							 | 
						|
								namespace App\AdminAgent\Controllers;
							 | 
						|
								
							 | 
						|
								use App\Models\AgentProduct;
							 | 
						|
								use App\Models\Category;
							 | 
						|
								use Dcat\Admin\Admin;
							 | 
						|
								use Dcat\Admin\Form;
							 | 
						|
								use Dcat\Admin\Grid;
							 | 
						|
								use Dcat\Admin\Layout\Content;
							 | 
						|
								use Dcat\Admin\Layout\Row;
							 | 
						|
								use Dcat\Admin\Show;
							 | 
						|
								use Dcat\Admin\Http\Controllers\AdminController;
							 | 
						|
								use Dcat\Admin\Tree;
							 | 
						|
								
							 | 
						|
								
							 | 
						|
								class CategoryController extends AdminController
							 | 
						|
								{
							 | 
						|
									public function index(Content $content)
							 | 
						|
									{
							 | 
						|
										return $content->header('产品分类')
							 | 
						|
											->body(function (Row $row) {
							 | 
						|
												$tree = new Tree(new Category);
							 | 
						|
												$tree->query(function ($model) {
							 | 
						|
													//agent_id为0是系统分类,其它是代理商分类
							 | 
						|
													return $model->where('agent_id', Admin::user()->id);
							 | 
						|
												});
							 | 
						|
												$row->column(12, $tree);
							 | 
						|
											});
							 | 
						|
									}
							 | 
						|
								
							 | 
						|
									/**
							 | 
						|
								     * Make a grid builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Grid
							 | 
						|
								     */
							 | 
						|
								    protected function grid()
							 | 
						|
								    {
							 | 
						|
								        return Grid::make(new Category(), function (Grid $grid) {
							 | 
						|
								            $grid->column('id')->sortable();
							 | 
						|
								            $grid->column('name');
							 | 
						|
								            $grid->column('pid');
							 | 
						|
								            $grid->column('sort');
							 | 
						|
								            $grid->column('template');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a show builder.
							 | 
						|
								     *
							 | 
						|
								     * @param mixed $id
							 | 
						|
								     *
							 | 
						|
								     * @return Show
							 | 
						|
								     */
							 | 
						|
								    protected function detail($id)
							 | 
						|
								    {
							 | 
						|
								        return Show::make($id, new Category(), function (Show $show) {
							 | 
						|
								            $show->field('id');
							 | 
						|
								            $show->field('name');
							 | 
						|
								            $show->field('pid');
							 | 
						|
								            $show->field('sort');
							 | 
						|
								            $show->field('template');
							 | 
						|
								        });
							 | 
						|
								    }
							 | 
						|
								
							 | 
						|
								    /**
							 | 
						|
								     * Make a form builder.
							 | 
						|
								     *
							 | 
						|
								     * @return Form
							 | 
						|
								     */
							 | 
						|
								    protected function form()
							 | 
						|
								    {
							 | 
						|
								        return Form::make(new Category(), function (Form $form) {
							 | 
						|
								            $agent_id = Admin::user()->id;
							 | 
						|
											$options = Category::selectOptions(fn($query) => $query->where('agent_id', $agent_id));
							 | 
						|
								
							 | 
						|
								        	$form->display('id');
							 | 
						|
								            $form->hidden('agent_id')->value($agent_id)->required();
							 | 
						|
											$form->select('pid')->options($options)->required();
							 | 
						|
								            $form->text('name')->required();
							 | 
						|
								            $form->text('sort')->default(255)->help('越小越靠前');
							 | 
						|
								//            $form->text('template');
							 | 
						|
								        })->saving(function (Form $form) {
							 | 
						|
											//不允许编辑的字段
							 | 
						|
											$form->ignore(['id', 'deleted_at']);
							 | 
						|
								
							 | 
						|
											$form->agent_id = Admin::user()->id;
							 | 
						|
											$form->sort = $form->sort ?? 255;
							 | 
						|
										})->deleting(function (Form $form) {
							 | 
						|
											//获取到要删除分类的ID
							 | 
						|
											$category_id = (int)$form->getKey();
							 | 
						|
								
							 | 
						|
											//获取要删除类目的所有下级ID
							 | 
						|
											$agent_id = Admin::user()->id;
							 | 
						|
											$category = Category::where('agent_id', $agent_id)->pluck('pid', 'id')->toArray();
							 | 
						|
											$ids = getChildCate($category_id, $category);
							 | 
						|
								
							 | 
						|
											if (AgentProduct::query()->where('agent_id', $agent_id)->whereIn('category_id', $ids)->exists()) {
							 | 
						|
												return $form->response()->error('该分类下已经发布产品,不允许删除');
							 | 
						|
											}
							 | 
						|
										});
							 | 
						|
								    }
							 | 
						|
								}
							 | 
						|
								
							 | 
						|
								//获取$pid下的所有子类目
							 | 
						|
								function getChildCate($pid, $category): array
							 | 
						|
								{
							 | 
						|
									$cate_arr = [$pid];
							 | 
						|
									foreach ($category as $k => $v) {
							 | 
						|
										if ($v == $pid) {
							 | 
						|
											$cate_arr = array_merge($cate_arr, getChildCate($k, $category));
							 | 
						|
										}
							 | 
						|
									}
							 | 
						|
									return $cate_arr;
							 | 
						|
								}
							 |