CategoryService.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * 品牌分类
  4. * @author wesmielr
  5. */
  6. namespace app\index\service;
  7. use think\Db;
  8. class CategoryService
  9. {
  10. /**
  11. * 获取首页分类导航列表
  12. * @param bool $refresh
  13. * @return array|bool|\PDOStatement|string|\think\Collection
  14. * @throws \think\db\exception\DataNotFoundException
  15. * @throws \think\db\exception\ModelNotFoundException
  16. * @throws \think\exception\DbException
  17. */
  18. public static function getCateList($refresh = false){
  19. $cacheKey ='cache:index:catearr';
  20. $catearr = RedisService::get($cacheKey);
  21. if(empty($catearr) || $refresh){
  22. //一级分类+二级分类
  23. $catearr = Db::name('category')
  24. ->where('parent_id',0)
  25. ->field('id,catname,enname')
  26. ->order('list_order')
  27. ->limit(0,12)
  28. ->select();
  29. foreach($catearr as $k=>$v){
  30. $erji_cate = Db::name('category')
  31. ->where('parent_id',$v['id'])
  32. ->field('id,enname,catname')
  33. ->order('list_order')
  34. ->limit(0,2)
  35. ->select();
  36. $v['son'] = $erji_cate;
  37. $xmList = Db::name('jiameng')
  38. ->where(['catid|pcatid'=> $v['id'],'status'=> 1])
  39. ->field('id,title')
  40. ->order('list_order')
  41. ->limit(12)
  42. ->select();
  43. $data = $v;
  44. $data['xmList'] = $xmList;
  45. $v['sonData'][] = $data;
  46. // 子类的ID
  47. foreach ($erji_cate as $val){
  48. $xmList = Db::name('jiameng')
  49. ->where(['catid|pcatid'=> $val['id'],'status'=> 1])
  50. ->field('id,title')
  51. ->order('list_order')
  52. ->limit(12)
  53. ->select();
  54. $data = $val;
  55. $data['xmList'] = $xmList;
  56. $v['sonData'][] = $data;
  57. }
  58. $v['son'] = $erji_cate;
  59. $catearr[$k] = $v;
  60. }
  61. if($catearr){
  62. $catearr = $catearr->toArray();
  63. RedisService::set($cacheKey, $catearr, 7 * 24 *3600);
  64. }
  65. }
  66. return $catearr;
  67. }
  68. /**
  69. * 获取主分类行业
  70. * @param int $num 条数
  71. * @return array|\PDOStatement|string|\think\Collection
  72. * @throws \think\db\exception\DataNotFoundException
  73. * @throws \think\db\exception\ModelNotFoundException
  74. * @throws \think\exception\DbException
  75. */
  76. public static function getCates($num=30, $pid=0){
  77. return Db::name('category')
  78. ->where('parent_id',$pid)
  79. ->field('id,catname,enname')
  80. ->order('list_order')
  81. ->limit($num)
  82. ->select();
  83. }
  84. /**
  85. * 获取推荐分类列表以及子类列表
  86. * @param int $num 主分类
  87. * @return $this
  88. * @throws \think\db\exception\DataNotFoundException
  89. * @throws \think\db\exception\ModelNotFoundException
  90. * @throws \think\exception\DbException
  91. */
  92. public static function getRecCates($num=6){
  93. return Db::name('category')
  94. ->where('parent_id',0)
  95. ->field('id,catname,enname')
  96. ->order('list_order')
  97. ->limit($num)
  98. ->select()
  99. ->each(function($item, $k){
  100. $id = isset($item['id'])? intval($item['id']) : 0;
  101. $item['subList'] = [];
  102. if($id){
  103. $subList = CategoryService::getCates(12, $id);
  104. $item['subList'] = $subList? $subList->toArray() : [];
  105. }
  106. return $item;
  107. });
  108. }
  109. }