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.

73 lines
2.3 KiB

5 years ago
6 years ago
5 years ago
  1. <?php
  2. namespace App\Controller\v3;
  3. use App\Constants\v3\Tabs;
  4. use App\Controller\BaseController;
  5. use App\Model\v3\Goods;
  6. /**
  7. * 推荐商品相关
  8. * Class GoodsRecommend
  9. * @package App\Controller\v3
  10. */
  11. class GoodsRecommendController extends BaseController
  12. {
  13. /**
  14. * 获取首页tabs推荐商品列表
  15. * 1、前端上传tab标识
  16. * 2、根据tab标识从Elasticsearch中获取商品IDs
  17. * 3、根据IDs获取商品数据
  18. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  19. */
  20. public function getByTabsForAppletIndex()
  21. {
  22. $tab = $this->request->input('tab', '');
  23. $page = $this->request->input('page', 1);
  24. $pagesize = $this->request->input('pagesize', 10);
  25. $builder = Goods::query()->with('store');
  26. switch ($tab) {
  27. case Tabs::APPLET_INDEX_RECOMMEND:
  28. $builder = $builder->orderBy('sales', 'desc');
  29. break;
  30. case Tabs::APPLET_INDEX_NEW:
  31. $builder = $builder->orderBy('created_at', 'desc');
  32. break;
  33. case Tabs::APPLET_INDEX_FRESH:
  34. $builder = $builder->orderBy('price', 'asc');
  35. break;
  36. case Tabs::APPLET_INDEX_OFFICE:
  37. $builder = $builder->whereIn('category_id', [97,98]);
  38. break;
  39. }
  40. $paginate = $builder->paginate($pagesize);
  41. $goods = $paginate->toArray();
  42. return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
  43. }
  44. /**
  45. * 获取推荐商品列表
  46. * 1、前端上传标识
  47. * recommend_search_goods
  48. * recommend_search_stores
  49. * recommend_user_index
  50. * 2、根据标识从Elasticsearch中获取商品IDs
  51. * 3、根据IDs获取商品数据
  52. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  53. */
  54. public function getByTab()
  55. {
  56. $goods = Goods::query()
  57. ->with(['store'])
  58. ->inRandomOrder()
  59. ->limit(20)
  60. ->get()->toArray();
  61. return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
  62. }
  63. }