diff --git a/app/Constants/v3/LogLabel.php b/app/Constants/v3/LogLabel.php new file mode 100644 index 0000000..3328736 --- /dev/null +++ b/app/Constants/v3/LogLabel.php @@ -0,0 +1,20 @@ +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]); + } + } \ No newline at end of file diff --git a/app/Controller/v3/SearchController.php b/app/Controller/v3/SearchController.php index c13db94..9d8581c 100644 --- a/app/Controller/v3/SearchController.php +++ b/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(); diff --git a/app/Exception/Handler/ErrorCodeExceptionHandler.php b/app/Exception/Handler/ErrorCodeExceptionHandler.php index dc9c6ab..7cc8575 100644 --- a/app/Exception/Handler/ErrorCodeExceptionHandler.php +++ b/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 diff --git a/app/Model/v3/Goods.php b/app/Model/v3/Goods.php index 4c0848c..3b49baf 100644 --- a/app/Model/v3/Goods.php +++ b/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() diff --git a/app/Model/v3/Store.php b/app/Model/v3/Store.php index 2d66d3c..5d4bab2 100644 --- a/app/Model/v3/Store.php +++ b/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() { diff --git a/config/routes.php b/config/routes.php index 9e015eb..a758aa9 100644 --- a/config/routes.php +++ b/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]]); // 需要登录的路由