| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- <?php
- /**
- * 品牌分类
- * @author wesmielr
- */
- namespace app\index\service;
- use think\Db;
- class CategoryService
- {
- /**
- * 获取首页分类导航列表
- * @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 getCateList($refresh = false){
- $cacheKey ="cache:index: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')
- ->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;
- }
- }
|