MenusController.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace App\Http\Controllers\Admin\Base;
  3. use App\Http\Controllers\Admin\Controller;
  4. use App\Services\Admin\Menus;
  5. use Request;
  6. class MenusController extends Controller
  7. {
  8. private $_service;
  9. public function __construct ()
  10. {
  11. if (!$this->_service) $this->_service = new Menus();
  12. }
  13. function index ()
  14. {
  15. $search['keyword'] = Request::input('keyword');
  16. $list = $this->_service->getMenusTree($search);
  17. return view('admin.base.menus.index', compact('list'));
  18. }
  19. public function _showEdit ()
  20. {
  21. $MenusTrees = $this->_service->getMenusTree();
  22. view()->share("MenusTrees", $MenusTrees);
  23. }
  24. /**
  25. * 添加
  26. */
  27. public function create ()
  28. {
  29. if (Request::method() == 'POST') {
  30. return $this->_createSave();
  31. }
  32. $this->_showEdit();
  33. return view('admin.base.menus.edit');
  34. }
  35. private function _createSave ()
  36. {
  37. $id = $this->_service->create(Request::input('data'));
  38. if (isset($id->id)) {
  39. //更新菜单层级关系
  40. $level = $this->_service->getLevel($id->id);
  41. $this->_service->update($id->id, ['level' => $level]);
  42. $url[] = array(
  43. 'url' => U( 'Base/Menus/index'),
  44. 'title' => '返回列表'
  45. );
  46. $url[] = array(
  47. 'url' => U( 'Base/Menus/create'),
  48. 'title' => '继续添加'
  49. );
  50. $this->showMessage('添加成功', $url);
  51. } else {
  52. $url[] = array(
  53. 'url' => U( 'Base/Menus/index'),
  54. 'title' => '返回列表'
  55. );
  56. $this->showWarning('添加失败', $url);
  57. }
  58. }
  59. /**
  60. * 修改
  61. */
  62. public function update ()
  63. {
  64. if (Request::method() == 'POST') {
  65. return $this->_updateSave();
  66. }
  67. $this->_showEdit();
  68. $data = $this->_service->find(Request::input('id'));
  69. return view('admin.base.menus.edit', compact('data'));
  70. }
  71. private function _updateSave ()
  72. {
  73. $data = Request::input('data');
  74. $data['level'] = $this->_service->getLevel(Request::input('id'));
  75. $ok = $this->_service->update(Request::input('id'), $data);
  76. if ($ok) {
  77. $url[] = array(
  78. 'url' => U( 'Base/Menus/index'),
  79. 'title' => '返回列表'
  80. );
  81. $this->showMessage('操作成功', urldecode(Request::input('_referer')));
  82. } else {
  83. $url[] = array(
  84. 'url' => U( 'Base/Menus/index'),
  85. 'title' => '返回列表'
  86. );
  87. $this->showWarning('操作失败', $url);
  88. }
  89. }
  90. /**
  91. * 删除
  92. */
  93. public function destroy ()
  94. {
  95. $bool = $this->_service->destroy(Request::input('id'));
  96. if ($bool) {
  97. $this->showMessage('操作成功');
  98. } else {
  99. $this->showWarning("操作失败");
  100. }
  101. }
  102. }