220 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			220 lines
		
	
	
		
			6.4 KiB
		
	
	
	
		
			PHP
		
	
	
| <?php
 | |
| namespace app\model;
 | |
| 
 | |
| class Category extends Base
 | |
| {
 | |
|     public static  $CIdList = [
 | |
|         'marketing_network' => 15, // 网络营销(主栏目)
 | |
|         'marketing_network_child' => 16, // 网络营销(子栏目)
 | |
|         'marketing_achievement' => 17, // 主要业绩
 | |
|         'products' => 2, // 产品中心
 | |
|         'news_enterprise' => 20, // 企业新闻
 | |
|         'news_industry' => 21, // 行业资讯
 | |
|         'news_dynamic' => 21, // 新闻动态
 | |
|     ];
 | |
| 
 | |
|     //获取首页栏目ID
 | |
|     public static function getIndex()
 | |
|     {
 | |
|         return self::where('is_index', 1)->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){
 | |
|             $crumbs[] = $category;
 | |
|         }else{
 | |
|             $categoryIds = explode(',', trim($category['path'], ','));
 | |
|             $categories = self::alias('c')
 | |
|             ->leftJoin('model m', 'c.model_id = m.id')
 | |
|             ->where('c.id', 'in', $categoryIds)
 | |
|             ->column('c.*,m.template', 'c.id');
 | |
|             foreach($categoryIds as $id){
 | |
|                 if(isset($categories[$id])){
 | |
|                     $crumbs[] = $categories[$id];
 | |
|                 }
 | |
|             }
 | |
|             $crumbs[] = $category;
 | |
|         }
 | |
|         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;
 | |
|     }
 | |
| 
 | |
|     //根据栏目ID获取面包屑列表
 | |
|     public static function getListForCrumbs($categoryId)
 | |
|     {
 | |
|         $categories = [];
 | |
|         $category = self::getById($categoryId);
 | |
|         if(!empty($category)){
 | |
|             $categories[] = $category;
 | |
|             if($category['parent_id'] != 0 ){
 | |
|                 $parents = self::getListForCrumbs($category['parent_id']);
 | |
|                 if(!empty($parents)){
 | |
|                     $categories = array_merge($categories, $parents);
 | |
|                 }
 | |
|             }
 | |
|         }
 | |
|         return $categories;
 | |
|     }
 | |
| }
 |