Menu.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace app\admin\controller;
  3. use app\BaseController;
  4. use app\admin\model\Menu as MenuModel;
  5. use app\admin\model\RoleMenu;
  6. use app\admin\model\Admin;
  7. use app\admin\validate\MenuValidate;
  8. use think\facade\Db;
  9. class Menu extends BaseController
  10. {
  11. /**
  12. * @api {get} /menu/list 查询菜单列表
  13. */
  14. public function list()
  15. {
  16. try {
  17. $count = MenuModel::count();
  18. $list = MenuModel::order('sort', 'asc')
  19. ->order('id', 'asc')
  20. ->select();
  21. $list = linear_to_tree($list, 'children');
  22. } catch (\Exception $e) {
  23. return $this->error($e->getMessage());
  24. }
  25. return $this->success(['count' => $count, 'list' => $list]);
  26. }
  27. /**
  28. * @api {get} /menu/route 获取菜单路由
  29. */
  30. public function route()
  31. {
  32. try {
  33. $adminId = $this->admin_id;
  34. // 缓存优化
  35. $list = cache('MenuLogic:getMenuByAdminId:'.$adminId);
  36. if(empty($list)){
  37. $admin = Admin::where('id',$adminId)->find();
  38. $where = [];
  39. $where[] = ['type', 'in', [1,2]];
  40. $where[] = ['is_disable', '=', 0];
  41. if ($adminId != 1) {
  42. $roleMenu = RoleMenu::whereIn('role_id', $admin['role_id'])->column('menu_id');
  43. $where[] = ['id', 'in', $roleMenu];
  44. }
  45. $menu = MenuModel::where($where)
  46. ->order(['sort' => 'desc', 'id' => 'asc'])
  47. ->select();
  48. $list = linear_to_tree($menu, 'children');
  49. cache('MenuLogic:getMenuByAdminId:'.$adminId,$list);
  50. }
  51. } catch (\Exception $e) {
  52. return $this->error($e->getMessage());
  53. }
  54. return $this->success($list);
  55. }
  56. /**
  57. * @api {get} /menu/info 菜单详情
  58. */
  59. public function info()
  60. {
  61. try {
  62. $params = (new MenuValidate())->goCheck('id');
  63. $detail = MenuModel::where('id', $params['id'])->find();
  64. if(empty($detail)){
  65. return $this->error('菜单不存在');
  66. }
  67. } catch (\Exception $e) {
  68. return $this->error($e->getMessage());
  69. }
  70. return $this->success($detail);
  71. }
  72. /**
  73. * @api {post} /menu/update 添加/修改
  74. * @apiGroup 菜单
  75. * @apiVersion 1.0.0
  76. * @apiUse header
  77. * @apiUse lang
  78. *
  79. * @apiParam {Integer} parent_id 父级ID
  80. * @apiParam {String} type 类型 M:菜单 C:按钮
  81. * @apiParam {String} name 名称
  82. * @apiParam {String} icon 图标
  83. * @apiParam {Integer} sort 排序
  84. * @apiParam {String} perms 权限
  85. * @apiParam {String} paths 路径
  86. * @apiParam {String} component 组件
  87. * @apiParam {String} params 参数
  88. * @apiParam {Integer} is_cache 是否缓存:1是 0否,默认0
  89. * @apiParam {Integer} status 状态: 1正常 0停用,默认1
  90. */
  91. public function update()
  92. {
  93. Db::startTrans();
  94. try {
  95. $params = (new MenuValidate)->post()->goCheck('edit');
  96. $id = $this->request->param('id',0);
  97. $params['parent_id'] = $params['parent_id'] ?? 0;
  98. $params['is_cache'] = $params['is_cache'] ?? 0;
  99. $params['status'] = $params['status'] ?? 1;
  100. $params['sort'] = $params['sort'] ?? 0;
  101. if (!$id) {
  102. MenuModel::create($params);
  103. } else {
  104. MenuModel::where('id', $id)->update($params);
  105. }
  106. Db::commit();
  107. } catch (\Exception $e) {
  108. Db::rollBack();
  109. return $this->error($e->getMessage());
  110. }
  111. return $this->success();
  112. }
  113. //删除
  114. public function delete()
  115. {
  116. try {
  117. $params = (new MenuValidate())->goCheck('id');
  118. MenuModel::where('id', $params['id'])->delete();
  119. } catch (\Exception $e) {
  120. return $this->error($e->getMessage());
  121. }
  122. return $this->success();
  123. }
  124. }