MenuController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. <?php
  2. namespace app\admin\controller\sys_manage;
  3. use app\admin\validate\sys_manage\MenuValidate;
  4. use app\common\Tree;
  5. use app\controller\Curd;
  6. use app\model\SysMenu;
  7. use app\model\SysRole;
  8. use app\model\SysUser;
  9. use support\Db;
  10. use support\exception\BusinessException;
  11. use support\Request;
  12. use support\Response;
  13. class MenuController extends Curd
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new SysMenu();
  18. $this->validate = true;
  19. $this->validateClass = new MenuValidate();
  20. }
  21. /**
  22. * @Desc 列表
  23. * @Author Gorden
  24. * @Date 2024/3/12 14:07
  25. *
  26. * @param Request $request
  27. * @return Response
  28. * @throws \support\exception\BusinessException
  29. */
  30. public function select(Request $request): Response
  31. {
  32. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  33. $format = $request->get('format', 'menu_tree');
  34. $order = $request->get('order', 'desc');
  35. $field = $field ?? 'menu_sort';
  36. $limit = 1000;
  37. $query = $this->doSelect($where, $field, $order);
  38. return $this->doFormat($query, $format, $limit);
  39. }
  40. public function save(Request $request)
  41. {
  42. $params = $request->post();
  43. $data = [
  44. 'menu_pid' => $params['parentId'],
  45. 'menu_icon' => $params['meta']['icon'] ?? '',
  46. 'menu_name' => $params['name'],
  47. 'menu_title' => $params['meta']['title'],
  48. 'menu_uri' => $params['redirect'] ?? '',
  49. 'menu_route' => $params['path'],
  50. 'menu_component' => $params['component'] ?? '',
  51. 'menu_sort' => $params['meta']['sort'] ?? 0,
  52. 'menu_is_show' => isset($params['meta']['hidden']) && $params['meta']['hidden'] ? 2 : 1,
  53. 'menu_is_menu' => $params['meta']['type'] == 'menu' ? 1 : 2,
  54. 'menu_status' => $params['meta']['status'] == true ? 1 : 2,
  55. // 'level' => $params['level'],
  56. ];
  57. $operate = $params['operate'];
  58. if ($operate == 'add') {
  59. if ($this->validate && !$this->validateClass->scene('add')->check($data)) {
  60. return json_fail($this->validateClass->getError());
  61. }
  62. try {
  63. $data = $this->inputFilter($data);
  64. $id = $this->doInsert($data);
  65. } catch (BusinessException $customException) {
  66. return json_fail($customException->getMessage());
  67. } catch (\Exception $exception) {
  68. return json_fail('数据写入失败');
  69. }
  70. // 写操作日志
  71. _syslog("添加菜单", "添加菜单【" . $data['menu_name'] . '】', $data);
  72. return json_success('success', ['id' => $id]);
  73. } else if ($operate == 'update') {
  74. $data['goods_id'] = $params['id'];
  75. $goodsId = $data['goods_id'];
  76. if ($this->validate && !$this->validateClass->scene('update')->check($data)) {
  77. return json_fail($this->validateClass->getError());
  78. }
  79. try {
  80. $data = $this->inputFilter($data);
  81. $this->doUpdate($goodsId, $data);
  82. } catch (BusinessException $customException) {
  83. return json_fail($customException->getMessage());
  84. } catch (\Exception $e) {
  85. return json_fail('数据更新失败');
  86. }
  87. // 记录日志
  88. _syslog("修改菜单", "修改菜单【" . $data['menu_title'] . '】', $data);
  89. return json_success('success', ['id' => $params['id']]);
  90. }
  91. return json_fail('请求失败~');
  92. }
  93. /**
  94. * @Desc 执行插入
  95. * @Author Gorden
  96. * @Date 2024/3/12 14:15
  97. *
  98. * @param array $data
  99. * @throws BusinessException\
  100. */
  101. protected function doInsert(array $data)
  102. {
  103. Db::beginTransaction();
  104. try {
  105. $primary_key = $this->model->getKeyName();
  106. $model_class = get_class($this->model);
  107. $model = new $model_class;
  108. foreach ($data as $key => $val) {
  109. $model->{$key} = $val;
  110. }
  111. $model->save();
  112. $prePath = '/0/';
  113. if ($model->menu_pid != 0) {
  114. $parentMenu = SysMenu::getParent($model->menu_pid);
  115. $prePath = $parentMenu->menu_path;
  116. }
  117. $model->menu_path = $prePath . $model->menu_id . '/';
  118. $model->save();
  119. Db::commit();
  120. return $model->menu_id;
  121. } catch (\Exception $e) {
  122. Db::rollBack();
  123. throw new BusinessException('数据写入失败~');
  124. }
  125. }
  126. /**
  127. * 执行更新
  128. * @param $id
  129. * @param $data
  130. * @return void
  131. */
  132. protected function doUpdate($id, $data)
  133. {
  134. $model = $this->model->find($id);
  135. foreach ($data as $key => $val) {
  136. $model->{$key} = $val;
  137. }
  138. if ($model->menu_pid != $data['menu_pid']) {
  139. $parentMenu = SysMenu::getParent($data['menu_pid']);
  140. $prePath = $parentMenu->menu_path;
  141. $model->menu_path = $prePath . $model->menu_id . '/';
  142. }
  143. $model->save();
  144. }
  145. protected function doFormat($query, $format, $limit): Response
  146. {
  147. $methods = [
  148. 'select' => 'formatSelect',
  149. 'tree' => 'formatTree',
  150. 'menu_tree' => 'formatMenuTree',
  151. 'table_tree' => 'formatTableTree',
  152. 'normal' => 'formatNormal',
  153. ];
  154. $paginator = $query->paginate($limit);
  155. $total = $paginator->total();
  156. $items = $paginator->items();
  157. if (method_exists($this, "afterQuery")) {
  158. $items = call_user_func([$this, "afterQuery"], $items);
  159. }
  160. $format_function = $methods[$format] ?? 'formatNormal';
  161. return call_user_func([$this, $format_function], $items, $total);
  162. }
  163. /**
  164. * @Desc 树形输出
  165. * @Author Gorden
  166. * @Date 2024/3/12 14:22
  167. *
  168. * @param $items
  169. * @return Response
  170. */
  171. protected function formatTree($items): Response
  172. {
  173. $format_items = [];
  174. foreach ($items as $item) {
  175. $format_items[] = [
  176. 'id' => $item->menu_id,
  177. 'pid' => $item->menu_pid,
  178. 'name' => $item->menu_name,
  179. 'path' => $item->menu_route,
  180. 'component' => $item->menu_component,
  181. 'meta' => [
  182. 'icon' => $item->menu_icon,
  183. 'title' => $item->menu_title,
  184. 'type' => $item->menu_is_menu == 1 ? 'menu' : 'button',
  185. 'status' => $item->menu_status == 1 ? true : false,
  186. 'sort' => $item->menu_sort,
  187. 'hidden' => $item->menu_is_show == 1 ? false : true,
  188. ]
  189. ];
  190. }
  191. $tree = new Tree($format_items);
  192. $menu = $tree->getTree();
  193. // foreach ($menu as &$item) {
  194. // unset($item['component']);
  195. // unset($item['redirect']);
  196. // }
  197. $data = [
  198. 'dashboardGrid' => ["welcome", "member", "sysManage", "about"],
  199. 'menu' => $menu,
  200. 'permissions' => ['list.add']
  201. ];
  202. return json_success('success', $data);
  203. }
  204. protected function formatMenuTree($items): Response
  205. {
  206. $format_items = [];
  207. foreach ($items as $item) {
  208. $format_items[] = [
  209. 'id' => $item->menu_id,
  210. 'pid' => $item->menu_pid,
  211. 'name' => $item->menu_name,
  212. 'path' => $item->menu_route,
  213. 'component' => $item->menu_component,
  214. 'meta' => [
  215. 'icon' => $item->menu_icon,
  216. 'title' => $item->menu_title,
  217. 'type' => $item->menu_is_menu == 1 ? 'menu' : 'button',
  218. 'status' => $item->menu_status == 1 ? true : false,
  219. 'sort' => $item->menu_sort,
  220. 'hidden' => $item->menu_is_show == 1 ? false : true,
  221. ]
  222. ];
  223. }
  224. $tree = new Tree($format_items);
  225. $menu = $tree->getTree();
  226. // foreach ($menu as &$item) {
  227. // unset($item['component']);
  228. // }
  229. return json_success('success', $menu);
  230. }
  231. public function myMenu(Request $request)
  232. {
  233. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  234. $adminId = \Tinywan\Jwt\JwtToken::getCurrentId();
  235. if (!$adminId) {
  236. throw new BusinessException("请先登录");
  237. }
  238. $user = SysUser::where('user_id', $adminId)->first();
  239. if (empty($user->join_user_role_id)) {
  240. throw new BusinessException("权限不足");
  241. }
  242. $role = SysRole::where('role_id', $user->join_user_role_id)->first();
  243. $permission = json_decode($role->role_permission,true);
  244. if (!empty($permission)){
  245. $where['menu_name'] = ['in', implode(',', $permission)];
  246. }else{
  247. throw new BusinessException("权限不足");
  248. }
  249. $format = $request->get('format', 'tree');
  250. $order = $request->get('order', 'desc');
  251. $field = $field ?? 'menu_sort';
  252. // $where['menu_is_show'] = 1;
  253. $where['menu_status'] = 1;
  254. $limit = 1000;
  255. $query = $this->doSelect($where, $field, $order);
  256. return $this->doFormat($query, $format, $limit);
  257. }
  258. /**
  259. * @Des
  260. * @Author Gorden
  261. * @Date 2024/3/14 10:02
  262. *
  263. * @param Request $request
  264. * @return Response
  265. */
  266. public function delete(Request $request): Response
  267. {
  268. Db::beginTransaction();
  269. try {
  270. $ids = $this->deleteInput($request);
  271. if (!$ids) {
  272. return json_fail('数据错误');
  273. }
  274. $menu = $this->model->whereIn('menu_id', $ids)->get();
  275. foreach ($menu as $item) {
  276. // 删菜单及子菜单
  277. $this->model->where('menu_path', 'like', $item->menu_path . '%')->delete();
  278. }
  279. Db::commit();
  280. // 记录日志
  281. $menu = $menu ? $menu->toArray() : [];
  282. _syslog("删除菜单", "删除菜单", $menu);
  283. return json_success('删除成功');
  284. } catch (\Exception $e) {
  285. Db::rollBack();
  286. return json_fail('删除失败');
  287. }
  288. }
  289. }