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.

90 lines
3.2 KiB

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