diff --git a/app/api/controller/Category.php b/app/api/controller/Category.php index 197b9bf3..f0f4b1eb 100644 --- a/app/api/controller/Category.php +++ b/app/api/controller/Category.php @@ -55,8 +55,12 @@ class Category extends Controller $model = new CategoryModel; $param['status'] = 1; - $list = $model->getChildrenList($param); - Cache::set($cache_key, $list, 3600); + $list = []; + if (isset($param['category_id']) && $param['category_id']) { + $list = $model->getChildrenList($param); + } + + //Cache::set($cache_key, $list, 3600); return $this->renderSuccess(compact('list')); } diff --git a/app/api/controller/GoodsNew.php b/app/api/controller/GoodsNew.php index 3377f8cf..a157ae04 100644 --- a/app/api/controller/GoodsNew.php +++ b/app/api/controller/GoodsNew.php @@ -19,25 +19,15 @@ class GoodsNew extends Controller public function search() { - $keyword = $this->request->param('keyword'); + $param = $this->request->param(); $page = $this->request->param('page', 1); - $limit = $this->request->param('limit', 10); - if (!$keyword) { - $this->renderError('请输入搜索关键字'); - } - $params = ['goods_name' => $keyword, 'status' => 10]; - - //限制过滤条件-渠道 - $params['channels'] = $this->storeInfo['open_channel'] ? array_merge(['zy'], explode(",", $this->storeInfo['open_channel'])) : []; - //分类利润-利润率 - $params['fliter_condition'] = $this->storeInfo['fliter_condition']; - - + $pageSize = $this->request->param('pageSize', 10); + $param['channels'] = $this->storeInfo['open_channel'] ? array_merge(['zy'], explode(",", $this->storeInfo['open_channel'])) : []; + $param['fliter_condition'] = $this->storeInfo['fliter_condition']; + $param['status'] = 10; + $param['store_id'] = $this->storeId; $goodsService = new GoodsCateEs(); - $list = $goodsService->list( $params, $page, $limit); - $data['list'] = $list; - $data['total'] = $goodsService->count($params); - + $data = $goodsService->list($param, $page, $pageSize); return $this->renderSuccess($data); } diff --git a/app/api/service/Goods.php b/app/api/service/Goods.php index 6be8cd53..44703d89 100644 --- a/app/api/service/Goods.php +++ b/app/api/service/Goods.php @@ -247,7 +247,7 @@ class Goods extends GoodsService $goodsList = $model->getList([ 'status' => 10, 'is_delete' => 0, - 'paihang' => 1, + 'is_paihang' => 1, 'store_id' => $storeid, 'categoryId' => (int)$_GET['categoryId'], ], 15); @@ -275,7 +275,7 @@ class Goods extends GoodsService $goodsList = $model->getList([ 'status' => 10, 'is_delete' => 0, - 'paihang' => 1, + 'is_paihang' => 1, 'categoryId' => $v['category_id'], ], 3); $v['goods_list'] = $goodsList->toArray()['data']; diff --git a/app/common/library/elasticsearch/Client.php b/app/common/library/elasticsearch/Client.php index 916644b6..90bde7b5 100644 --- a/app/common/library/elasticsearch/Client.php +++ b/app/common/library/elasticsearch/Client.php @@ -67,6 +67,7 @@ class Client public function from($number = 0) { $this->queryParams['from'] = $number ? ($number-1)*10 : 0; + $this->queryParams['current_page'] = $number; return $this; } @@ -88,7 +89,7 @@ class Client */ public function equal($params = []) { - $this->queryParams['equal'][] = $params; + $this->queryParams['equal'] = $params; return $this; } @@ -99,7 +100,7 @@ class Client */ public function in($params = []) { - $this->queryParams['in'][] = $params; + $this->queryParams['in'] = $params; return $this; } @@ -119,13 +120,15 @@ class Client */ public function like($params = []) { - $this->queryParams['like'][] = $params; + //$this->queryParams['like'][] = $params; + $this->queryParams['like'] = $params; return $this; } public function orlike($params = []) { - $this->queryParams['orlike'][] = $params; + //$this->queryParams['orlike'][] = $params; + $this->queryParams['orlike'] = $params; return $this; } @@ -153,7 +156,7 @@ class Client public function order($params = []) { - $this->queryParams['order'][] = $params; + $this->queryParams['order'] = $params; return $this; } @@ -238,22 +241,13 @@ class Client 'filter' => [] ] ], - ], - 'highlight'=>[//高亮 - "fields"=>[ - "goods_name" => new \stdClass() - // "activity_name"=>[ - // "pre_tags"=>"", - // "post_tags"=>"" - // ] - ], - "pre_tags"=>"", - "post_tags"=>"" - ], + 'from' => $this->queryParams['from'] ?? 0, - 'size' => $this->queryParams['size'] ?? 10 + 'size' => $this->queryParams['size'] ?? 10, + 'current_page' => $this->queryParams['current_page'] ?? 1, + 'track_total_hits' => true, ]; @@ -404,7 +398,7 @@ class Client public function query($isTotal = false) { -// try { + try { $this->parseQueryParams(); //dd($this->queryParams); @@ -414,59 +408,53 @@ class Client } if ($isTotal === true) { - unset( - $this->queryParams['from'], - $this->queryParams['size'], - $this->queryParams['_source'] - ); - $count = $this->client->count($this->queryParams); - return (int)$count['count']; - } if (!empty($this->queryParams)) { - + //高亮 + $this->queryParams['body']['highlight'] = [ + "fields"=>[ + "goods_name" => new \stdClass() + ], + "pre_tags"=>"", + "post_tags"=>"" + ]; $result = $this->client->search($this->queryParams); - echo "
"; - print_r($result); - exit(); - - if (!empty($result['hits']['hits'])) { - - $return = []; - - foreach ($result['hits']['hits'] as $row) { - - $return[] = $row['_source']; - + $data = [ + 'current_page' => 1, + 'data' => [], + 'last_page' => 0, + 'per_page' => $this->queryParams['size'], + 'total' => 0, + ]; + if (isset($result['hits']) && !empty($result['hits'])) { + $list = $result['hits']['hits']; + $arr = []; + foreach ($list as $key => &$value) { + $value['_source']['highlight_goods_name'] = $value['highlight']['goods_name'][0] ?? ""; + $arr[] = $value['_source']; } - - return $return; - - } else { - - return []; - + $data['current_page'] = $this->queryParams['current_page']; + $data['data'] = $arr; + $data['last_page'] = ceil($result['hits']['total']['value'] / $this->queryParams['size']); + $data['per_page'] = $this->queryParams['size']; + $data['total'] = $result['hits']['total']['value']; } + return $data; } - -// } catch (\Exception $e) { -// -// $msg = $e->getMessage(); -// -// $msg = '服务器开小差了~'; -// -// throw new Exception($msg); -// -// } + } catch (\Exception $e) { + $msg = $e->getMessage(); + //$msg = '服务器开小差了~'; + throw new Exception($msg); + } } /** diff --git a/app/common/model/Goods.php b/app/common/model/Goods.php index 26f62d39..02177c1f 100644 --- a/app/common/model/Goods.php +++ b/app/common/model/Goods.php @@ -482,10 +482,7 @@ class Goods extends BaseModel // $filter[] = ['is_new', '=', intval($param['is_new'])]; // } - // if (!empty($param['paihang'])) { - // $filter[] = ['paihang', '>', 0]; - // $query->order('paihang asc'); - // } + if (isset($param['spec_type']) && $param['spec_type'] !== '') { $filter[] = ['spec_type', '=', $params['spec_type']]; } @@ -538,9 +535,14 @@ class Goods extends BaseModel if (isset($param['is_in_store']) && $param['is_in_store'] !== '') { $filter[] = ['goods.is_in_store', '=', $params['is_in_store']]; } + // if (!empty($param['paihang'])) { + // $filter[] = ['paihang', '>', 0]; + // $query->order('paihang asc'); + // } if (isset($param['is_paihang']) && $param['is_paihang'] !== '') { if ($param['is_paihang'] > 0 ) { $filter[] = ['goods.paihang', '>', 0]; + $query->order('paihang asc'); } else { $filter[] = ['goods.paihang', '=', 0]; } diff --git a/app/common/service/GoodsCateEs.php b/app/common/service/GoodsCateEs.php index c16eb579..d962b6fc 100644 --- a/app/common/service/GoodsCateEs.php +++ b/app/common/service/GoodsCateEs.php @@ -6,6 +6,7 @@ use app\common\library\elasticsearch\Client; use app\common\model\GoodsCategoryRel; use app\common\model\Goods; use think\Exception; +use app\common\library\helper; class GoodsCateEs { @@ -13,7 +14,7 @@ class GoodsCateEs private string $index_name = 'goods_category'; - private string $field = 'goods_name,goods_id,category_id,profit,profit_rate,status,create_time'; + private string $field = 'goods_name,goods_id,category_id,goods_price_min,profit,profit_rate,status,create_time,goods_no,sort,store_id'; public function __construct() { @@ -22,46 +23,80 @@ class GoodsCateEs /** - * 查询分类列表 + * 查询商品列表 * @param $params */ public function list(array $params = [],$page = 1, $limit = 10) { - $between = $ins = []; - $like = ['goods_name' => $params['goods_name'] ?? '']; - $equal = ['status' => $params['status']]; + + $between = $ins = $orlike = $equal = []; + //商品名称like查询 + if (isset($params['goods_name']) && $params['goods_name']) { + $orlike[] = ['goods_name' => $params['goods_name'] ?? '']; + $orlike[] = ['goods_no' => $params['goods_name'] ?? '']; + } + //等于查询 + if (isset($params['status']) && $params['status']) { + $equal[] = ['status' => $params['status'] ?? 0]; + } + if (isset($params['store_id']) && $params['store_id']) { + $equal[] = ['store_id' => $params['store_id'] ?? 0]; + } + //between查询 if (isset($params['fliter_condition']) && $params['fliter_condition']) { $fliter_condition = json_decode($params['fliter_condition'], true); - foreach ($fliter_condition as $key => $value) - { + foreach ($fliter_condition as $key => $value) { $between[] = [ 'category_id' => $value['category'], 'profit' => $value['profit'], 'profit_rate' => $value['profit_rate'] ]; } - unset($params['fliter_condition']); - } - - if (!empty($params['channels'])) { - $ins = ['channels' => $params['channels']]; - unset($params['channels']); + //in查询 + if (isset($params['channels']) && !empty($params['channels'])) { + $ins[] = ['channel' => $params['channels']]; } - + + $sort = $this->setQuerySort($params); + + //dd($sort); return $this->esService ->select($this->field) - ->like($like) + ->orlike($orlike) ->hasChild($between) ->in($ins) ->equal($equal) ->from($page) ->size($limit) - ->order(['goods_id' => 'desc']) -// ->setDebug() + ->order($sort) + //->setDebug() ->query(); } - + /** + * 检索排序条件 + * @param array $param + * @return array|string[] + */ + private function setQuerySort(array $param = []): array + { + $params = helper::setQueryDefaultValue($param, [ + 'sortType' => 'all', // 排序类型 (all默认 sales销量 price价格) + 'sortPrice' => false, // 价格排序 (true高到低 false低到高) + ]); + //var_dump($params); + // 排序规则 + $sort = []; + if ($params['sortType'] === 'all') { + $sort[] = ['sort' => 'asc']; + } elseif ($params['sortType'] === 'sales') { + $sort[] = ['goods_sales' => 'desc']; + } elseif ($params['sortType'] === 'price') { + $sort[] = $params['sortPrice'] ? ['goods_price_min' => 'desc'] : ['goods_price_min' => 'asc']; + } + $sort[] = ["goods_id" => 'desc']; + return $sort; + } public function count($params = [],$page = 1, $limit = 10) { $between = $ins = [];