SystemMenu.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | EasyAdmin
  4. // +----------------------------------------------------------------------
  5. // | PHP交流群: 763822524
  6. // +----------------------------------------------------------------------
  7. // | 开源协议 https://mit-license.org
  8. // +----------------------------------------------------------------------
  9. // | github开源项目:https://github.com/zhongshaofa/EasyAdmin
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model;
  12. use app\common\constants\MenuConstant;
  13. use app\common\model\TimeModel;
  14. class SystemMenu extends TimeModel
  15. {
  16. protected $deleteTime = 'delete_time';
  17. public function getPidMenuList()
  18. {
  19. $list = $this->field('id,pid,title')
  20. ->where([
  21. ['pid', '<>', MenuConstant::HOME_PID],
  22. ['status', '=', 1],
  23. ])
  24. ->select()
  25. ->toArray();
  26. $pidMenuList = $this->buildPidMenu(0, $list);
  27. $pidMenuList = array_merge([[
  28. 'id' => 0,
  29. 'pid' => 0,
  30. 'title' => '顶级菜单',
  31. ]], $pidMenuList);
  32. return $pidMenuList;
  33. }
  34. protected function buildPidMenu($pid, $list, $level = 0)
  35. {
  36. $newList = [];
  37. foreach ($list as $vo) {
  38. if ($vo['pid'] == $pid) {
  39. $level++;
  40. foreach ($newList as $v) {
  41. if ($vo['pid'] == $v['pid'] && isset($v['level'])) {
  42. $level = $v['level'];
  43. break;
  44. }
  45. }
  46. $vo['level'] = $level;
  47. if ($level > 1) {
  48. $repeatString = "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
  49. $markString = str_repeat("{$repeatString}├{$repeatString}", $level - 1);
  50. $vo['title'] = $markString . $vo['title'];
  51. }
  52. $newList[] = $vo;
  53. $childList = $this->buildPidMenu($vo['id'], $list, $level);
  54. !empty($childList) && $newList = array_merge($newList, $childList);
  55. }
  56. }
  57. return $newList;
  58. }
  59. }