Menu.php 5.8 KB

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