Browse Source

搜索页为你推荐

master
weigang 6 years ago
parent
commit
ddc54e2627
  1. 20
      app/Constants/v3/LogLabel.php
  2. 5
      app/Constants/v3/SsdbKeys.php
  3. 10
      app/Constants/v3/Tabs.php
  4. 102
      app/Controller/v3/GoodsRecommendController.php
  5. 8
      app/Controller/v3/SearchController.php
  6. 16
      app/Exception/Handler/ErrorCodeExceptionHandler.php
  7. 5
      app/Model/v3/Goods.php
  8. 5
      app/Model/v3/Store.php
  9. 1
      config/routes.php

20
app/Constants/v3/LogLabel.php

@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace App\Constants\v3;
use Hyperf\Constants\AbstractConstants;
use Hyperf\Constants\Annotation\Constants;
/**
* @Constants
*/
class LogLabel extends AbstractConstants
{
/**
* @Message("Error Code Log")
*/
const ERROR_CODE_EXCEPTION = 'error_code_exception';
}

5
app/Constants/v3/SsdbKeys.php

@ -19,4 +19,9 @@ class SsdbKeys extends AbstractConstants
* @Message("商品月销")
*/
const GOODS_MONTH_SALES = 'goods_m_sales_';
/**
* @Message("商户月销")
*/
const STORE_MONTH_SALES = 'store_m_sales_';
}

10
app/Constants/v3/Tabs.php

@ -29,4 +29,14 @@ class Tabs extends AbstractConstants
* @Message("小程序首页上班带餐")
*/
const APPLET_INDEX_OFFICE = 'applet_index_office';
/**
* @Message("搜索商品为你推荐")
*/
const RECOMMEND_FOR_SEARCH_GOODS = 'recommend_search_goods';
/**
* @Message("搜索商户为你推荐")
*/
const RECOMMEND_FOR_SEARCH_STORE = 'recommend_search_store';
}

102
app/Controller/v3/GoodsRecommendController.php

@ -13,6 +13,95 @@ use App\Controller\BaseController;
class GoodsRecommendController extends BaseController
{
protected $data;
public function __construct()
{
$this->data = [
[
'goods' => [
'id' => 1,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜11',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'tags' => ['限时', '折扣'],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 10,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
],
'store' => ['id' => 111, 'logo' => '', 'name' => '我是一个商家']
],
[
'goods' => [
'id' => 2,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜11',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'tags' => ['限时', '折扣'],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 10,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
],
'store' => ['id' => 222, 'logo' => '', 'name' => '我是一个商家222']
],
[
'goods' => [
'id' => 3,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜11',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'tags' => ['限时', '折扣'],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 10,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
],
'store' => ['id' => 333, 'logo' => '', 'name' => '我是一个商家333']
],
[
'goods' => [
'id' => 4,
'cover_img' => config('alioss.img_host').'/attachment/types/c9b656181bbbc463624ca3803c5be7539f2fd62253f9a-cwVLri.png',
'name' => '【优质】大白菜11',
'spec' => [
['key' => '净含量', 'value' => '500g']
],
'tags' => ['限时', '折扣'],
'original_price' => 50.5,
'price' => 25.25,
'inventory' => 10,
'month_sales' => 20,
'total_sales' => 20,
'cart_num' => 0,
'is_effective' => 2,
'noneffective_note' => '已卖完',
],
'store' => ['id' => 444, 'logo' => '', 'name' => '我是一个商家444']
]
];
parent::__construct();
}
/**
* 获取首页tabs推荐商品列表
* 1、前端上传tab标识
@ -119,4 +208,17 @@ class GoodsRecommendController extends BaseController
'tab_data' => $tabsData[$this->request->post('tab', 'applet_index_recommend')]
]);
}
/**
* 获取搜索页推荐商品列表
* 1、前端上传标识recommend_search_goods recommend_search_stores
* 2、根据标识从Elasticsearch中获取商品IDs
* 3、根据IDs获取商品数据
* 4、返回数据,id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
*/
public function getForSearch()
{
return $this->success(['tab_data' => $this->data]);
}
}

8
app/Controller/v3/SearchController.php

@ -47,6 +47,14 @@ class SearchController extends BaseController
return $this->success(['goods' => $data]);
}
/**
* 商户搜索
* 1、筛选条件:商户分类、商户关键词
* 2、排序:综合排序、销量最多
* 3、返回数据格式,大购物车统计+商品数据含有id、封面图、名称、原价、现价、库存、月销、tag、规格、购物车相关、商户id、商户avatar、商户名
* @param SearchStoreRequest $request
* @return ResponseInterface
*/
public function stores(SearchStoreRequest $request)
{
$params = $request->validated();

16
app/Exception/Handler/ErrorCodeExceptionHandler.php

@ -2,15 +2,23 @@
namespace App\Exception\Handler;
use App\Commons\Log;
use App\Constants\v3\LogLabel;
use App\Exception\ErrorCodeException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\ExceptionHandler\ExceptionHandler;
use Hyperf\HttpMessage\Stream\SwooleStream;
use Psr\Http\Message\ResponseInterface;
use Throwable;
use Hyperf\Di\Annotation\Inject;
class ErrorCodeExceptionHandler extends ExceptionHandler
{
/**
* @Inject
* @var Log
*/
protected $log;
/**
* @var StdoutLoggerInterface
@ -29,16 +37,18 @@ class ErrorCodeExceptionHandler extends ExceptionHandler
$this->stopPropagation();
$content = json_encode([
$content = [
"status" => 'error',
"code" => $throwable->getCode(),
"result" => [],
"message" => $throwable->getMessage()
]);
];
$this->log->event(LogLabel::ERROR_CODE_EXCEPTION, $content);
return $response->withHeader('Content-Type', 'application/json')
->withStatus(200)
->withBody(new SwooleStream($content));
->withBody(new SwooleStream(json_encode($content)));
}
public function isValid(Throwable $throwable): bool

5
app/Model/v3/Goods.php

@ -80,9 +80,8 @@ class Goods extends Model
public function getMonthSalesAttribute()
{
return 1;
// $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
// return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
return (integer)$ssdb->exec('get', SsdbKeys::GOODS_MONTH_SALES.date('YM').'_'.$this->id);
}
public function getCartNumAttribute()

5
app/Model/v3/Store.php

@ -3,9 +3,11 @@
declare (strict_types=1);
namespace App\Model\v3;
use App\Constants\v3\SsdbKeys;
use App\Model\Model;
use Hyperf\Database\Model\Builder;
use Hyperf\Database\Model\SoftDeletes;
use Hyperf\Utils\ApplicationContext;
/**
*/
@ -58,7 +60,8 @@ class Store extends Model
public function scopeOrderBySales($query, $sort)
{
return $query->orderBy('sales', $sort);
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
return (integer)$ssdb->exec('get', SsdbKeys::STORE_MONTH_SALES.date('YM').'_'.$this->id);
}
public function getMonthSalesAttribute() {

1
config/routes.php

@ -95,6 +95,7 @@ Router::addGroup('/v3/', function () {
Router::post('search/hotKeywords', 'App\Controller\v3\SearchController@hotKeywords');
Router::post('search/goods', 'App\Controller\v3\SearchController@goods');
Router::post('search/stores', 'App\Controller\v3\SearchController@stores');
Router::post('goodsRecommend/getForSearch', 'App\Controller\v3\GoodsRecommendController@getForSearch');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class]]);
// 需要登录的路由

Loading…
Cancel
Save