Menu.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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\common\model\SystemMenu;
  13. use app\common\model\SystemNode;
  14. use app\admin\service\TriggerService;
  15. use app\common\constants\MenuConstant;
  16. use EasyAdmin\annotation\ControllerAnnotation;
  17. use EasyAdmin\annotation\NodeAnotation;
  18. use app\common\controller\AdminController;
  19. use think\App;
  20. /**
  21. * Class Menu
  22. * @package app\admin\controller\system
  23. * @ControllerAnnotation(title="菜单管理",auth=true)
  24. */
  25. class Menu extends AdminController
  26. {
  27. use \app\admin\traits\Curd;
  28. protected $sort = [
  29. 'sort' => 'desc',
  30. 'id' => 'asc',
  31. ];
  32. public function __construct(App $app)
  33. {
  34. parent::__construct($app);
  35. $this->model = new SystemMenu();
  36. }
  37. /**
  38. * @NodeAnotation(title="列表")
  39. */
  40. public function index()
  41. {
  42. if ($this->request->isAjax()) {
  43. if (input('selectFields')) {
  44. return $this->selectList();
  45. }
  46. $count = $this->model->count();
  47. $list = $this->model->order($this->sort)->select();
  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. try {
  80. $save = $this->model->save($post);
  81. } catch (\Exception $e) {
  82. $this->error('保存失败');
  83. }
  84. if ($save) {
  85. TriggerService::updateMenu();
  86. $this->success('保存成功');
  87. } else {
  88. $this->error('保存失败');
  89. }
  90. }
  91. $pidMenuList = $this->model->getPidMenuList();
  92. $this->assign('id', $id);
  93. $this->assign('pidMenuList', $pidMenuList);
  94. return $this->fetch();
  95. }
  96. /**
  97. * @NodeAnotation(title="编辑")
  98. */
  99. public function edit($id)
  100. {
  101. $row = $this->model->find($id);
  102. empty($row) && $this->error('数据不存在');
  103. if ($this->request->isPost()) {
  104. $post = $this->request->post();
  105. $rule = [
  106. 'pid|上级菜单' => 'require',
  107. 'title|菜单名称' => 'require',
  108. 'icon|菜单图标' => 'require',
  109. ];
  110. $this->validate($post, $rule);
  111. try {
  112. $save = $row->save($post);
  113. } catch (\Exception $e) {
  114. $this->error('保存失败');
  115. }
  116. if ($save) {
  117. TriggerService::updateMenu();
  118. $this->success('保存成功');
  119. } else {
  120. $this->error('保存失败');
  121. }
  122. }
  123. $pidMenuList = $this->model->getPidMenuList();
  124. $this->assign([
  125. 'id' => $id,
  126. 'pidMenuList' => $pidMenuList,
  127. 'row' => $row,
  128. ]);
  129. return $this->fetch();
  130. }
  131. /**
  132. * @NodeAnotation(title="删除")
  133. */
  134. public function delete($id)
  135. {
  136. $this->checkPostRequest();
  137. $row = $this->model->whereIn('id', $id)->select();
  138. empty($row) && $this->error('数据不存在');
  139. try {
  140. $save = $row->delete();
  141. } catch (\Exception $e) {
  142. $this->error('删除失败');
  143. }
  144. if ($save) {
  145. TriggerService::updateMenu();
  146. $this->success('删除成功');
  147. } else {
  148. $this->error('删除失败');
  149. }
  150. }
  151. /**
  152. * @NodeAnotation(title="属性修改")
  153. */
  154. public function modify()
  155. {
  156. $this->checkPostRequest();
  157. $post = $this->request->post();
  158. $rule = [
  159. 'id|ID' => 'require',
  160. 'field|字段' => 'require',
  161. 'value|值' => 'require',
  162. ];
  163. $this->validate($post, $rule);
  164. $row = $this->model->find($post['id']);
  165. if (!$row) {
  166. $this->error('数据不存在');
  167. }
  168. if (!in_array($post['field'], $this->allowModifyFields)) {
  169. $this->error('该字段不允许修改:' . $post['field']);
  170. }
  171. $homeId = $this->model
  172. ->where([
  173. 'pid' => MenuConstant::HOME_PID,
  174. ])
  175. ->value('id');
  176. if ($post['id'] == $homeId && $post['field'] == 'status') {
  177. $this->error('首页状态不允许关闭');
  178. }
  179. try {
  180. $row->save([
  181. $post['field'] => $post['value'],
  182. ]);
  183. } catch (\Exception $e) {
  184. $this->error($e->getMessage());
  185. }
  186. TriggerService::updateMenu();
  187. $this->success('保存成功');
  188. }
  189. /**
  190. * @NodeAnotation(title="添加菜单提示")
  191. */
  192. public function getMenuTips()
  193. {
  194. $node = input('get.keywords');
  195. $list = SystemNode::whereLike('node', "%{$node}%")
  196. ->field('node,title')
  197. ->limit(10)
  198. ->select();
  199. return json([
  200. 'code' => 0,
  201. 'content' => $list,
  202. 'type' => 'success',
  203. ]);
  204. }
  205. }