Menu.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller\system;
  12. use app\admin\logic\SystemMenuLogic;
  13. use app\common\model\SystemMenu;
  14. use app\common\model\SystemNode;
  15. use app\admin\service\TriggerService;
  16. use app\common\constants\MenuConstant;
  17. use EasyAdmin\annotation\ControllerAnnotation;
  18. use EasyAdmin\annotation\NodeAnotation;
  19. use app\common\controller\AdminController;
  20. use think\App;
  21. /**
  22. * Class Menu
  23. * @package app\admin\controller\system
  24. * @ControllerAnnotation(title="菜单管理",auth=true)
  25. */
  26. class Menu extends AdminController
  27. {
  28. use \app\admin\traits\Curd;
  29. protected $sort = [
  30. 'sort' => 'desc',
  31. 'id' => 'asc',
  32. ];
  33. public function __construct(App $app)
  34. {
  35. parent::__construct($app);
  36. $this->model = new SystemMenu();
  37. }
  38. /**
  39. * @NodeAnotation(title="列表")
  40. */
  41. public function index()
  42. {
  43. if ($this->request->isAjax()) {
  44. if (input('selectFields')) {
  45. return $this->selectList();
  46. }
  47. list($count, $list) = SystemMenuLogic::getList($this->sort);
  48. $data = [
  49. 'code' => 0,
  50. 'msg' => '',
  51. 'count' => $count,
  52. 'data' => $list,
  53. ];
  54. return json($data);
  55. }
  56. return $this->fetch();
  57. }
  58. /**
  59. * @NodeAnotation(title="添加")
  60. */
  61. public function add($id = null)
  62. {
  63. $homeId = $this->model
  64. ->where([
  65. 'pid' => MenuConstant::HOME_PID,
  66. ])
  67. ->value('id');
  68. if ($id == $homeId) {
  69. $this->error('首页不能添加子菜单');
  70. }
  71. if ($this->request->isPost()) {
  72. $post = $this->request->post();
  73. $rule = [
  74. 'pid|上级菜单' => 'require',
  75. 'title|菜单名称' => 'require',
  76. 'icon|菜单图标' => 'require',
  77. ];
  78. $this->validate($post, $rule);
  79. $result = SystemMenuLogic::add($post);
  80. $result === true ? $this->success('保存成功') : $this->error('保存失败');
  81. }
  82. $pidMenuList = $this->model->getPidMenuList();
  83. $this->assign('id', $id);
  84. $this->assign('pidMenuList', $pidMenuList);
  85. return $this->fetch();
  86. }
  87. /**
  88. * @NodeAnotation(title="编辑")
  89. */
  90. public function edit($id)
  91. {
  92. $row = $this->model->find($id);
  93. empty($row) && $this->error('数据不存在');
  94. if ($this->request->isPost()) {
  95. $post = $this->request->post();
  96. $rule = [
  97. 'pid|上级菜单' => 'require',
  98. 'title|菜单名称' => 'require',
  99. 'icon|菜单图标' => 'require',
  100. ];
  101. $this->validate($post, $rule);
  102. $result = SystemMenuLogic::edit($id, $post);
  103. $result === true ? $this->success('保存成功') : $this->error('保存失败');
  104. }
  105. $pidMenuList = $this->model->getPidMenuList();
  106. $this->assign([
  107. 'id' => $id,
  108. 'pidMenuList' => $pidMenuList,
  109. 'row' => $row,
  110. ]);
  111. return $this->fetch();
  112. }
  113. /**
  114. * @NodeAnotation(title="删除")
  115. */
  116. public function delete($id)
  117. {
  118. $this->checkPostRequest();
  119. $row = $this->model->whereIn('id', $id)->select();
  120. empty($row) && $this->error('数据不存在');
  121. try {
  122. $save = $row->delete();
  123. } catch (\Exception $e) {
  124. $this->error('删除失败');
  125. }
  126. if ($save) {
  127. TriggerService::updateMenu();
  128. $this->success('删除成功');
  129. } else {
  130. $this->error('删除失败');
  131. }
  132. }
  133. /**
  134. * @NodeAnotation(title="属性修改")
  135. */
  136. public function modify()
  137. {
  138. $this->checkPostRequest();
  139. $post = $this->request->post();
  140. $rule = [
  141. 'id|ID' => 'require',
  142. 'field|字段' => 'require',
  143. 'value|值' => 'require',
  144. ];
  145. $this->validate($post, $rule);
  146. $row = $this->model->find($post['id']);
  147. if (!$row) {
  148. $this->error('数据不存在');
  149. }
  150. if (!in_array($post['field'], $this->allowModifyFields)) {
  151. $this->error('该字段不允许修改:' . $post['field']);
  152. }
  153. $homeId = $this->model
  154. ->where([
  155. 'pid' => MenuConstant::HOME_PID,
  156. ])
  157. ->value('id');
  158. if ($post['id'] == $homeId && $post['field'] == 'status') {
  159. $this->error('首页状态不允许关闭');
  160. }
  161. try {
  162. $row->save([
  163. $post['field'] => $post['value'],
  164. ]);
  165. } catch (\Exception $e) {
  166. $this->error($e->getMessage());
  167. }
  168. TriggerService::updateMenu();
  169. $this->success('保存成功');
  170. }
  171. /**
  172. * @NodeAnotation(title="添加菜单提示")
  173. */
  174. public function getMenuTips()
  175. {
  176. $node = input('get.keywords');
  177. $list = SystemNode::whereLike('node', "%{$node}%")
  178. ->field('node,title')
  179. ->limit(10)
  180. ->select();
  181. return json([
  182. 'code' => 0,
  183. 'content' => $list,
  184. 'type' => 'success',
  185. ]);
  186. }
  187. }