MenuController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. $btnAuth = []; //按钮权限
  175. foreach ($items as $item) {
  176. if ($item->menu_is_menu == 2) {
  177. $btnAuth[] = $item->menu_name;
  178. }
  179. $format_items[] = [
  180. 'id' => $item->menu_id,
  181. 'pid' => $item->menu_pid,
  182. 'name' => $item->menu_name,
  183. 'path' => $item->menu_route,
  184. 'component' => $item->menu_component,
  185. 'meta' => [
  186. 'icon' => $item->menu_icon,
  187. 'title' => $item->menu_title,
  188. 'type' => $item->menu_is_menu == 1 ? 'menu' : 'button',
  189. 'status' => $item->menu_status == 1 ? true : false,
  190. 'sort' => $item->menu_sort,
  191. 'hidden' => $item->menu_is_show == 1 ? false : true,
  192. ]
  193. ];
  194. }
  195. $tree = new Tree($format_items);
  196. $menu = $tree->getTree();
  197. // foreach ($menu as &$item) {
  198. // unset($item['component']);
  199. // unset($item['redirect']);
  200. // }
  201. $data = [
  202. 'dashboardGrid' => ["welcome", "member", "sysManage", "about"],
  203. 'menu' => $menu,
  204. 'permissions' => $btnAuth //按钮权限
  205. ];
  206. return json_success('success', $data);
  207. }
  208. protected function formatMenuTree($items): Response
  209. {
  210. $format_items = [];
  211. foreach ($items as $item) {
  212. $format_items[] = [
  213. 'id' => $item->menu_id,
  214. 'pid' => $item->menu_pid,
  215. 'name' => $item->menu_name,
  216. 'path' => $item->menu_route,
  217. 'component' => $item->menu_component,
  218. 'meta' => [
  219. 'icon' => $item->menu_icon,
  220. 'title' => $item->menu_title,
  221. 'type' => $item->menu_is_menu == 1 ? 'menu' : 'button',
  222. 'status' => $item->menu_status == 1 ? true : false,
  223. 'sort' => $item->menu_sort,
  224. 'hidden' => $item->menu_is_show == 1 ? false : true,
  225. ]
  226. ];
  227. }
  228. $tree = new Tree($format_items);
  229. $menu = $tree->getTree();
  230. // foreach ($menu as &$item) {
  231. // unset($item['component']);
  232. // }
  233. return json_success('success', $menu);
  234. }
  235. public function myMenu(Request $request)
  236. {
  237. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  238. $adminId = \Tinywan\Jwt\JwtToken::getCurrentId();
  239. if (!$adminId) {
  240. throw new BusinessException("请先登录");
  241. }
  242. $user = SysUser::where('user_id', $adminId)->first();
  243. if (empty($user->join_user_role_id)) {
  244. throw new BusinessException("权限不足");
  245. }
  246. $role = SysRole::where('role_id', $user->join_user_role_id)->first();
  247. $permission = json_decode($role->role_permission,true);
  248. if (!empty($permission)){
  249. $where['menu_name'] = ['in', implode(',', $permission)];
  250. }else{
  251. throw new BusinessException("权限不足");
  252. }
  253. $format = $request->get('format', 'tree');
  254. $order = $request->get('order', 'desc');
  255. $field = $field ?? 'menu_sort';
  256. // $where['menu_is_show'] = 1;
  257. $where['menu_status'] = 1;
  258. $limit = 1000;
  259. $query = $this->doSelect($where, $field, $order);
  260. return $this->doFormat($query, $format, $limit);
  261. }
  262. /**
  263. * @Des
  264. * @Author Gorden
  265. * @Date 2024/3/14 10:02
  266. *
  267. * @param Request $request
  268. * @return Response
  269. */
  270. public function delete(Request $request): Response
  271. {
  272. Db::beginTransaction();
  273. try {
  274. $ids = $this->deleteInput($request);
  275. if (!$ids) {
  276. return json_fail('数据错误');
  277. }
  278. $menu = $this->model->whereIn('menu_id', $ids)->get();
  279. foreach ($menu as $item) {
  280. // 删菜单及子菜单
  281. $this->model->where('menu_path', 'like', $item->menu_path . '%')->delete();
  282. }
  283. Db::commit();
  284. // 记录日志
  285. $menu = $menu ? $menu->toArray() : [];
  286. _syslog("删除菜单", "删除菜单", $menu);
  287. return json_success('删除成功');
  288. } catch (\Exception $e) {
  289. Db::rollBack();
  290. return json_fail('删除失败');
  291. }
  292. }
  293. }