NavigationController.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. namespace App\Admin\Controllers\System;
  3. use App\Http\Controllers\Controller;
  4. use App\Models\Navigation;
  5. use App\Models\NavigationCategory;
  6. use App\Models\Page;
  7. use Encore\Admin\Controllers\HasResourceActions;
  8. use Encore\Admin\Facades\Admin;
  9. use Encore\Admin\Form;
  10. use Encore\Admin\Grid;
  11. use Encore\Admin\Layout\Content;
  12. use Encore\Admin\Show;
  13. use Illuminate\Support\Facades\Cache;
  14. class NavigationController extends Controller
  15. {
  16. use HasResourceActions;
  17. /**
  18. * Index interface.
  19. *
  20. * @param Content $content
  21. * @return Content
  22. */
  23. public function index(Content $content)
  24. {
  25. return $content
  26. ->header('导航列表')
  27. ->description('')
  28. ->body($this->grid());
  29. }
  30. /**
  31. * Show interface.
  32. *
  33. * @param mixed $id
  34. * @param Content $content
  35. * @return Content
  36. */
  37. public function show($id, Content $content)
  38. {
  39. return $content
  40. ->header('导航列表')
  41. ->description('详细')
  42. ->body($this->detail($id));
  43. }
  44. /**
  45. * Edit interface.
  46. *
  47. * @param mixed $id
  48. * @param Content $content
  49. * @return Content
  50. */
  51. public function edit($id, Content $content)
  52. {
  53. return $content
  54. ->header('导航列表')
  55. ->description('编辑')
  56. ->body($this->editForm($id)->edit($id));
  57. }
  58. /**
  59. * Create interface.
  60. *
  61. * @param Content $content
  62. * @return Content
  63. */
  64. public function create(Content $content)
  65. {
  66. return $content
  67. ->header('导航列表')
  68. ->description('创建')
  69. ->body($this->createForm());
  70. }
  71. /**
  72. * Make a grid builder.
  73. *
  74. * @return Grid
  75. */
  76. protected function grid()
  77. {
  78. $grid = new Grid(new Navigation);
  79. $grid->id('ID');
  80. $grid->alias('分类名称')->width(200);
  81. $grid->urltype('类型')->display(function ($urltype) {
  82. return $urltype ? '系统内' : '系统外';
  83. });
  84. $grid->display('是否显示')->display(function ($urltype) {
  85. return $urltype ? "<i class='fa fa-check' style='color:green'></i>" : "<i class='fa fa-close' style='color:red'></i>";
  86. });
  87. $grid->title('栏目名称')->width(200);
  88. $grid->target('打开方式')->display(function ($target) {
  89. if ($target=='_self') {
  90. return '原窗口';
  91. } else {
  92. return '新窗口';
  93. }
  94. });
  95. $grid->created_at('添加时间');
  96. $grid->updated_at('创建时间');
  97. $grid->actions(function ($actions) use ($grid) {
  98. if (Admin::user()->can('system_nav_list_edit')) {
  99. $actions->disableEdit(false);
  100. }
  101. if (Admin::user()->can('system_nav_list_delete')) {
  102. $actions->disableDelete(false);
  103. }
  104. });
  105. if (Admin::user()->can('system_nav_list_delete')) {
  106. $grid->tools(function ($tools) {
  107. $tools->batch(function ($batch) {
  108. $batch->disableDelete(false);
  109. });
  110. });
  111. $grid->disableRowSelector(false);
  112. }
  113. if (Admin::user()->can('system_nav_list_create')) {
  114. $grid->disableCreateButton(false);
  115. }
  116. return $grid;
  117. }
  118. /**
  119. * Make a show builder.
  120. *
  121. * @param mixed $id
  122. * @return Show
  123. */
  124. protected function detail($id)
  125. {
  126. $show = new Show(Navigation::findOrFail($id));
  127. $show->id('ID');
  128. $show->alias('分类名称');
  129. $show->urltype('类型')->as(function ($urltype) {
  130. return $urltype ? '系统内' : '系统外';
  131. });
  132. $show->display('是否显示')->as(function ($urltype) {
  133. return $urltype ? '显示' : '隐藏';
  134. });
  135. $show->title('栏目名称');
  136. $show->target('打开方式')->as(function ($target) {
  137. if ($target=='_self') {
  138. return '原窗口';
  139. } else {
  140. return '新窗口';
  141. }
  142. });
  143. $show->created_at('添加时间');
  144. $show->updated_at('创建时间');
  145. return $show;
  146. }
  147. /**
  148. * Make a form builder.
  149. *
  150. * @return Form
  151. */
  152. protected function editForm($id)
  153. {
  154. $form = new Form(new Navigation);
  155. $form->radio('urltype', '类型')->options([1 => '系统内',0=>'系统外'])->default(1);
  156. $form->select('alias', '分类名称')->options(
  157. NavigationCategory::List()->pluck('name', 'alias')
  158. )->rules([
  159. 'required',
  160. ])->setWidth(3)->setMustMark();
  161. $form->radio('display', '是否显示')->options([1=>'显示',0=>'隐藏'])->default(1);
  162. $form->display('title', '栏目名称')->setWidth(3);
  163. $form->color('color', '显示颜色')->setWidth(3);
  164. $form->select('pagealias', '页面名称')->options(
  165. Page::List()->pluck('pname', 'alias')
  166. )->load('tag', route('admin.sys.getTag'))->rules([
  167. 'required',
  168. ])->setWidth(3)->setMustMark();
  169. $form->text('tag', '导航关联')->setWidth(3);
  170. $form->text('url', '链接地址(路由)')->setWidth(3);
  171. $form->radio('target', '打开方式')->options(['_self'=>'原窗口','_blank'=>'新窗口'])->default('_self');
  172. $form->number('order', '排序')->min(0)->default(0);
  173. return $form;
  174. }
  175. protected function createForm()
  176. {
  177. $form = new Form(new Navigation);
  178. $form->radio('urltype', '类型')->options([1 => '系统内',0=>'系统外'])->default(1);
  179. $form->select('alias', '分类名称')->options(
  180. NavigationCategory::List()->pluck('name', 'alias')
  181. )->rules([
  182. 'required',
  183. ])->setWidth(3)->setMustMark();
  184. $form->radio('display', '是否显示')->options([1=>'显示',0=>'隐藏'])->default(1);
  185. $form->text('title', '栏目名称')->rules([
  186. 'required',
  187. ])->setWidth(3)->setMustMark();
  188. $form->color('color', '显示颜色');
  189. $form->select('pagealias', '页面名称')->options(
  190. Page::List()->pluck('pname', 'alias')
  191. )->load('tag', route('admin.sys.getTag'))->rules([
  192. 'required',
  193. ])->setWidth(3)->setMustMark();
  194. $form->select('tag', '导航关联')->rules([
  195. 'required',
  196. ])->setWidth(3)->setMustMark();
  197. $form->text('url', '链接地址(路由)')->setWidth(3);
  198. $form->radio('target', '打开方式')->options(['_self'=>'原窗口','_blank'=>'新窗口'])->default('_self');
  199. $form->number('order', '排序')->min(0)->default(0);
  200. return $form;
  201. }
  202. /**
  203. * Store a newly created resource in storage.
  204. *
  205. * @return mixed
  206. */
  207. public function store()
  208. {
  209. Cache::forget('nav_list');
  210. return $this->createForm()->store();
  211. }
  212. /**
  213. * Update the specified resource in storage.
  214. *
  215. * @param int $id
  216. *
  217. * @return \Illuminate\Http\Response|\Symfony\Component\HttpFoundation\Response
  218. */
  219. public function update($id)
  220. {
  221. Cache::forget('nav_list');
  222. return $this->editForm($id)->update($id);
  223. }
  224. protected function form()
  225. {
  226. $form = new Form(new Navigation);
  227. $form->display('ID');
  228. $form->display('添加时间');
  229. $form->display('更新时间');
  230. return $form;
  231. }
  232. }