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.

91 lines
3.2 KiB

5 years ago
5 years ago
5 years ago
5 years ago
6 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. var_dump('ids', $categoryIds);
  45. // 查询出三级分类
  46. $goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id');
  47. $builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds) {
  48. $query->whereIn('category_id', $categoryIds)
  49. ->orWhereIn('goods_category_id', $goodsCategoryIds);
  50. });
  51. break;
  52. }
  53. $paginate = $builder->paginate($pagesize);
  54. $goods = $paginate->toArray();
  55. return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
  56. }
  57. /**
  58. * 获取推荐商品列表
  59. * 1、前端上传标识
  60. * recommend_search_goods
  61. * recommend_search_stores
  62. * recommend_user_index
  63. * 2、根据标识从Elasticsearch中获取商品IDs
  64. * 3、根据IDs获取商品数据
  65. * 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
  66. */
  67. public function getByTab()
  68. {
  69. $marketId = $this->request->input('market_id', 0);
  70. $goods = Goods::query()
  71. ->with(['store'])
  72. ->where('market_id', $marketId)
  73. ->where(function ($query) {
  74. $query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
  75. })
  76. ->inRandomOrder()
  77. ->limit(20)
  78. ->get()->toArray();
  79. return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
  80. }
  81. }