238 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			PHP
		
	
	
			
		
		
	
	
			238 lines
		
	
	
		
			7.3 KiB
		
	
	
	
		
			PHP
		
	
	
<?php
 | 
						|
namespace app\model;
 | 
						|
 | 
						|
class Category extends Base
 | 
						|
{
 | 
						|
    public static  $CIdList = [
 | 
						|
        'about'                     => 1, // 走进超宇
 | 
						|
        'about_children'            => [
 | 
						|
            'company'               => 3, // 企业简介
 | 
						|
            'honor'                 => 8, // 资质荣誉
 | 
						|
            'structure'             => 9, // 组织架构
 | 
						|
            'history'               => 10, // 发展历程
 | 
						|
            'video'                 => 11, // 企业视频
 | 
						|
        ],
 | 
						|
        'honors_manage'             => 26, // 荣誉资质管理
 | 
						|
        'history_manage'            => 29, // 发展历程管理
 | 
						|
        'products'                  => 2, // 产品中心
 | 
						|
        'marketing'                 => 15, // 网络营销(主)
 | 
						|
        'marketing_children'        => [
 | 
						|
            'marketing_network'     => 16, // 网络营销
 | 
						|
            'achievement'           => 17, // 主要业绩
 | 
						|
        ],
 | 
						|
        'news'                      => 18, //新闻(主)
 | 
						|
        'news_children'             => [
 | 
						|
            'enterprise'            => 20, // 企业新闻
 | 
						|
            'industry'              => 21, // 行业资讯
 | 
						|
            'dynamics'              => 22, // 新闻动态
 | 
						|
        ],
 | 
						|
    ];
 | 
						|
 | 
						|
    //获取首页栏目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)
 | 
						|
        ->where('c.is_menu', 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) {
 | 
						|
            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;
 | 
						|
    }
 | 
						|
}
 |