Menu.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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\common\model\store;
  13. use cores\BaseModel;
  14. use app\common\library\helper;
  15. use think\model\relation\HasMany;
  16. /**
  17. * 商家后台菜单模型
  18. * Class Menu
  19. * @package app\common\model\admin
  20. */
  21. class Menu extends BaseModel
  22. {
  23. // 定义表名
  24. protected $name = 'store_menu';
  25. // 定义表主键
  26. protected $pk = 'menu_id';
  27. /**
  28. * 关联操作权限
  29. * @return HasMany
  30. */
  31. public function menuApi(): HasMany
  32. {
  33. return $this->hasMany('MenuApi', 'menu_id');
  34. }
  35. /**
  36. * 获取所有菜单
  37. * @param array $where
  38. * @return \think\Collection
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. protected static function getAll(array $where = []): \think\Collection
  44. {
  45. // 菜单列表
  46. $model = (new static)->addHidden(['menuApi']);
  47. $list = static::withoutGlobalScope()
  48. ->with(['menuApi'])
  49. ->where($where)
  50. ->order(['sort' => 'asc', 'create_time' => 'asc'])
  51. ->select();
  52. // 整理菜单绑定的apiID集
  53. return $model->getMenuApiIds($list);
  54. }
  55. /**
  56. * 获取树状菜单列表
  57. * @param $menuList
  58. * @param int $parentId
  59. * @return array
  60. */
  61. protected function getTreeData($menuList, int $parentId = 0): array
  62. {
  63. $data = [];
  64. foreach ($menuList as $key => $item) {
  65. if ($item['parent_id'] == $parentId) {
  66. $children = $this->getTreeData($menuList, (int)$item['menu_id']);
  67. !empty($children) && $item['children'] = $children;
  68. $data[] = $item;
  69. unset($menuList[$key]);
  70. }
  71. }
  72. return $data;
  73. }
  74. /**
  75. * 整理菜单的api ID集
  76. * @param $menuList
  77. * @return mixed
  78. */
  79. private function getMenuApiIds($menuList)
  80. {
  81. foreach ($menuList as &$item) {
  82. if (!empty($item['menuApi'])) {
  83. $item['apiIds'] = helper::getArrayColumn($item['menuApi'], 'api_id');
  84. }
  85. }
  86. return $menuList;
  87. }
  88. /**
  89. * 菜单信息
  90. * @param int|array $where
  91. * @return static|array|null
  92. * @throws \think\db\exception\DataNotFoundException
  93. * @throws \think\db\exception\DbException
  94. * @throws \think\db\exception\ModelNotFoundException
  95. */
  96. public static function detail($where)
  97. {
  98. $query = static::withoutGlobalScope();
  99. is_array($where) ? $query->where($where) : $query->where('menu_id', '=', $where);
  100. return $query->find();
  101. }
  102. }