Menu.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\store\model\store;
  13. use app\common\model\store\Menu as MenuModel;
  14. /**
  15. * 商家后台菜单模型
  16. * Class Menu
  17. * @package app\store\model\store
  18. */
  19. class Menu extends MenuModel
  20. {
  21. // 隐藏的字段
  22. protected $hidden = [
  23. 'action_mark',
  24. 'sort',
  25. 'create_time',
  26. 'update_time'
  27. ];
  28. /**
  29. * 根据菜单ID集获取列表
  30. * @param array $menuIds
  31. * @return mixed
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public static function getListByIds(array $menuIds)
  37. {
  38. // 菜单列表
  39. $list = static::getAll([['menu_id', 'in', $menuIds]]);
  40. // 获取树状菜单列表
  41. return (new static)->getTreeData($list);
  42. }
  43. /**
  44. * 获取菜单列表
  45. * @return array
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\DbException
  48. * @throws \think\db\exception\ModelNotFoundException
  49. */
  50. public function getList(): array
  51. {
  52. // 获取所有菜单
  53. $menuList = $this->getTreeData(static::getAll()->toArray());
  54. // 过滤空子项的菜单
  55. return $this->filterEmptyChild($menuList);
  56. }
  57. /**
  58. * 过滤空子项的菜单
  59. * @param array $menuList
  60. * @return array
  61. * @throws \think\db\exception\DataNotFoundException
  62. * @throws \think\db\exception\DbException
  63. * @throws \think\db\exception\ModelNotFoundException
  64. */
  65. private static function filterEmptyChild(array $menuList): array
  66. {
  67. foreach ($menuList as $key => &$item) {
  68. // 判断当前菜单不是页面, 并且子集为空
  69. if (!$item['is_page'] && empty($item['children'])) {
  70. unset($menuList[$key]);
  71. continue;
  72. }
  73. // 递归处理下级
  74. if (!empty($item['children'])) {
  75. $item['children'] = self::filterEmptyChild($item['children']);
  76. }
  77. }
  78. return \array_values($menuList);
  79. }
  80. }