123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141 |
- <?php
- // +----------------------------------------------------------------------
- // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
- // +----------------------------------------------------------------------
- // | Copyright (c) 2017~2021 https://www.yiovo.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
- // +----------------------------------------------------------------------
- // | Author: 萤火科技 <admin@yiovo.com>
- // +----------------------------------------------------------------------
- declare (strict_types=1);
- namespace app\common\model\store;
- use app\common\library\helper;
- use app\common\model\BaseModel;
- /**
- * 商家后台菜单模型
- * Class Menu
- * @package app\common\model\admin
- */
- class Menu extends BaseModel
- {
- // 定义表名
- protected $name = 'store_menu';
- // 定义表主键
- protected $pk = 'menu_id';
- /**
- * 关联操作权限
- * @return \think\model\relation\HasMany
- */
- public function menuApi()
- {
- return $this->hasMany('MenuApi', 'menu_id');
- }
- /**
- * 获取菜单列表
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public function getList()
- {
- $list = static::getAll();
- return $this->getTreeData($list);
- }
- /**
- * 获取所有菜单
- * @param array $where
- * @return \think\Collection
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- protected static function getAll(array $where = [])
- {
- // 菜单列表
- $model = (new static)->addHidden(['menuApi']);
- $list = static::withoutGlobalScope()
- ->with(['menuApi'])
- ->where($where)
- ->order(['sort' => 'asc', 'create_time' => 'asc'])
- ->select();
- // 整理菜单绑定的apiID集
- return $model->getMenuApiIds($list);
- }
- /**
- * 获取树状菜单列表
- * @param $menuList
- * @param int $parentId
- * @return array
- */
- protected function getTreeData($menuList, int $parentId = 0)
- {
- $data = [];
- foreach ($menuList as $key => $item) {
- if ($item['parent_id'] == $parentId) {
- $children = $this->getTreeData($menuList, (int)$item['menu_id']);
- !empty($children) && $item['children'] = $children;
- $data[] = $item;
- unset($menuList[$key]);
- }
- }
- return $data;
- }
- /**
- * 整理菜单的api ID集
- * @param $menuList
- * @return mixed
- */
- private function getMenuApiIds($menuList)
- {
- foreach ($menuList as &$item) {
- if (!empty($item['menuApi'])) {
- $item['apiIds'] = helper::getArrayColumn($item['menuApi'], 'api_id');
- }
- }
- return $menuList;
- }
- /**
- * 菜单信息
- * @param int|array $where
- * @return array|null|static
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function detail($where)
- {
- $query = static::withoutGlobalScope();
- is_array($where) ? $query->where($where) : $query->where('menu_id', '=', $where);
- return $query->find();
- }
- /**
- * 获取菜单url集
- * @param $menuIds
- * @return array
- * @throws \think\db\exception\DataNotFoundException
- * @throws \think\db\exception\DbException
- * @throws \think\db\exception\ModelNotFoundException
- */
- public static function getMenuUrls($menuIds)
- {
- $urls = [];
- foreach (static::getAll() as $item) {
- in_array($item['menu_id'], $menuIds) && $urls[] = $item['url'];
- }
- return $urls;
- }
- }
|