MenusController.php 3.1 KB

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