SiteMenuController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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\DistVideoCategory;
  7. use App\Distributor\Repositories\SiteMenu;
  8. use App\Distributor\Repositories\SitePages;
  9. use App\Distributor\Repositories\SitePagesTag;
  10. use App\Libraries\CommonHelper;
  11. use Dcat\Admin\Form;
  12. use Dcat\Admin\Grid;
  13. use Dcat\Admin\Layout\Content;
  14. use Dcat\Admin\Admin;
  15. use Dcat\Admin\Tree;
  16. use Dcat\Admin\Layout\Row;
  17. use Dcat\Admin\Widgets\Box;
  18. use function Symfony\Component\String\u;
  19. use Dcat\Admin\Layout\Column;
  20. class SiteMenuController extends AdminDistController
  21. {
  22. /**
  23. * page index
  24. */
  25. public function index(Content $content)
  26. {
  27. $location = isset($_GET['location']) ? intval($_GET['location']) : 0;
  28. //保存临时变量
  29. setTempValue('location', $location);
  30. //翻译
  31. $header = admin_trans( 'admin.top_menu');
  32. if ($location == 1) {
  33. $header = admin_trans( 'admin.footer_menu');
  34. }
  35. return $content
  36. ->header($header)
  37. ->description('')
  38. ->body($this->tree($location));
  39. }
  40. private function tree($location)
  41. {
  42. $thisObject = $this;
  43. return function (Row $row) use ($location, $thisObject) {
  44. $tree = new Tree(new SiteMenu);
  45. //两层
  46. $tree->maxDepth(2);
  47. //标题显示
  48. $tree->branch(function ($branch) {
  49. return $branch['title'];
  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. //$menuConfig = json_decode($menuConfig,true);
  82. //父ID
  83. $form->select('parent_id', admin_trans_label('parent_id'))
  84. ->options(SiteMenu::selectOptions(function ($query) use ($location) {
  85. $query = $query->where('menu_location',$location)->where('dist_id', getDistributorId())->orderBy('order', 'asc');
  86. return $query;
  87. }))->required();
  88. //标题
  89. $form->text('title')->required();
  90. //图片
  91. $form->image("image_url", admin_trans_label('images'))
  92. ->retainable()//禁止删OSS图
  93. ->autoUpload()
  94. ->uniqueName()
  95. ->accept(config('distributor.upload.oss_image.accept'))
  96. ->maxSize(config('distributor.upload.oss_image.max_size'))
  97. ->dir(config("distributor.upload.directory.image").'/menu/'.date("Ymd"));
  98. //类型
  99. $form->select('menu_type')->options(admin_trans_array(config('dictionary.menu_type')))->required()
  100. ->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  101. //选择集合类型
  102. $form->select('collections_type')->options(admin_trans_array(config('dictionary.collections_type')))
  103. ->value($thisObject->getArrayValue('collections_type',$menuConfig))
  104. ->when(0, function (Form $form) use ($menuConfig,$thisObject) {
  105. //产品分类
  106. $form->select('product_category')->options(DistProductCategory::selectOptions())->value($thisObject->getArrayValue('product_category',$menuConfig));
  107. })->when(1, function (Form $form) use ($menuConfig,$thisObject) {
  108. //文章标签
  109. $form->select('pages_tag')->options('api/tag')->value($thisObject->getArrayValue('pages_tag',$menuConfig));
  110. })->when(2, function (Form $form) use ($menuConfig,$thisObject) {
  111. //视频
  112. $form->select('video_category')->options(DistVideoCategory::selectOptions())->value($thisObject->getArrayValue('video_category',$menuConfig));
  113. });
  114. })
  115. ->when(2, function (Form $form) use ($menuConfig,$thisObject) {
  116. //选择产品
  117. $form->select('product',admin_trans_label('select_product'))->options('api/products')
  118. ->value($thisObject->getArrayValue('product',$menuConfig))
  119. ->help('The latest 30 products are displayed by default, and you can also use the search function to find all products.');
  120. })
  121. ->when(3, function (Form $form) use ($menuConfig,$thisObject) {
  122. //选择页面
  123. $form->select('pages',admin_trans_label('select_pages'))->options('api/pages')
  124. ->value($thisObject->getArrayValue('pages',$menuConfig))
  125. ->help('The latest 30 published posts are displayed by default, and you can also use the search function to find all posts.');
  126. })
  127. ->when(10, function (Form $form) use ($menuConfig,$thisObject) {
  128. //url
  129. $form->text('url')->value($thisObject->getArrayValue('url',$menuConfig));
  130. })
  131. ->when(4, function (Form $form) use ($menuConfig,$thisObject) {
  132. $form->select('landing_page',admin_trans_label('landing_page'))->options('api/landing-pages')
  133. ->value($thisObject->getArrayValue('landing_page',$menuConfig));
  134. })->when(5, function (Form $form) use ($menuConfig,$thisObject) {
  135. $form->select('videos',admin_trans_label('videos'))->options('api/videos')
  136. ->value($thisObject->getArrayValue('videos',$menuConfig));
  137. });
  138. //显示
  139. $form->switch('show')->default(1);
  140. //隐藏字段
  141. $form->textarea('menu_config')->hideInDialog();
  142. $form->hidden('menu_location')->value($location);
  143. $form->hidden('uri');
  144. $form->hidden('dist_id'); // 隐藏dist_id字段,用于保存
  145. //以下字段不保存
  146. $form->ignore(['collections_type','product_category','pages_tag','product','pages','url','landing_page','videos','video_category']);
  147. //保存事件
  148. $form->submitted(function (Form $form) use ($thisObject) {
  149. $result = $thisObject->convertMenuConfig($form);
  150. $form->uri = $result['uri'];
  151. $form->menu_config = $result['menuConfig'];
  152. });
  153. //保存前,强制写死dist_id
  154. $form->saving(function (Form $form) {
  155. $form->dist_id =getDistributorId();
  156. });
  157. });
  158. }
  159. protected function convertMenuConfig(Form $form) {
  160. $menuConfig = [
  161. 'collections_type' => $form->input('collections_type'),
  162. 'product_category' => $form->input('product_category'),
  163. 'video_category' => $form->input('video_category'),
  164. 'pages_tag' => $form->input('pages_tag'),
  165. 'product' => $form->input('product'),
  166. 'pages' => $form->input('pages'),
  167. 'url' => $form->input('url'),
  168. 'landing_page' => $form->input('landing_page'),
  169. 'videos' => $form->input('videos'),
  170. ];
  171. $menuType = $form->input('menu_type');
  172. $uri = SiteMenu::generateUri($menuType,$menuConfig);
  173. return ['uri'=>$uri,'menuConfig'=>$menuConfig];
  174. }
  175. protected function getArrayValue($key,$arr) {
  176. if (isset($arr[$key])) {
  177. return $arr[$key];
  178. }
  179. return "";
  180. }
  181. /*
  182. * 重写store
  183. */
  184. public function store()
  185. {
  186. $location = getTempValue('location');
  187. return $this->form()->store(null,'/site-menu?location='.$location);
  188. }
  189. }