diff --git a/server/app/api/controller/Goods.php b/server/app/api/controller/Goods.php index a1fc31b3..90315c43 100644 --- a/server/app/api/controller/Goods.php +++ b/server/app/api/controller/Goods.php @@ -100,4 +100,20 @@ class Goods extends Api return JsonServer::error('请求方式错误'); } } + + // 获取商品的推荐商品 + public function getRecommendGoods() + { + if($this->request->isGet()) { + $goodsId = input('goods_id/d',0); + if (empty($goodsId)) { + return JsonServer::error('参数错误'); + } + + $data = GoodsLogic::getRecommendGoodsById($goodsId); + return JsonServer::success('获取成功', $data); + }else{ + return JsonServer::error('请求方式错误'); + } + } } \ No newline at end of file diff --git a/server/app/api/logic/GoodsLogic.php b/server/app/api/logic/GoodsLogic.php index 0fe6608b..cf9081e6 100644 --- a/server/app/api/logic/GoodsLogic.php +++ b/server/app/api/logic/GoodsLogic.php @@ -5,6 +5,7 @@ namespace app\api\logic; use app\common\model\distribution\Distribution; use app\common\model\distribution\DistributionLevel; +use app\common\model\goods\GoodsCategory; use app\common\model\shop\ShopFollow; use app\common\model\user\User; use app\common\basics\Logic; @@ -499,6 +500,47 @@ class GoodsLogic extends Logic return $data; } + /** + * 根据商品栏目获取商品列表 + */ + public static function getRecommendGoodsById($goodsId, $limit = 6) + { + // 销售中商品:未删除/审核通过/已上架 + $onSaleWhere = [ + ['del', '=', GoodsEnum::DEL_NORMAL], + ['status', '=', GoodsEnum::STATUS_SHELVES], + ['audit_status', '=', GoodsEnum::AUDIT_STATUS_OK], + ]; + + $goodsInfo = Goods::where('id', $goodsId)->field('id,name,third_cate_id')->find(); + if (empty($goodsInfo)) { + return []; + } + + + + if (!empty(self::filterShopsIds())) { + // 过滤已删除、已冻结、已暂停营业、已到期的店铺 + $onSaleWhere[] = ['shop_id', 'not in', self::filterShopsIds()]; + } + +// $order = [ +// 'sort_weight' => 'asc', // 数字越小,权重越大 +// 'sales_actual' => 'desc', +// 'id' => 'desc' +// ]; + + $list = Goods::field('id,name,image,market_price,min_price,sales_actual,column_ids,sort_weight,sales_virtual,(sales_actual + sales_virtual) as sales_total') + ->where($onSaleWhere) + ->where('id', "<>", $goodsId) + ->whereFindInSet('column_ids', $goodsInfo['third_cate_id']) + ->orderRaw('RAND()') + ->limit($limit) + ->select(); + + return $list->toArray(); + } + /** * @notes 获取已删除、已冻结、已暂停营业、已到期店铺的id * @return array