Menu.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. <?php
  2. namespace app\controller\admin;
  3. use app\service\admin\admin\UserServiceFacade;
  4. use laytp\controller\Backend;
  5. use laytp\library\CommonFun;
  6. use laytp\library\Tree;
  7. use think\facade\Config;
  8. /**
  9. * 菜单控制器
  10. */
  11. class Menu extends Backend
  12. {
  13. public $noNeedAuth = ['getMenuTree', 'getTree'];
  14. public $model;
  15. public $orderRule = ['sort' => 'desc', 'id' => 'asc'];
  16. public function _initialize()
  17. {
  18. $this->model = new \app\model\admin\Menu();
  19. }
  20. public function index()
  21. {
  22. global $_W;
  23. $where = $this->buildSearchParams(false);
  24. // $where[] = ['uniacid','=',$_W['uniacid']];
  25. $order = $this->buildOrder();
  26. $sourceData = $this->model->where($where)->order($order);
  27. $isTree = $this->request->param('is_tree');
  28. if ($isTree) {
  29. $menuTreeObj = Tree::instance();
  30. $menuTreeObj->init($sourceData->select()->toArray());
  31. $data = $menuTreeObj->getRootTrees();
  32. } else {
  33. $paging = $this->request->param('paging', false);
  34. if ($paging) {
  35. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  36. $data = $sourceData->paginate($limit)->toArray();
  37. $data['data'] = $this->getSelectedData($data['data']);
  38. } else {
  39. $data = $sourceData->select()->toArray();
  40. }
  41. }
  42. return $this->success('数据获取成功', $data);
  43. }
  44. //获取当前登录者的权限列表,返回树形数据,角色管理赋予权限时用到
  45. public function getTree()
  46. {
  47. $user = UserServiceFacade::getUser();
  48. if ($user->is_super_manager === 1) {
  49. $sourceData = $this->model->order($this->orderRule)->select()->toArray();
  50. } else {
  51. $roleIds = \app\model\admin\role\User::where('admin_user_id', '=', $user->id)
  52. ->column('admin_role_id');
  53. $menuIds = \app\model\admin\menu\Role::where('admin_role_id', 'in', $roleIds)
  54. ->column('admin_menu_id');
  55. $where[] = ['id', 'in', $menuIds];
  56. $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
  57. }
  58. $menuTreeObj = Tree::instance();
  59. $menuTreeObj->init($sourceData);
  60. //由列表数据转化成树形结构数据
  61. $data = $menuTreeObj->getRootTrees();
  62. return $this->success('获取成功', $data);
  63. }
  64. //获取当前登录者的菜单列表,返回树形数据,仅返回is_menu=1的列表,后台菜单列表展示使用
  65. public function getMenuTree()
  66. {
  67. $user = UserServiceFacade::getUser();
  68. $where[] = ['is_show', '=', 1];
  69. $where[] = ['is_menu', '=', 1];
  70. if ($user->is_super_manager === 1) {
  71. $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
  72. } else {
  73. $roleIds = \app\model\admin\role\User::where('admin_user_id', '=', $user->id)
  74. ->column('admin_role_id');
  75. $menuIds = \app\model\admin\menu\Role::where('admin_role_id', 'in', $roleIds)
  76. ->column('admin_menu_id');
  77. $where[] = ['id', 'in', $menuIds];
  78. $sourceData = $this->model->order($this->orderRule)->where($where)->select()->toArray();
  79. }
  80. $menuTreeObj = Tree::instance();
  81. $menuTreeObj->init($sourceData);
  82. //由列表数据转化成树形结构数据
  83. $data = $menuTreeObj->getRootTrees();
  84. return $this->success('获取成功', $data);
  85. }
  86. //添加
  87. public function add()
  88. {
  89. $post = CommonFun::filterPostData($this->request->post());
  90. if ($post['rule'] && substr($post['rule'], 0, 1) != '/') {
  91. $post['rule'] = '/' . $post['rule'];
  92. }
  93. if ($this->model->create($post)) {
  94. return $this->success('添加成功', $post);
  95. } else {
  96. return $this->error('操作失败');
  97. }
  98. }
  99. //编辑
  100. public function edit()
  101. {
  102. $id = $this->request->param('id');
  103. $info = $this->model->find($id);
  104. $post = CommonFun::filterPostData($this->request->post());
  105. if ($post['rule'] && substr($post['rule'], 0, 1) != '/') {
  106. $post['rule'] = '/' . $post['rule'];
  107. }
  108. if ($id == $post['pid']) {
  109. return $this->error('不能将上级改成自己');
  110. }
  111. foreach ($post as $k => $v) {
  112. $info->$k = $v;
  113. }
  114. $update_res = $info->save();
  115. if ($update_res) {
  116. return $this->success('操作成功');
  117. } else if ($update_res === 0) {
  118. return $this->success('未做修改');
  119. } else {
  120. return $this->error('操作失败');
  121. }
  122. }
  123. //删除
  124. public function del()
  125. {
  126. $ids = $this->request->post('ids');
  127. if (!$ids) {
  128. return $this->error('参数ids不能为空');
  129. }
  130. $sourceData = $this->model->select()->toArray();
  131. $treeLib = Tree::instance();
  132. $treeLib->init($sourceData);
  133. $childIds = $treeLib->getChildIds($ids);
  134. if ($this->model->destroy($childIds)) {
  135. return $this->success('数据删除成功', $childIds);
  136. } else {
  137. return $this->error('数据删除失败');
  138. }
  139. }
  140. //设置排序
  141. public function setSort()
  142. {
  143. $id = $this->request->post('id');
  144. $fieldVal = $this->request->post('field_val');
  145. $isRecycle = $this->request->post('is_recycle');
  146. $update['sort'] = $fieldVal;
  147. try {
  148. if ($isRecycle) {
  149. $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
  150. } else {
  151. $updateRes = $this->model->where('id', '=', $id)->update($update);
  152. }
  153. if ($updateRes) {
  154. return $this->success('操作成功');
  155. } else if ($updateRes === 0) {
  156. return $this->success('未作修改');
  157. } else {
  158. return $this->error('操作失败');
  159. }
  160. } catch (\Exception $e) {
  161. return $this->exceptionError($e);
  162. }
  163. }
  164. //设置是否为菜单
  165. public function setIsMenu()
  166. {
  167. $id = $this->request->post('id');
  168. $fieldVal = $this->request->post('field_val');
  169. $isRecycle = $this->request->post('is_recycle');
  170. $update['is_menu'] = $fieldVal;
  171. try {
  172. if ($isRecycle) {
  173. $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
  174. } else {
  175. $updateRes = $this->model->where('id', '=', $id)->update($update);
  176. }
  177. if ($updateRes) {
  178. return $this->success('操作成功');
  179. } else if ($updateRes === 0) {
  180. return $this->success('未作修改');
  181. } else {
  182. return $this->error('操作失败');
  183. }
  184. } catch (\Exception $e) {
  185. return $this->exceptionError($e);
  186. }
  187. }
  188. //设置是否显示
  189. public function setIsShow()
  190. {
  191. $id = $this->request->post('id');
  192. $fieldVal = $this->request->post('field_val');
  193. $isRecycle = $this->request->post('is_recycle');
  194. $update['is_show'] = $fieldVal;
  195. try {
  196. if ($isRecycle) {
  197. $updateRes = $this->model->onlyTrashed()->where('id', '=', $id)->update($update);
  198. } else {
  199. $updateRes = $this->model->where('id', '=', $id)->update($update);
  200. }
  201. if ($updateRes) {
  202. return $this->success('操作成功');
  203. } else if ($updateRes === 0) {
  204. return $this->success('未作修改');
  205. } else {
  206. return $this->error('操作失败');
  207. }
  208. } catch (\Exception $e) {
  209. return $this->exceptionError($e);
  210. }
  211. }
  212. // 复制菜单
  213. public function copy()
  214. {
  215. $pid = (int)$this->request->post('pid');
  216. $ids = $this->request->post('ids');
  217. if (!$ids) {
  218. return $this->error('参数ids不能为空');
  219. }
  220. $data = \app\model\admin\Menu::where('id', 'in', $ids)
  221. ->withoutField('id')->select()
  222. ->each(function ($item) use ($pid) {
  223. $item->pid = $pid;
  224. })->toArray();
  225. $insert = $this->model->insertAll($data);
  226. if ($insert) {
  227. return $this->success('复制成功');
  228. } else {
  229. return $this->error('复制失败');
  230. }
  231. }
  232. // 移动菜单
  233. public function move()
  234. {
  235. $pid = (int)$this->request->post('pid');
  236. $ids = $this->request->post('ids');
  237. if (!$ids) {
  238. return $this->error('参数ids不能为空');
  239. }
  240. $save = \app\model\admin\Menu::where('id', 'in', $ids)
  241. ->save(['pid' => $pid]);
  242. if ($save) {
  243. return $this->success('移动成功');
  244. } else {
  245. return $this->error('移动失败');
  246. }
  247. }
  248. public function recycle()
  249. {
  250. global $_W;
  251. $where = $this->buildSearchParams(false);
  252. $order = $this->buildOrder();
  253. $data = $this->model->onlyTrashed()->where($where)->order($order);
  254. $limit = $this->request->param('limit', Config::get('paginate.limit'));
  255. $data = $data->paginate($limit)->toArray();
  256. $data['data'] = $this->getSelectedData($data['data']);
  257. return $this->success('回收站数据获取成功', $data);
  258. }
  259. }