header($header) ->description('') ->body($this->tree($location)); } private function tree($location) { $thisObject = $this; return function (Row $row) use ($location, $thisObject) { $tree = new Tree(new SiteMenu); //两层 $tree->maxDepth(2); //标题显示 $tree->branch(function ($branch) { $arr[] = $branch['title']; if ($branch['uri']) { $arr[] = $branch['uri']; } return implode(' - ', $arr); }); //是否显示操作 $tree->actions(function (Tree\Actions $actions) { $actions->prepend(new MenuShow()); }); //按钮 $tree->disableEditButton(); $tree->disableCreateButton(); $tree->showQuickCreateButton(); $tree->showQuickEditButton(); $row->column(6, $tree); //增加JS,使新增与编辑带location $paramsUrl = 'location='.$location; CommonHelper::replaceAddEditerUrl('.tree-quick-create', '.tree-quick-edit', $paramsUrl); //主页权限 $tree->query(function ($model) use ($location) { return $model->where('dist_id', getDistributorId())->where('menu_location', $location); }); }; } /** * Make a form builder. * * @return Form */ protected function form() { $location = isset($_GET['location']) ? intval($_GET['location']) : 0; $thisObject = $this; return Form::make(new SiteMenu(), function (Form $form) use ($thisObject,$location) { $menuConfig = $form->model()->menu_config; //父ID $form->select('parent_id', admin_trans_label('parent_id')) ->options(SiteMenu::selectOptions(function ($query) use ($location) { $query = $query->where('menu_location',$location)->where('dist_id', getDistributorId())->orderBy('order', 'asc'); return $query; }))->required(); //标题 $form->text('title')->required(); //类型 $form->select('menu_type')->options(config('dictionary.menu_type'))->required() ->when(1, function (Form $form) use ($menuConfig,$thisObject) { //选择产品 $form->select('collections_type')->options(config('dictionary.collections_type')) ->value($thisObject->getArrayValue('collections_type',$menuConfig)) ->when(0, function (Form $form) use ($menuConfig,$thisObject) { $form->select('product_category')->options(DistProductCategory::selectOptions())->value($thisObject->getArrayValue('product_category',$menuConfig)); })->when(1, function (Form $form) use ($menuConfig,$thisObject) { $form->select('pages_tag')->options('api/tag')->value($thisObject->getArrayValue('posts_tag',$menuConfig)); }); }) ->when(2, function (Form $form) use ($menuConfig,$thisObject) { //选择产品 $form->select('product',admin_trans_label('select_product'))->options('api/products') ->value($thisObject->getArrayValue('product',$menuConfig)) ->help('The latest 30 products are displayed by default, and you can also use the search function to find all products.'); }) ->when(3, function (Form $form) use ($menuConfig,$thisObject) { //选择页面 $form->select('posts',admin_trans_label('select_pages'))->options('api/pages') ->value($thisObject->getArrayValue('posts',$menuConfig)) ->help('The latest 30 published posts are displayed by default, and you can also use the search function to find all posts.'); }) ->when(4, function (Form $form) use ($menuConfig,$thisObject) { //url $form->url('url')->value($thisObject->getArrayValue('url',$menuConfig)); }); //显示 $form->switch('show')->default(1); //隐藏字段 $form->textarea('menu_config')->hideInDialog(); $form->hidden('menu_location')->value($location); $form->hidden('uri'); $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存 //以下字段不保存 $form->ignore(['collections_type','product_category','pages_tag','product','posts','url']); //保存事件 $form->submitted(function (Form $form) use ($thisObject) { $result = $thisObject->convertMenuConfig($form); $form->uri = $result['uri']; $form->menu_config = $result['menuConfig']; }); //保存前,强制写死dist_id $form->saving(function (Form $form) { $form->dist_id =getDistributorId(); }); }); } protected function convertMenuConfig(Form $form) { $uri = ""; $menuConfig = [ 'collections_type' => $form->input('collections_type'), 'product_category' => $form->input('product_category'), 'posts_tag' => $form->input('posts_tag'), 'product' => $form->input('product'), 'posts' => $form->input('posts'), 'url' => $form->input('url'), ]; $menuType = $form->input('menu_type'); switch ($menuType) { case 0: //选择产品 $uri ="/"; break; case 1: //集合 if ($form->input('collections_type') == 0) { //产品分类 $row = DistProductCategory::getOneById($form->input('product_category')); $uri = $row ? "/product-category/".$row->slug : "/"; } else { //文章标签 $row = SitePagesTag::getOneById($form->input('posts_tag')); $uri = $row ? "/collections/".$row->slug : "/"; } break; case 2: //选择产品 $row = DistProduct::getOneById($form->input('product')); $uri = $row ? "/products/".$row->slug : "/"; break; case 3: //选择页面 $row = SitePages::getOneById($form->input('posts')); $uri = $row ? "/pages/".$row->slug : "/"; break; case 4: //url $uri = $form->input('url'); break; } return ['uri'=>$uri,'menuConfig'=>$menuConfig]; } protected function getArrayValue($key,$arr) { if (isset($arr[$key])) { return $arr[$key]; } return ""; } }