SystemMenu.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. namespace app\admin\model;
  3. use app\common\constants\MenuConstant;
  4. use app\common\model\TimeModel;
  5. use think\db\exception\DataNotFoundException;
  6. use think\db\exception\DbException;
  7. use think\db\exception\ModelNotFoundException;
  8. class SystemMenu extends TimeModel
  9. {
  10. protected function getOptions(): array
  11. {
  12. return [
  13. 'deleteTime' => 'delete_time',
  14. ];
  15. }
  16. /**
  17. * @throws ModelNotFoundException
  18. * @throws DbException
  19. * @throws DataNotFoundException
  20. */
  21. public static function getPidMenuList(): array
  22. {
  23. $list = self::field('id,pid,title')->where([
  24. ['pid', '<>', MenuConstant::HOME_PID],
  25. ['status', '=', 1],
  26. ])->select()->toArray();
  27. $pidMenuList = self::buildPidMenu(0, $list);
  28. return array_merge([[
  29. 'id' => 0,
  30. 'pid' => 0,
  31. 'title' => '顶级菜单',
  32. ]], $pidMenuList);
  33. }
  34. protected static function buildPidMenu($pid, $list, $level = 0): array
  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 = self::buildPidMenu($vo['id'], $list, $level);
  54. !empty($childList) && $newList = array_merge($newList, $childList);
  55. }
  56. }
  57. return $newList;
  58. }
  59. }