Menu.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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\common\model\store;
  13. use app\common\library\helper;
  14. use app\common\model\BaseModel;
  15. /**
  16. * 商家后台菜单模型
  17. * Class Menu
  18. * @package app\common\model\admin
  19. */
  20. class Menu extends BaseModel
  21. {
  22. // 定义表名
  23. protected $name = 'store_menu';
  24. // 定义表主键
  25. protected $pk = 'menu_id';
  26. /**
  27. * 关联操作权限
  28. * @return \think\model\relation\HasMany
  29. */
  30. public function menuApi()
  31. {
  32. return $this->hasMany('MenuApi', 'menu_id');
  33. }
  34. /**
  35. * 获取菜单列表
  36. * @return array
  37. * @throws \think\db\exception\DataNotFoundException
  38. * @throws \think\db\exception\DbException
  39. * @throws \think\db\exception\ModelNotFoundException
  40. */
  41. public function getList()
  42. {
  43. $list = static::getAll();
  44. return $this->getTreeData($list);
  45. }
  46. /**
  47. * 获取所有菜单
  48. * @param array $where
  49. * @return \think\Collection
  50. * @throws \think\db\exception\DataNotFoundException
  51. * @throws \think\db\exception\DbException
  52. * @throws \think\db\exception\ModelNotFoundException
  53. */
  54. protected static function getAll(array $where = [])
  55. {
  56. // 菜单列表
  57. $model = (new static)->addHidden(['menuApi']);
  58. $list = static::withoutGlobalScope()
  59. ->with(['menuApi'])
  60. ->where($where)
  61. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  62. ->select();
  63. // 整理菜单绑定的apiID集
  64. return $model->getMenuApiIds($list);
  65. }
  66. /**
  67. * 获取树状菜单列表
  68. * @param $menuList
  69. * @param int $parentId
  70. * @return array
  71. */
  72. protected function getTreeData($menuList, int $parentId = 0)
  73. {
  74. $data = [];
  75. foreach ($menuList as $key => $item) {
  76. if ($item['parent_id'] == $parentId) {
  77. $children = $this->getTreeData($menuList, (int)$item['menu_id']);
  78. !empty($children) && $item['children'] = $children;
  79. $data[] = $item;
  80. unset($menuList[$key]);
  81. }
  82. }
  83. return $data;
  84. }
  85. /**
  86. * 整理菜单的api ID集
  87. * @param $menuList
  88. * @return mixed
  89. */
  90. private function getMenuApiIds($menuList)
  91. {
  92. foreach ($menuList as &$item) {
  93. if (!empty($item['menuApi'])) {
  94. $item['apiIds'] = helper::getArrayColumn($item['menuApi'], 'api_id');
  95. }
  96. }
  97. return $menuList;
  98. }
  99. /**
  100. * 菜单信息
  101. * @param int|array $where
  102. * @return array|null|static
  103. * @throws \think\db\exception\DataNotFoundException
  104. * @throws \think\db\exception\DbException
  105. * @throws \think\db\exception\ModelNotFoundException
  106. */
  107. public static function detail($where)
  108. {
  109. $query = static::withoutGlobalScope();
  110. is_array($where) ? $query->where($where) : $query->where('menu_id', '=', $where);
  111. return $query->find();
  112. }
  113. /**
  114. * 获取菜单url集
  115. * @param $menuIds
  116. * @return array
  117. * @throws \think\db\exception\DataNotFoundException
  118. * @throws \think\db\exception\DbException
  119. * @throws \think\db\exception\ModelNotFoundException
  120. */
  121. public static function getMenuUrls($menuIds)
  122. {
  123. $urls = [];
  124. foreach (static::getAll() as $item) {
  125. in_array($item['menu_id'], $menuIds) && $urls[] = $item['url'];
  126. }
  127. return $urls;
  128. }
  129. }