SiteMenuController.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <?php
  2. namespace App\Distributor\Controllers;
  3. use App\Distributor\Actions\MenuShow;
  4. use App\Distributor\Repositories\DistProduct;
  5. use App\Distributor\Repositories\DistProductCategory;
  6. use App\Distributor\Repositories\SiteMenu;
  7. use App\Distributor\Repositories\SitePages;
  8. use App\Distributor\Repositories\SitePagesTag;
  9. use App\Libraries\CommonHelper;
  10. use Dcat\Admin\Form;
  11. use Dcat\Admin\Grid;
  12. use Dcat\Admin\Layout\Content;
  13. use Dcat\Admin\Admin;
  14. use Dcat\Admin\Tree;
  15. use Dcat\Admin\Layout\Row;
  16. use Dcat\Admin\Widgets\Box;
  17. use function Symfony\Component\String\u;
  18. use Dcat\Admin\Layout\Column;
  19. class SiteMenuController extends AdminDistController
  20. {
  21. /**
  22. * page index
  23. */
  24. public function index(Content $content)
  25. {
  26. $location = isset($_GET['location']) ? intval($_GET['location']) : 0;
  27. $header = admin_trans( 'admin.top_menu');
  28. if ($location == 1) {
  29. $header = admin_trans( 'admin.footer_menu');
  30. }
  31. return $content
  32. ->header($header)
  33. ->description('')
  34. ->body($this->tree($location));
  35. }
  36. private function tree($location)
  37. {
  38. $thisObject = $this;
  39. return function (Row $row) use ($location, $thisObject) {
  40. $tree = new Tree(new SiteMenu);
  41. //两层
  42. $tree->maxDepth(2);
  43. //标题显示
  44. $tree->branch(function ($branch) {
  45. $arr[] = $branch['title'];
  46. if ($branch['uri']) {
  47. $arr[] = $branch['uri'];
  48. }
  49. return implode(' - ', $arr);
  50. });
  51. //是否显示操作
  52. $tree->actions(function (Tree\Actions $actions) {
  53. $actions->prepend(new MenuShow());
  54. });
  55. //按钮
  56. $tree->disableEditButton();
  57. $tree->disableCreateButton();
  58. $tree->showQuickCreateButton();
  59. $tree->showQuickEditButton();
  60. $row->column(6, $tree);
  61. //增加JS,使新增与编辑带location
  62. $paramsUrl = 'location='.$location;
  63. CommonHelper::replaceAddEditerUrl('.tree-quick-create', '.tree-quick-edit', $paramsUrl);
  64. //主页权限
  65. $tree->query(function ($model) use ($location) {
  66. return $model->where('dist_id', getDistributorId())->where('menu_location', $location);
  67. });
  68. };
  69. }
  70. /**
  71. * Make a form builder.
  72. *
  73. * @return Form
  74. */
  75. protected function form()
  76. {
  77. $location = isset($_GET['location']) ? intval($_GET['location']) : 0;
  78. $thisObject = $this;
  79. return Form::make(new SiteMenu(), function (Form $form) use ($thisObject,$location) {
  80. $menuConfig = $form->model()->menu_config;
  81. //父ID
  82. $form->select('parent_id', admin_trans_label('parent_id'))
  83. ->options(SiteMenu::selectOptions(function ($query) use ($location) {
  84. $query = $query->where('menu_location',$location)->where('dist_id', getDistributorId())->orderBy('order', 'asc');
  85. return $query;
  86. }))->required();
  87. //标题
  88. $form->text('title')->required();
  89. //类型
  90. $form->select('menu_type')->options(config('dictionary.menu_type'))->required()
  91. ->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  92. //选择产品
  93. $form->select('collections_type')->options(config('dictionary.collections_type'))
  94. ->value($thisObject->getArrayValue('collections_type',$menuConfig))
  95. ->when(0, function (Form $form) use ($menuConfig,$thisObject) {
  96. $form->select('product_category')->options(DistProductCategory::selectOptions())->value($thisObject->getArrayValue('product_category',$menuConfig));
  97. })->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  98. $form->select('pages_tag')->options('api/tag')->value($thisObject->getArrayValue('posts_tag',$menuConfig));
  99. });
  100. })
  101. ->when(2, function (Form $form) use ($menuConfig,$thisObject) {
  102. //选择产品
  103. $form->select('product',admin_trans_label('select_product'))->options('api/products')
  104. ->value($thisObject->getArrayValue('product',$menuConfig))
  105. ->help('The latest 30 products are displayed by default, and you can also use the search function to find all products.');
  106. })
  107. ->when(3, function (Form $form) use ($menuConfig,$thisObject) {
  108. //选择页面
  109. $form->select('posts',admin_trans_label('select_pages'))->options('api/pages')
  110. ->value($thisObject->getArrayValue('posts',$menuConfig))
  111. ->help('The latest 30 published posts are displayed by default, and you can also use the search function to find all posts.');
  112. })
  113. ->when(4, function (Form $form) use ($menuConfig,$thisObject) {
  114. //url
  115. $form->url('url')->value($thisObject->getArrayValue('url',$menuConfig));
  116. });
  117. //显示
  118. $form->switch('show')->default(1);
  119. //隐藏字段
  120. $form->textarea('menu_config')->hideInDialog();
  121. $form->hidden('menu_location')->value($location);
  122. $form->hidden('uri');
  123. $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
  124. //以下字段不保存
  125. $form->ignore(['collections_type','product_category','pages_tag','product','posts','url']);
  126. //保存事件
  127. $form->submitted(function (Form $form) use ($thisObject) {
  128. $result = $thisObject->convertMenuConfig($form);
  129. $form->uri = $result['uri'];
  130. $form->menu_config = $result['menuConfig'];
  131. });
  132. //保存前,强制写死dist_id
  133. $form->saving(function (Form $form) {
  134. $form->dist_id =getDistributorId();
  135. });
  136. });
  137. }
  138. protected function convertMenuConfig(Form $form) {
  139. $uri = "";
  140. $menuConfig = [
  141. 'collections_type' => $form->input('collections_type'),
  142. 'product_category' => $form->input('product_category'),
  143. 'posts_tag' => $form->input('posts_tag'),
  144. 'product' => $form->input('product'),
  145. 'posts' => $form->input('posts'),
  146. 'url' => $form->input('url'),
  147. ];
  148. $menuType = $form->input('menu_type');
  149. switch ($menuType) {
  150. case 0: //选择产品
  151. $uri ="/";
  152. break;
  153. case 1: //集合
  154. if ($form->input('collections_type') == 0) {
  155. //产品分类
  156. $row = DistProductCategory::getOneById($form->input('product_category'));
  157. $uri = $row ? "/product-category/".$row->slug : "/";
  158. } else {
  159. //文章标签
  160. $row = SitePagesTag::getOneById($form->input('posts_tag'));
  161. $uri = $row ? "/collections/".$row->slug : "/";
  162. }
  163. break;
  164. case 2: //选择产品
  165. $row = DistProduct::getOneById($form->input('product'));
  166. $uri = $row ? "/products/".$row->slug : "/";
  167. break;
  168. case 3: //选择页面
  169. $row = SitePages::getOneById($form->input('posts'));
  170. $uri = $row ? "/pages/".$row->slug : "/";
  171. break;
  172. case 4: //url
  173. $uri = $form->input('url');
  174. break;
  175. }
  176. return ['uri'=>$uri,'menuConfig'=>$menuConfig];
  177. }
  178. protected function getArrayValue($key,$arr) {
  179. if (isset($arr[$key])) {
  180. return $arr[$key];
  181. }
  182. return "";
  183. }
  184. }