You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

61 lines
1.7 KiB

5 years ago
5 years ago
5 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Controller\BaseController;
  4. use App\Service\v3\Interfaces\CategoryServiceInterface;
  5. use App\Service\v3\Interfaces\BannerServiceInterface;
  6. use App\Constants\v3\Banner;
  7. use Hyperf\Di\Annotation\Inject;
  8. class CategoryController extends BaseController
  9. {
  10. /**
  11. * @Inject
  12. * @var CategoryServiceInterface
  13. */
  14. protected $categoryService;
  15. /**
  16. * @Inject
  17. * @var BannerServiceInterface
  18. */
  19. protected $bannerService;
  20. /**
  21. * 获取分类页所有分类
  22. * 1、无参数
  23. * 2、远程关联获取所有分类信息
  24. * @return \Psr\Http\Message\ResponseInterface
  25. */
  26. public function all()
  27. {
  28. $category = $this->categoryService->all();
  29. //mock datas
  30. $gmarket_id = $this->request->input('gmarket_id', 0);
  31. if($gmarket_id == -1 ) $gmarket_id = 0;
  32. $banners = $this->bannerService->all(Banner::TYPE_APPLET_INDEX, $gmarket_id);
  33. foreach ($category as $key => &$item) {
  34. $hot_types = [];
  35. if(count($item['goods_types']) > 4){
  36. $hot_types = array_slice($item['goods_types'],0,2);
  37. }
  38. $hot_titles = ['当季热销','为你推荐','热卖品类'];
  39. $rand_index = rand(0,2);
  40. $type_banners = [];
  41. if(count($banners)>0){
  42. $r = rand(0,count($banners)-1);
  43. $type_banners = [$banners[$r]];
  44. }
  45. $item['hot_category'] = ['title'=>$hot_titles[$rand_index],'goods_types'=>$hot_types,'banners'=>$type_banners];
  46. }
  47. return $this->success(['category' => $category]);
  48. }
  49. }