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
91 lines
3.2 KiB
<?php
|
|
|
|
namespace App\Controller\v3;
|
|
|
|
use App\Constants\v3\Tabs;
|
|
use App\Controller\BaseController;
|
|
use App\Model\v3\Goods;
|
|
use App\Model\v3\GoodsCategory;
|
|
|
|
/**
|
|
* 推荐商品相关
|
|
* Class GoodsRecommend
|
|
* @package App\Controller\v3
|
|
*/
|
|
class GoodsRecommendController extends BaseController
|
|
{
|
|
|
|
/**
|
|
* 获取首页tabs推荐商品列表
|
|
* 1、前端上传tab标识
|
|
* 2、根据tab标识从Elasticsearch中获取商品IDs
|
|
* 3、根据IDs获取商品数据
|
|
* 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
|
|
*/
|
|
public function getByTabsForAppletIndex()
|
|
{
|
|
$tab = $this->request->input('tab', '');
|
|
$marketId = $this->request->input('market_id', 0);
|
|
$page = $this->request->input('page', 1);
|
|
$pagesize = $this->request->input('pagesize', 10);
|
|
|
|
$builder = Goods::query()->with('store')
|
|
->where('market_id', $marketId)
|
|
->where(function ($query) {
|
|
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
|
|
});
|
|
|
|
switch ($tab) {
|
|
case Tabs::APPLET_INDEX_RECOMMEND:
|
|
$builder = $builder->orderBy('sales', 'desc');
|
|
break;
|
|
case Tabs::APPLET_INDEX_NEW:
|
|
$builder = $builder->orderBy('created_at', 'desc');
|
|
break;
|
|
case Tabs::APPLET_INDEX_FRESH:
|
|
$builder = $builder->orderBy('price', 'asc');
|
|
break;
|
|
case Tabs::APPLET_INDEX_OFFICE:
|
|
$categoryIds = [97,98];
|
|
// 查询出三级分类
|
|
$goodsCategoryIds = GoodsCategory::query()->whereIn('category_id', $categoryIds)->pluck('id');
|
|
$builder = $builder->where(function ($query) use ($categoryIds, $goodsCategoryIds) {
|
|
$query->whereIn('category_id', $categoryIds)
|
|
->orWhereIn('goods_category_id', $goodsCategoryIds);
|
|
});
|
|
break;
|
|
}
|
|
|
|
$paginate = $builder->paginate($pagesize);
|
|
$goods = $paginate->toArray();
|
|
return $this->success(['has_more_pages' => $paginate->hasMorePages(), 'tab_data' => $goods['data']]);
|
|
|
|
}
|
|
|
|
/**
|
|
* 获取推荐商品列表
|
|
* 1、前端上传标识
|
|
* recommend_search_goods
|
|
* recommend_search_stores
|
|
* recommend_user_index
|
|
* 2、根据标识从Elasticsearch中获取商品IDs
|
|
* 3、根据IDs获取商品数据
|
|
* 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
|
|
*/
|
|
public function getByTab()
|
|
{
|
|
$marketId = $this->request->input('market_id', 0);
|
|
$goods = Goods::query()
|
|
->with(['store'])
|
|
->where('market_id', $marketId)
|
|
->where(function ($query) {
|
|
$query->where('inventory', '>', 0)->orWhere('is_infinite', '=', 1);
|
|
})
|
|
->inRandomOrder()
|
|
->limit(20)
|
|
->get()->toArray();
|
|
return $this->success(['has_more_pages' => false, 'tab_data' => $goods]);
|
|
}
|
|
|
|
|
|
}
|