where('parent_id',0) ->field('id,catname,enname') ->order('list_order') ->limit(0,12) ->select(); foreach($catearr as $k=>$v){ $erji_cate = Db::name('category') ->where('parent_id',$v['id']) ->field('id,enname,catname') ->order('list_order') ->limit(0,3) ->select(); $erji_cate = $erji_cate? $erji_cate->toArray() : []; // 子类的ID foreach ($erji_cate as $val){ $xmList = Db::name('jiameng') ->where(['catid'=> $val['id'],'status'=> 1]) ->field('id,title') ->order('list_order') ->limit(12) ->select(); $data = $val; $data['xmList'] = $xmList; $v['sonData'][] = $data; } $v['son'] = $erji_cate? array_slice($erji_cate, 0, 2) : []; $catearr[$k] = $v; } if($catearr){ $catearr = $catearr->toArray(); RedisService::set($cacheKey, $catearr, 7 * 24 *3600); } } return $catearr; } /** * 获取首页分类导航列表 * @param bool $refresh * @return array|bool|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getNavCateList($refresh = false){ $cacheKey ="cache:index:nav_catearr"; $catearr = RedisService::get($cacheKey); if(empty($catearr) || $refresh){ //一级分类+二级分类 $catearr = Db::name('category') ->where('parent_id',0) ->field('id,catname,enname') ->order('list_order') ->limit(0,12) ->select(); foreach($catearr as $k=>$v){ $erji_cate = Db::name('category') ->where('parent_id',$v['id']) ->field('id,enname,catname') ->order('list_order') ->select(); $erji_cate = $erji_cate? $erji_cate->toArray() : []; $v['son'] = $erji_cate; $catearr[$k] = $v; } if($catearr){ $catearr = $catearr->toArray(); RedisService::set($cacheKey, $catearr, 7 * 24 *3600); } } return $catearr; } /** * 获取主分类行业 * @param int $num 条数 * @return array|\PDOStatement|string|\think\Collection * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getCates($num=30, $pid=0, $field='', $orderBy = 'list_order'){ $cacheKey = "cache:cates:list_byparent_".$pid.'_n'.$num.($field? ':'.md5($field) : ''); $dataList = RedisService::get($cacheKey); if ($dataList) { return $dataList; } $field = $field? $field : 'id,catname,enname'; $dataList = Db::name('category') ->where(function($query) use ($pid){ if($pid>=0){ $query->where('parent_id', $pid); } }) ->field($field) ->order($orderBy) ->limit($num) ->select(); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { RedisService::set($cacheKey, $dataList, 6 * 3600); } return $dataList; } /** * 获取推荐分类列表以及子类列表 * @param int $num 主分类 * @return $this * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getRecCates($num=6){ $cacheKey = "cache:cates:recommend_".$num; $dataList = RedisService::get($cacheKey); if ($dataList) { return $dataList; } $dataList = Db::name('category') ->where('parent_id',0) ->field('id,catname,enname') ->order('list_order') ->limit($num) ->select() ->each(function($item, $k){ $id = isset($item['id'])? intval($item['id']) : 0; $item['subList'] = []; if($id){ $item['subList'] = CategoryService::getCates(12, $id); } return $item; }); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { RedisService::set($cacheKey, $dataList, 3 * 3600); } return $dataList; } /** * 获取推荐分类列表以及子类列表 * @param int $num 主分类 * @return $this * @throws \think\db\exception\DataNotFoundException * @throws \think\db\exception\ModelNotFoundException * @throws \think\exception\DbException */ public static function getHotCates($num=6){ $cacheKey = "cache:cates:hots_".$num; $dataList = RedisService::get($cacheKey); if ($dataList) { return $dataList; } $catIds = Db::name('jiameng') ->where('status',1) ->order(db()->raw("sum('hits')")) ->limit($num) ->group('catid') ->column('catid'); if(empty($catIds)){ return false; } $dataList = Db::name('category') ->whereIn('id',$catIds) ->field('id,catname,enname') ->order('list_order') ->limit($num) ->select(); $dataList = $dataList ? $dataList->toArray() : []; if ($dataList) { RedisService::set($cacheKey, $dataList, 3 * 3600); } return $dataList; } }