findOrEmpty()->toArray(); } //根据上级ID和语言获取下级栏目 public static function getChildrenByParentId($parentId) { if($parentId <= 0){ return []; } $category = self::getById($parentId); if(empty($category)){ return []; } return self::alias('c') ->leftJoin('model m', 'c.model_id=m.id') ->field('c.*, m.manager, m.template, m.name as modelName') ->where('c.parent_id', $parentId) ->order('c.sort','asc') ->select() ->toArray(); } //重写方法 public static function getById($categoryId) { return self::alias('c') ->leftJoin('model m', 'c.model_id = m.id') ->where('c.id', $categoryId) ->field('c.*, m.template') ->findOrEmpty() ->toArray(); } //查看是否有下级栏目 public static function hasChildren($categoryId) { if (is_array($categoryId)) { $count = self::where('parent_id', 'in', $categoryId)->count(); } else { $count = self::where(['parent_id'=>$categoryId])->count(); } return $count ? true : false; } //获取前台菜单 public static function getListForFrontMenu() { $items = self::alias('c') ->leftJoin('model m','c.model_id=m.id') ->field('c.*, m.manager, m.template') ->where('c.state', 1) ->order('is_index desc, sort asc') ->select() ->toArray(); return self::getCates($items); } /** * 获取栏目 * @param bool $limit 是否限制查询 * @param array $cates 需要限制查询的栏目IDs */ public static function getList($limit = false, $cates = []) { if ($limit && empty($cates)) { return []; } return self::alias('c') ->leftJoin('model m', 'c.model_id=m.id') ->field('c.*, m.manager, m.name as modelName') ->when($limit, function($query) use($cates) { $query->whereIn('c.id', $cates); }) ->order('sort','asc') ->select() ->toArray(); } //获取栏目分级列表 public static function getListTree($isMenu = false) { if ($isMenu) { $items = self::where('c.state', 1) ->order('sort','asc')->select()->toArray(); } else { $items = self::order('sort','asc')->select()->toArray(); } return self::getCates($items); } //获取递归栏目 public static function getCates($cates,$parentId=0) { $menus = []; foreach($cates as $cate){ if($cate['parent_id'] == $parentId){ $children = self::getCates($cates,$cate['id']); if(!empty($children)){ $cate['children'] = $children; } $menus[] = $cate; } } return $menus; } public static function onAfterInsert($category) { $category->sort = $category->id; $category->save(); } //递归获取上级栏目名称 public static function getCatesCrumbs($currentId = 0) { $crumbs = []; $category = self::getById($currentId); if($category['parent_id'] == 0){ $cateCrumbs[] = $category['title']; }else{ $categoryIds = explode(',', trim($category['path'], ',')); $categories = self::where('id', 'in', $categoryIds)->column('*', 'id'); foreach($categoryIds as $id){ if(isset($categories[$id])){ $crumbs[] = $categories[$id]['title']; } } } return $crumbs; } //获取栏目中涉及到的文件 public static function getFilesInUse() { $items = self::select()->toArray(); $data = []; foreach($items as $item){ $src = trim($item['src']); if(!empty($src)){ $key = getKeyByPath($src); $data[$key] = $src; } } return $data; } //当前分类的最高级分类Id public static function firstGradeById($id) { $res = 0; $item = self::getById($id); if ($item) { $res = $id; if ($item['parent_id'] > 0) { $items = self::select()->toArray(); $first = self::getFirstGrade($items, $item['parent_id']); if (!empty($first)) { $res = $first['id']; } } } return $res; } public static function getFirstGrade($items, $parentId) { $data = []; foreach ($items as $key=> $item) { if($item['id'] == $parentId) { if ($item['parent_id'] > 0) { unset($items[$key]); $parent = self::getFirstGrade($items, $item['parent_id']); if (!empty($parent)) { $data = $parent; } else { $data = $item; } } else { $data = $item; } } } return $data; } }